|
1 |
| -<?php |
| 1 | +<?php declare(strict_types=1); |
2 | 2 | /**
|
3 | 3 | * @link https://github.com/monarc-project for the canonical source repository
|
4 |
| - * @copyright Copyright (c) 2016-2019 SMILE GIE Securitymadein.lu - Licensed under GNU Affero GPL v3 |
| 4 | + * @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 |
5 | 5 | * @license MONARC is licensed under GNU Affero General Public License version 3
|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | namespace Monarc\BackOffice\Controller;
|
9 | 9 |
|
10 |
| -use Monarc\Core\Controller\AbstractController; |
| 10 | +use Laminas\Mvc\Controller\AbstractRestfulController; |
11 | 11 | use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
|
| 12 | +use Monarc\Core\InputFormatter\Measure\GetMeasuresInputFormatter; |
12 | 13 | use Monarc\Core\Service\MeasureService;
|
| 14 | +use Monarc\Core\Validator\InputValidator\Measure\PostMeasureDataInputValidator; |
| 15 | +use Monarc\Core\Validator\InputValidator\Measure\UpdateMeasureDataInputValidator; |
13 | 16 |
|
14 |
| -/** |
15 |
| - * TODO: extend AbstractRestfulController and remove AbstractController. |
16 |
| - */ |
17 |
| -class ApiMeasuresController extends AbstractController |
| 17 | +class ApiMeasuresController extends AbstractRestfulController |
18 | 18 | {
|
19 | 19 | use ControllerRequestResponseHandlerTrait;
|
20 | 20 |
|
21 |
| - protected $name = 'measures'; |
22 |
| - protected $dependencies = ['category', 'referential', 'measuresLinked','rolfRisks']; |
| 21 | + public function __construct( |
| 22 | + private MeasureService $measureService, |
| 23 | + private GetMeasuresInputFormatter $getMeasuresInputFormatter, |
| 24 | + private PostMeasureDataInputValidator $postMeasureDataInputValidator, |
| 25 | + private UpdateMeasureDataInputValidator $updateMeasureDataInputValidator |
| 26 | + ) { |
| 27 | + } |
23 | 28 |
|
24 |
| - public function __construct(MeasureService $measureService) |
| 29 | + public function getList() |
25 | 30 | {
|
26 |
| - parent::__construct($measureService); |
| 31 | + $formattedParams = $this->getFormattedInputParams($this->getMeasuresInputFormatter); |
| 32 | + |
| 33 | + return $this->getPreparedJsonResponse([ |
| 34 | + 'count' => $this->measureService->getCount($formattedParams), |
| 35 | + 'measures' => $this->measureService->getList($formattedParams), |
| 36 | + ]); |
27 | 37 | }
|
28 | 38 |
|
29 | 39 | /**
|
30 |
| - * @inheritdoc |
| 40 | + * @param string $id |
31 | 41 | */
|
32 |
| - public function getList() |
| 42 | + public function get($id) |
33 | 43 | {
|
34 |
| - $page = $this->params()->fromQuery('page'); |
35 |
| - $limit = $this->params()->fromQuery('limit'); |
36 |
| - $order = $this->params()->fromQuery('order'); |
37 |
| - $filter = $this->params()->fromQuery('filter'); |
38 |
| - $status = $this->params()->fromQuery('status'); |
39 |
| - $referential = $this->params()->fromQuery('referential'); |
40 |
| - $category = $this->params()->fromQuery('category'); |
41 |
| - $filterAnd = []; |
42 |
| - if (is_null($status)) { |
43 |
| - $status = 1; |
44 |
| - } |
45 |
| - $filterAnd = ($status == "all") ? null : ['status' => (int) $status] ; |
46 |
| - if ($referential) { |
47 |
| - $filterAnd['referential'] = (array)$referential; |
48 |
| - } |
49 |
| - if ($category) { |
50 |
| - $filterAnd['category'] = (int)$category; |
51 |
| - } |
| 44 | + return $this->getPreparedJsonResponse($this->measureService->getMeasureData($id)); |
| 45 | + } |
52 | 46 |
|
53 |
| - $service = $this->getService(); |
| 47 | + public function create($data) |
| 48 | + { |
| 49 | + $isBatchData = $this->isBatchData($data); |
| 50 | + $this->validatePostParams($this->postMeasureDataInputValidator, $data, $isBatchData); |
54 | 51 |
|
55 |
| - $entities = $service->getList($page, $limit, $order, $filter, $filterAnd); |
56 |
| - if (count($this->dependencies)) { |
57 |
| - foreach ($entities as $key => $entity) { |
58 |
| - $this->formatDependencies($entities[$key], $this->dependencies); |
59 |
| - } |
| 52 | + if ($this->isBatchData($data)) { |
| 53 | + return $this->getSuccessfulJsonResponse([ |
| 54 | + 'id' => $this->measureService->createList($this->postMeasureDataInputValidator->getValidDataSets()), |
| 55 | + ]); |
60 | 56 | }
|
61 | 57 |
|
62 |
| - return $this->getPreparedJsonResponse([ |
63 |
| - 'count' => $service->getFilteredCount($filter, $filterAnd), |
64 |
| - $this->name => $entities |
| 58 | + return $this->getSuccessfulJsonResponse([ |
| 59 | + 'id' => $this->measureService->create($this->postMeasureDataInputValidator->getValidData())->getUuid(), |
65 | 60 | ]);
|
66 | 61 | }
|
67 | 62 |
|
| 63 | + /** |
| 64 | + * @param string $id |
| 65 | + * @param array $data |
| 66 | + */ |
68 | 67 | public function update($id, $data)
|
69 | 68 | {
|
70 |
| - $data ['referential'] = $data['referential']['uuid']; //all the objects is send but we just need the uuid |
| 69 | + $this->validatePostParams($this->updateMeasureDataInputValidator, $data); |
| 70 | + |
| 71 | + $this->measureService->update($id, $this->updateMeasureDataInputValidator->getValidData()); |
| 72 | + |
| 73 | + return $this->getSuccessfulJsonResponse(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string $id |
| 78 | + */ |
| 79 | + public function delete($id) |
| 80 | + { |
| 81 | + $this->measureService->delete($id); |
71 | 82 |
|
72 |
| - return parent::update($id,$data); |
| 83 | + return $this->getSuccessfulJsonResponse(); |
73 | 84 | }
|
74 | 85 |
|
75 | 86 | public function deleteList($data)
|
76 | 87 | {
|
77 |
| - $new_data = []; |
78 |
| - foreach ($data as $uuid) { |
79 |
| - $new_data[] = ['uuid' => $uuid]; |
80 |
| - } |
| 88 | + $this->measureService->deleteList($data); |
81 | 89 |
|
82 |
| - return parent::deleteList($new_data); |
| 90 | + return $this->getSuccessfulJsonResponse(); |
83 | 91 | }
|
84 | 92 | }
|
0 commit comments