Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
30 / 45 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| InventoryFieldsApiController | |
65.91% |
29 / 44 |
|
50.00% |
3 / 6 |
19.70 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| list | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| update | |
50.00% |
5 / 10 |
|
0.00% |
0 / 1 |
2.50 | |||
| delete | |
44.44% |
4 / 9 |
|
0.00% |
0 / 1 |
2.69 | |||
| parseJsonBody | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
11.10 | |||
| 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\Core\Engine\Presentation\Api; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Engine\Domain\Repository\InventoryFieldRepositoryInterface; |
| 12 | use App\Core\Engine\Application\Service\InventoryApplicationService; |
| 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 | |
| 18 | /** |
| 19 | * Inventory Fields Management API Controller. |
| 20 | */ |
| 21 | final readonly class InventoryFieldsApiController |
| 22 | { |
| 23 | use ApiResponseTrait; |
| 24 | |
| 25 | public function __construct( |
| 26 | private InventoryFieldRepositoryInterface $inventoryFieldRepo, |
| 27 | private InventoryApplicationService $inventoryService, |
| 28 | private Psr17Factory $psr17 |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Lists inventory fields for a module. |
| 34 | */ |
| 35 | public function list(int $moduleId): ResponseInterface |
| 36 | { |
| 37 | $fields = $this->inventoryFieldRepo->findByModule($moduleId); |
| 38 | return $this->buildJsonResponse($this->psr17, [ |
| 39 | 'success' => true, |
| 40 | 'data' => $fields, |
| 41 | ]); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Adds a new inventory field to a module. |
| 46 | */ |
| 47 | public function create(ServerRequestInterface $request, int $moduleId): ResponseInterface |
| 48 | { |
| 49 | $data = $this->parseJsonBody($request); |
| 50 | |
| 51 | try { |
| 52 | $id = $this->inventoryService->addInventoryField($moduleId, $data); |
| 53 | return $this->buildJsonResponse($this->psr17, [ |
| 54 | 'success' => true, |
| 55 | 'data' => ['id' => $id] |
| 56 | ], 201); |
| 57 | } catch (\Throwable $e) { |
| 58 | return $this->buildJsonResponse($this->psr17, [ |
| 59 | 'success' => false, |
| 60 | 'error' => $e->getMessage() |
| 61 | ], 400); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Updates an inventory field. |
| 67 | */ |
| 68 | public function update(ServerRequestInterface $request, int $fieldId): ResponseInterface |
| 69 | { |
| 70 | $data = $this->parseJsonBody($request); |
| 71 | |
| 72 | try { |
| 73 | $this->inventoryService->updateInventoryField($fieldId, $data); |
| 74 | return $this->buildJsonResponse($this->psr17, [ |
| 75 | 'success' => true, |
| 76 | ]); |
| 77 | } catch (\Throwable $e) { |
| 78 | return $this->buildJsonResponse($this->psr17, [ |
| 79 | 'success' => false, |
| 80 | 'error' => $e->getMessage(), |
| 81 | ], 400); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Deletes an inventory field. |
| 87 | */ |
| 88 | public function delete(int $fieldId): ResponseInterface |
| 89 | { |
| 90 | try { |
| 91 | $this->inventoryService->deleteInventoryField($fieldId); |
| 92 | return $this->buildJsonResponse($this->psr17, [ |
| 93 | 'success' => true, |
| 94 | ]); |
| 95 | } catch (\Throwable $e) { |
| 96 | return $this->buildJsonResponse($this->psr17, [ |
| 97 | 'success' => false, |
| 98 | 'error' => $e->getMessage(), |
| 99 | ], 400); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private function parseJsonBody(ServerRequestInterface $request): array |
| 104 | { |
| 105 | $parsed = $request->getParsedBody(); |
| 106 | if (is_array($parsed) && !empty($parsed)) { |
| 107 | return $parsed; |
| 108 | } |
| 109 | |
| 110 | $raw = (string) $request->getBody(); |
| 111 | if ($raw === '') { |
| 112 | return []; |
| 113 | } |
| 114 | |
| 115 | $decoded = json_decode($raw, true); |
| 116 | return is_array($decoded) ? $decoded : []; |
| 117 | } |
| 118 | } |