Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.33% |
59 / 60 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CurrenciesWebController | |
98.31% |
58 / 59 |
|
75.00% |
3 / 4 |
15 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
5 | |||
| add | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
5 | |||
| delete | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Currencies\Presentation\Web; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Instance\Application\Service\InstanceContextManagerInterface; |
| 12 | use App\Modules\Currencies\Domain\Repository\CurrencyRepositoryInterface; |
| 13 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 14 | use Psr\Http\Message\ResponseInterface; |
| 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | use Twig\Environment as TwigEnvironment; |
| 17 | |
| 18 | /** |
| 19 | * Web Controller for Currencies management in App Admin Tools. |
| 20 | */ |
| 21 | final readonly class CurrenciesWebController |
| 22 | { |
| 23 | private const string HTML_CONTENT_TYPE = 'text/html; charset=utf-8'; |
| 24 | |
| 25 | public function __construct( |
| 26 | private CurrencyRepositoryInterface $currencyRepository, |
| 27 | private TwigEnvironment $twig, |
| 28 | private Psr17Factory $psr17Factory, |
| 29 | private ?InstanceContextManagerInterface $instanceManager = null, |
| 30 | private string $appProfile = 'admin' |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Renders Currencies management view (GET /settings/currencies). |
| 36 | */ |
| 37 | public function index(ServerRequestInterface $request): ResponseInterface |
| 38 | { |
| 39 | $request->getMethod(); |
| 40 | $currencies = $this->currencyRepository->findAll(); |
| 41 | $rateHistory = $this->currencyRepository->getAllRateHistory(50); |
| 42 | $baseCurrency = null; |
| 43 | $latestNbpTable = null; |
| 44 | $latestEffectiveDate = null; |
| 45 | |
| 46 | foreach ($currencies as $curr) { |
| 47 | if ($curr->isBase()) { |
| 48 | $baseCurrency = $curr; |
| 49 | } |
| 50 | if ($curr->getNbpTableNo() !== null && $latestNbpTable === null) { |
| 51 | $latestNbpTable = $curr->getNbpTableNo(); |
| 52 | $latestEffectiveDate = $curr->getEffectiveDate(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $activeInstance = $this->instanceManager?->getActiveInstance(); |
| 57 | |
| 58 | $html = $this->twig->render('currencies/index.twig', [ |
| 59 | 'currencies' => $currencies, |
| 60 | 'rate_history' => $rateHistory, |
| 61 | 'base_currency' => $baseCurrency, |
| 62 | 'nbp_table_no' => $latestNbpTable ?? 'NBP Tabela A', |
| 63 | 'effective_date' => $latestEffectiveDate ?? date('Y-m-d'), |
| 64 | 'page_title' => 'Waluty i kursy NBP', |
| 65 | 'active_instance' => $activeInstance, |
| 66 | 'app_profile' => $this->appProfile, |
| 67 | ]); |
| 68 | |
| 69 | $response = $this->psr17Factory->createResponse(200) |
| 70 | ->withHeader('Content-Type', self::HTML_CONTENT_TYPE); |
| 71 | $response->getBody()->write($html); |
| 72 | |
| 73 | return $response; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Handles adding a new currency from web form (POST /settings/currencies/add). |
| 78 | */ |
| 79 | public function add(ServerRequestInterface $request): ResponseInterface |
| 80 | { |
| 81 | $body = (array) $request->getParsedBody(); |
| 82 | $code = strtoupper(trim((string) ($body['code'] ?? ''))); |
| 83 | $name = trim((string) ($body['name'] ?? '')); |
| 84 | $symbol = trim((string) ($body['symbol'] ?? $code)); |
| 85 | $rate = (float) ($body['exchange_rate'] ?? 1.0); |
| 86 | $isActive = !empty($body['is_active']); |
| 87 | |
| 88 | if ( |
| 89 | preg_match('/^[A-Z]{3}$/', $code) |
| 90 | && $name !== '' |
| 91 | && $this->currencyRepository->findByCode($code) === null |
| 92 | ) { |
| 93 | $currency = new \App\Modules\Currencies\Domain\Model\Currency( |
| 94 | id: null, |
| 95 | code: $code, |
| 96 | name: $name, |
| 97 | symbol: $symbol !== '' ? $symbol : $code, |
| 98 | exchangeRate: max(0.0001, $rate), |
| 99 | nbpTableNo: null, |
| 100 | effectiveDate: date('Y-m-d'), |
| 101 | isBase: false, |
| 102 | isActive: $isActive, |
| 103 | sortOrder: 50 |
| 104 | ); |
| 105 | $this->currencyRepository->save($currency); |
| 106 | } |
| 107 | |
| 108 | return $this->psr17Factory->createResponse(302)->withHeader('Location', '/settings/currencies'); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Handles deleting a currency from web form (POST /settings/currencies/delete). |
| 113 | */ |
| 114 | public function delete(ServerRequestInterface $request): ResponseInterface |
| 115 | { |
| 116 | $body = (array) $request->getParsedBody(); |
| 117 | $code = strtoupper(trim((string) ($body['code'] ?? ''))); |
| 118 | $id = (int) ($body['id'] ?? 0); |
| 119 | |
| 120 | if ($id > 0) { |
| 121 | $this->currencyRepository->delete($id); |
| 122 | } elseif ($code !== '' && $code !== 'PLN') { |
| 123 | $this->currencyRepository->deleteByCode($code); |
| 124 | } |
| 125 | |
| 126 | return $this->psr17Factory->createResponse(302)->withHeader('Location', '/settings/currencies'); |
| 127 | } |
| 128 | } |