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
50 changes: 50 additions & 0 deletions src/Controller/ExchangeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* PHP version 8.1
*
* @category Controller
* @package CurrencyExchange
* @author Gustavo de Almeida Oliveira <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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 <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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);
}
}
59 changes: 59 additions & 0 deletions src/Service/ExchangeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* PHP version 8.1
*
* @category Service
* @package CurrencyExchange
* @author Gustavo de Almeida Oliveira <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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 <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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,
];
}
}
44 changes: 44 additions & 0 deletions src/Util/CurrencySymbol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* PHP version 8.1
*
* @category Util
* @package CurrencyExchange
* @author Gustavo de Almeida Oliveira <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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 <almdgusta@gmail.com>
* @license MIT <https://opensource.org/licenses/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] ?? '';
}
}
46 changes: 41 additions & 5 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <seu-email@seu-provedor.com>
* @author Gustavo de Almeida Oliveira <almdgusta@gmail.com>
* @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']);
}
Loading