Skip to content

Commit 678cc16

Browse files
committed
Updated the referential, measure and soa categories related controllers.
1 parent d149555 commit 678cc16

8 files changed

+197
-147
lines changed

config/module.config.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@
298298
'id' => '[a-f0-9-]*',
299299
],
300300
'defaults' => [
301-
'controller' => PipeSpec::class,
302-
'middleware' => new PipeSpec(Controller\ApiReferentialsController::class),
301+
'controller' => Controller\ApiReferentialsController::class,
303302
],
304303
],
305304
],
@@ -312,8 +311,7 @@
312311
'id' => '[a-f0-9-]*',
313312
],
314313
'defaults' => [
315-
'controller' => PipeSpec::class,
316-
'middleware' => new PipeSpec(Controller\ApiMeasuresController::class),
314+
'controller' => Controller\ApiMeasuresController::class,
317315
],
318316
],
319317
],
@@ -352,8 +350,7 @@
352350
'id' => '[0-9]+',
353351
],
354352
'defaults' => [
355-
'controller' => PipeSpec::class,
356-
'middleware' => new PipeSpec(Controller\ApiObjectsCategoriesController::class),
353+
'controller' => Controller\ApiObjectsCategoriesController::class,
357354
],
358355
],
359356
],
@@ -446,8 +443,7 @@
446443
'id' => '[0-9]+',
447444
],
448445
'defaults' => [
449-
'controller' => PipeSpec::class,
450-
'middleware' => new PipeSpec(Controller\ApiSoaCategoryController::class),
446+
'controller' => Controller\ApiSoaCategoryController::class,
451447
],
452448
],
453449
],

src/Controller/ApiAssetsController.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,14 @@ public function create($data)
5959
$isBatchData = $this->isBatchData($data);
6060
$this->validatePostParams($this->postAssetDataInputValidator, $data, $isBatchData);
6161

62-
$assetsUuids = [];
63-
$validatedData = $isBatchData
64-
? $this->postAssetDataInputValidator->getValidDataSets()
65-
: [$this->postAssetDataInputValidator->getValidData()];
66-
foreach ($validatedData as $validatedDataRow) {
67-
$assetsUuids[] = $this->assetService->create($validatedDataRow)->getUuid();
62+
if ($isBatchData) {
63+
return $this->getSuccessfulJsonResponse([
64+
'id' => $this->assetService->createList($this->postAssetDataInputValidator->getValidDataSets()),
65+
]);
6866
}
6967

7068
return $this->getSuccessfulJsonResponse([
71-
'id' => \count($assetsUuids) === 1 ? current($assetsUuids) : $assetsUuids,
69+
'id' => $this->assetService->create($this->postAssetDataInputValidator->getValidData())->getUuid(),
7270
]);
7371
}
7472

src/Controller/ApiMeasureMeasureController.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
use Laminas\Mvc\Controller\AbstractRestfulController;
1111
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
1212
use Monarc\Core\Service\MeasureMeasureService;
13+
use Monarc\Core\Validator\InputValidator\MeasureMeasure\PostMeasureMeasureDataInputValidator;
1314

1415
class ApiMeasureMeasureController extends AbstractRestfulController
1516
{
1617
use ControllerRequestResponseHandlerTrait;
1718

18-
public function __construct(private MeasureMeasureService $measureMeasureService)
19-
{
19+
public function __construct(
20+
private MeasureMeasureService $measureMeasureService,
21+
private PostMeasureMeasureDataInputValidator $postMeasureMeasureDataInputValidator
22+
) {
2023
}
2124

2225
public function getList()
@@ -32,10 +35,13 @@ public function getList()
3235

3336
public function create($data)
3437
{
38+
$isBatchData = $this->isBatchData($data);
39+
$this->validatePostParams($this->postMeasureMeasureDataInputValidator, $data, $isBatchData);
40+
3541
if ($this->isBatchData($data)) {
36-
$this->measureMeasureService->createList($data);
42+
$this->measureMeasureService->createList($this->postMeasureMeasureDataInputValidator->getValidDataSets());
3743
} else {
38-
$this->measureMeasureService->create($data);
44+
$this->measureMeasureService->create($this->postMeasureMeasureDataInputValidator->getValidData());
3945
}
4046

4147
return $this->getSuccessfulJsonResponse();
@@ -45,6 +51,11 @@ public function deleteList($data)
4551
{
4652
$masterMeasureUuid = $this->params()->fromQuery('masterMeasureUuid');
4753
$linkedMeasureUuid = $this->params()->fromQuery('linkedMeasureUuid');
54+
$this->validatePostParams(
55+
$this->postMeasureMeasureDataInputValidator,
56+
compact('masterMeasureUuid', 'linkedMeasureUuid')
57+
);
58+
4859
$this->measureMeasureService->delete($masterMeasureUuid, $linkedMeasureUuid);
4960

5061
return $this->getSuccessfulJsonResponse();
+55-47
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,92 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* @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
55
* @license MONARC is licensed under GNU Affero General Public License version 3
66
*/
77

88
namespace Monarc\BackOffice\Controller;
99

10-
use Monarc\Core\Controller\AbstractController;
10+
use Laminas\Mvc\Controller\AbstractRestfulController;
1111
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
12+
use Monarc\Core\InputFormatter\Measure\GetMeasuresInputFormatter;
1213
use Monarc\Core\Service\MeasureService;
14+
use Monarc\Core\Validator\InputValidator\Measure\PostMeasureDataInputValidator;
15+
use Monarc\Core\Validator\InputValidator\Measure\UpdateMeasureDataInputValidator;
1316

14-
/**
15-
* TODO: extend AbstractRestfulController and remove AbstractController.
16-
*/
17-
class ApiMeasuresController extends AbstractController
17+
class ApiMeasuresController extends AbstractRestfulController
1818
{
1919
use ControllerRequestResponseHandlerTrait;
2020

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+
}
2328

24-
public function __construct(MeasureService $measureService)
29+
public function getList()
2530
{
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+
]);
2737
}
2838

2939
/**
30-
* @inheritdoc
40+
* @param string $id
3141
*/
32-
public function getList()
42+
public function get($id)
3343
{
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+
}
5246

53-
$service = $this->getService();
47+
public function create($data)
48+
{
49+
$isBatchData = $this->isBatchData($data);
50+
$this->validatePostParams($this->postMeasureDataInputValidator, $data, $isBatchData);
5451

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+
]);
6056
}
6157

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(),
6560
]);
6661
}
6762

63+
/**
64+
* @param string $id
65+
* @param array $data
66+
*/
6867
public function update($id, $data)
6968
{
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);
7182

72-
return parent::update($id,$data);
83+
return $this->getSuccessfulJsonResponse();
7384
}
7485

7586
public function deleteList($data)
7687
{
77-
$new_data = [];
78-
foreach ($data as $uuid) {
79-
$new_data[] = ['uuid' => $uuid];
80-
}
88+
$this->measureService->deleteList($data);
8189

82-
return parent::deleteList($new_data);
90+
return $this->getSuccessfulJsonResponse();
8391
}
8492
}
+55-28
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,80 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* @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
55
* @license MONARC is licensed under GNU Affero General Public License version 3
66
*/
77

88
namespace Monarc\BackOffice\Controller;
99

10-
use Monarc\Core\Controller\AbstractController;
10+
use Laminas\Mvc\Controller\AbstractRestfulController;
1111
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
12+
use Monarc\Core\InputFormatter\Referential\GetReferentialInputFormatter;
1213
use Monarc\Core\Service\ReferentialService;
14+
use Monarc\Core\Validator\InputValidator\Referential\PostReferentialDataInputValidator;
1315

14-
/**
15-
* TODO: extend AbstractRestfulController and remove AbstractController.
16-
*/
17-
class ApiReferentialsController extends AbstractController
16+
class ApiReferentialsController extends AbstractRestfulController
1817
{
1918
use ControllerRequestResponseHandlerTrait;
2019

21-
protected $name = 'referentials';
22-
protected $dependencies = ['measures'];
20+
public function __construct(
21+
private ReferentialService $referentialService,
22+
private GetReferentialInputFormatter $getReferentialInputFormatter,
23+
private PostReferentialDataInputValidator $postReferentialDataInputValidator,
24+
) {
25+
}
2326

24-
public function __construct(ReferentialService $referentialService)
27+
public function getList()
2528
{
26-
parent::__construct($referentialService);
29+
$formatterParams = $this->getFormattedInputParams($this->getReferentialInputFormatter);
30+
31+
return $this->getPreparedJsonResponse([
32+
'referentials' => $this->referentialService->getList($formatterParams),
33+
]);
2734
}
2835

2936
/**
30-
* @inheritdoc
37+
* @param string $id
3138
*/
32-
public function getList()
39+
public function get($id)
3340
{
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-
39-
$service = $this->getService();
41+
return $this->getPreparedJsonResponse($this->referentialService->getReferentialData($id));
42+
}
4043

41-
$entities = $service->getList($page, $limit, $order, $filter);
42-
if (count($this->dependencies)) {
43-
foreach ($entities as $key => $entity) {
44-
$this->formatDependencies($entities[$key], $this->dependencies);
45-
}
46-
}
44+
/**
45+
* @param array $data
46+
*/
47+
public function create($data)
48+
{
49+
$this->validatePostParams($this->postReferentialDataInputValidator, $data);
4750

48-
return $this->getPreparedJsonResponse([
49-
'count' => $service->getFilteredCount($filter),
50-
$this->name => $entities,
51+
return $this->getSuccessfulJsonResponse([
52+
'id' => $this->referentialService->create(
53+
$this->postReferentialDataInputValidator->getValidData()
54+
)->getUuid(),
5155
]);
5256
}
57+
58+
/**
59+
* @param string $id
60+
* @param array $data
61+
*/
62+
public function update($id, $data)
63+
{
64+
$this->validatePostParams($this->postReferentialDataInputValidator, $data);
65+
66+
$this->referentialService->update($id, $this->postReferentialDataInputValidator->getValidData());
67+
68+
return $this->getSuccessfulJsonResponse();
69+
}
70+
71+
/**
72+
* @param string $id
73+
*/
74+
public function delete($id)
75+
{
76+
$this->referentialService->delete($id);
77+
78+
return $this->getSuccessfulJsonResponse();
79+
}
5380
}

0 commit comments

Comments
 (0)