Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Exchange/ExchangeConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Este arquivo contém as constantes e mensagens de erro.
*
* @category Challenge
* @package Back-end
* @author Kayo Almondes Tusthler <kayotusthler@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/

declare(strict_types=1);

namespace App\Exchange;

/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Esta classe é responsável por configurar as constantes e mensagens de erro.
*
* @category Challenge
* @package Back-end
* @author Kayo Almondes Tusthler <kayotusthler@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
class ExchangeConfig
{
public const ROUTE = 'exchange';

public const SIGN_SYMBOL = [
'BRL' => 'R$',
'EUR' => '€',
'USD' => '$'
];

public const PERMITTED_CURRENCIES = [
'BRL',
'EUR',
'USD'
];

public const ERROR_MESSAGES = [
'INVALID_AMOUNT' => 'Valor invalido > 0',
'INVALID_CURRENCY' => 'Moedas validas: USD, BRL, EUR',
'INVALID_RATE' => 'Taxa invalida. Use: > 0',
'INVALID_ROUTE' => 'Rota válida: /exchange/{amount}/{from}/{to}/{rate}'
];
}
155 changes: 155 additions & 0 deletions src/Exchange/ExchangeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Este arquivo lida com a requisição
* e retorna o resultado da conversão de moedas.
*
* @category Challenge
* @package Back-end
* @author Kayo Almondes Tusthler <kayotusthler@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/

declare(strict_types=1);

namespace App\Exchange;

/**
* Back-end Challenge.
*
* PHP version 7.4
*
* Esta classe lida com a requisição
* e retorna o resultado da conversão de moedas.
*
* @category Challenge
* @package Back-end
* @author Kayo Almondes Tusthler <kayotusthler@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
class ExchangeHandler
{
private const PARAM_AMOUNT = 1;
private const PARAM_FROM = 2;
private const PARAM_TO = 3;
private const PARAM_RATE = 4;

/**
* Lida com a requisição
* e retorna o resultado da conversão de moedas.
*
* @param string $requestUri URL da requisição
*
* @return void
*/
public function handle(string $requestUri): void
{
$parts = $this->_parsePath($requestUri);

if (count($parts) !== 5) :
$this->_respondError(
400,
ExchangeConfig::ERROR_MESSAGES['INVALID_ROUTE']
);
endif;

if ($parts[0] !== ExchangeConfig::ROUTE) :
$this->_respondError(
404,
ExchangeConfig::ERROR_MESSAGES['INVALID_ROUTE']
);
endif;

if (!is_numeric($parts[self::PARAM_AMOUNT])
|| (float) $parts[self::PARAM_AMOUNT] <= 0
) :
$this->_respondError(
400,
ExchangeConfig::ERROR_MESSAGES['INVALID_AMOUNT']
);
endif;

if (!is_numeric($parts[self::PARAM_RATE])
|| (float) $parts[self::PARAM_RATE] <= 0
) :
$this->_respondError(
400,
ExchangeConfig::ERROR_MESSAGES['INVALID_RATE']
);
endif;

$from = trim($parts[self::PARAM_FROM]);
$to = trim($parts[self::PARAM_TO]);

if (!in_array($from, ExchangeConfig::PERMITTED_CURRENCIES, true)
|| !in_array($to, ExchangeConfig::PERMITTED_CURRENCIES, true)
) :
$this->_respondError(
400,
ExchangeConfig::ERROR_MESSAGES['INVALID_CURRENCY']
);
endif;

$amount = (float) trim($parts[self::PARAM_AMOUNT]);
$rate = (float) trim($parts[self::PARAM_RATE]);
$result = $amount * $rate;

$this->_respondSuccess(
[
'valorConvertido' => round($result, 2),
'simboloMoeda' => ExchangeConfig::SIGN_SYMBOL[$to],
]
);
}

/**
* Analisa a URL da requisição
* e retorna os parâmetros.
*
* @param string $requestUri URL da requisição
*
* @return array
*/
private function _parsePath(string $requestUri): array
{
$path = parse_url($requestUri, PHP_URL_PATH);

if (!is_string($path)) :
return [];
endif;

return explode('/', trim($path, '/'));
}

/**
* Função que envia a resposta de erro.
*
* @param int $statusCode Código de status da resposta
* @param string $message Mensagem de erro
*
* @return void
*/
private function _respondError(int $statusCode, string $message): void
{
http_response_code($statusCode);
echo json_encode(['erro' => $message]);
exit;
}

/**
* Função que envia a resposta de sucesso.
*
* @param array $payload Payload da resposta
*
* @return void
*/
private function _respondSuccess(array $payload): void
{
echo json_encode($payload);
}
}
8 changes: 7 additions & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
*
* @category Challenge
* @package Back-end
* @author Seu Nome <seu-email@seu-provedor.com>
* @author Kayo Almondes Tusthler <kayotusthler@gmail.com>
* @license http://opensource.org/licenses/MIT MIT
* @link https://github.com/apiki/back-end-challenge
*/
declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use App\Exchange\ExchangeHandler;

header('Content-Type: application/json');

$handler = new ExchangeHandler();
$handler->handle($_SERVER['REQUEST_URI']);
Loading