Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TaxCalculateApiController | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| calculate | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| simulate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Tax\Presentation\Api; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Modules\Tax\Application\DTO\CalculateDocumentRequest; |
| 12 | use App\Modules\Tax\Application\Service\TaxApplicationService; |
| 13 | use App\Shared\Infrastructure\Http\ApiResponseTrait; |
| 14 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | use Psr\Http\Message\ServerRequestInterface; |
| 17 | use Throwable; |
| 18 | |
| 19 | /** |
| 20 | * REST API controller for document tax calculations and simulations. |
| 21 | */ |
| 22 | final readonly class TaxCalculateApiController |
| 23 | { |
| 24 | use ApiResponseTrait; |
| 25 | |
| 26 | /** |
| 27 | * TaxCalculateApiController constructor. |
| 28 | */ |
| 29 | public function __construct( |
| 30 | private TaxApplicationService $taxService, |
| 31 | private Psr17Factory $psr17Factory, |
| 32 | ) { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Calculate and return document tax breakdown (POST /api/v1/tax/calculate). |
| 37 | */ |
| 38 | public function calculate(ServerRequestInterface $request): ResponseInterface |
| 39 | { |
| 40 | $body = $this->parseJsonBody($request); |
| 41 | |
| 42 | try { |
| 43 | $dto = CalculateDocumentRequest::fromArray($body); |
| 44 | $summary = $this->taxService->calculateDocument($dto); |
| 45 | |
| 46 | return $this->jsonResponse( |
| 47 | $this->psr17Factory, |
| 48 | ['success' => true, 'data' => $summary->toArray()] |
| 49 | ); |
| 50 | } catch (Throwable $e) { |
| 51 | return $this->jsonResponse( |
| 52 | $this->psr17Factory, |
| 53 | ['success' => false, 'error' => $e->getMessage()], |
| 54 | 400 |
| 55 | ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Simulate tax outcome for diagnostic/preview purposes (POST /api/v1/tax/simulate). |
| 61 | */ |
| 62 | public function simulate(ServerRequestInterface $request): ResponseInterface |
| 63 | { |
| 64 | return $this->calculate($request); |
| 65 | } |
| 66 | } |