diff --git a/src/Controller/ExchangeController.php b/src/Controller/ExchangeController.php new file mode 100644 index 00000000..b21024e2 --- /dev/null +++ b/src/Controller/ExchangeController.php @@ -0,0 +1,50 @@ + + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +namespace Controller; + +require_once __DIR__ . '/../Service/ExchangeService.php'; + +use Service\ExchangeService; + +/** + * Controlador responsável por receber requisições e chamar o serviço de conversão. + * + * @category Controller + * @package CurrencyExchange + * @author Gustavo de Almeida Oliveira + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +class ExchangeController +{ + /** + * Converte um valor de uma moeda para outra. + * + * @param float $amount Valor a ser convertido + * @param string $from Moeda de origem (ex: BRL) + * @param string $to Moeda de destino (ex: USD) + * @param float $rate Taxa de conversão + * + * @return array Retorno com valor convertido e símbolo + */ + public function convert( + float $amount, + string $from, + string $to, + float $rate + ): array { + $service = new ExchangeService(); + return $service->convertCurrency($amount, $from, $to, $rate); + } +} diff --git a/src/Service/ExchangeService.php b/src/Service/ExchangeService.php new file mode 100644 index 00000000..c9c312c0 --- /dev/null +++ b/src/Service/ExchangeService.php @@ -0,0 +1,59 @@ + + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +namespace Service; + +require_once __DIR__ . '/../Util/CurrencySymbol.php'; + +use Util\CurrencySymbol; + +/** + * Serviço responsável pela lógica de conversão de moedas. + * + * @category Service + * @package CurrencyExchange + * @author Gustavo de Almeida Oliveira + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +class ExchangeService +{ + /** + * Converte o valor da moeda para outra utilizando a taxa de câmbio fornecida. + * + * @param float $amount Valor a ser convertido. + * @param string $from Moeda de origem. + * @param string $to Moeda de destino. + * @param float $rate Taxa de câmbio. + * + * @return array Resultado da conversão com valor convertido e símbolo da moeda. + */ + public function convertCurrency( + float $amount, + string $from, + string $to, + float $rate + ): array { + if ($rate <= 0) { + throw new \InvalidArgumentException('A taxa de câmbio deve ser maior que zero.'); + } + $converted = $amount * $rate; + + $symbol = CurrencySymbol::getSymbol($to); + + return [ + 'valorConvertido' => $converted, + 'simboloMoeda' => $symbol, + ]; + } +} diff --git a/src/Util/CurrencySymbol.php b/src/Util/CurrencySymbol.php new file mode 100644 index 00000000..acd38265 --- /dev/null +++ b/src/Util/CurrencySymbol.php @@ -0,0 +1,44 @@ + + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +namespace Util; + +/** + * Utilitário para obter o símbolo de uma moeda. + * + * @category Util + * @package CurrencyExchange + * @author Gustavo de Almeida Oliveira + * @license MIT + * @link https://github.com/almdgustadev/back-end-challenge + */ + +class CurrencySymbol +{ + /** + * Retorna o símbolo da moeda correspondente ao código fornecido. + * + * @param string $currency Código da moeda. + * + * @return string Símbolo da moeda ou uma string vazia se não encontrado. + */ + public static function getSymbol(string $currency): string + { + $symbols = [ + 'USD' => '$', + 'BRL' => 'R$', + 'EUR' => '€', + ]; + + return $symbols[$currency] ?? ''; + } +} diff --git a/src/index.php b/src/index.php index 92841bc8..ba559de6 100644 --- a/src/index.php +++ b/src/index.php @@ -2,17 +2,53 @@ /** * Back-end Challenge. * - * PHP version 7.4 + * PHP version 8.1 * * Este será o arquivo chamado na execução dos testes automátizados. * * @category Challenge * @package Back-end - * @author Seu Nome + * @author Gustavo de Almeida Oliveira * @license http://opensource.org/licenses/MIT MIT - * @link https://github.com/apiki/back-end-challenge + * @link https://github.com/almdgustadev/back-end-challenge + * + * Ponto de entrada para a aplicação + * Roteia a requisição para o controlador de conversão de moedas */ -declare(strict_types=1); -require __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/Controller/ExchangeController.php'; + +use Controller\ExchangeController; + +$uri = $_SERVER['REQUEST_URI']; +$method = $_SERVER['REQUEST_METHOD']; + +$pattern = '#^/exchange/(-?[\d.]+)/([A-Z]{3})/([A-Z]{3})/(-?[\d.]+)$#'; + +try { + if ($method === 'GET' && preg_match($pattern, $uri, $matches)) { + $amount = (float) $matches[1]; + $from = $matches[2]; + $to = $matches[3]; + $rate = (float) $matches[4]; + + $controller = new ExchangeController(); + $response = $controller->convert($amount, $from, $to, $rate); + http_response_code(200); + header('Content-Type: application/json'); + echo json_encode($response); + } else { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Rota não encontrada ou inválida']); + } +} catch (\InvalidArgumentException $e) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => $e->getMessage()]); +} catch (\Throwable $e) { + http_response_code(500); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Erro interno no servidor']); +} \ No newline at end of file