Skip to content

Commit

Permalink
Finished the objects, categories and composition functionality update…
Browse files Browse the repository at this point in the history
…, add the anr field to the entities where it was dropeed from core.
  • Loading branch information
ruslanbaidan committed Feb 20, 2024
1 parent e15b8da commit 8b63132
Show file tree
Hide file tree
Showing 23 changed files with 671 additions and 357 deletions.
17 changes: 4 additions & 13 deletions src/Controller/ApiAnrAssetsController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

Expand All @@ -18,20 +18,11 @@ class ApiAnrAssetsController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;

private GetAssetsInputFormatter $getAssetsInputFormatter;

private PostAssetDataInputValidator $postAssetDataInputValidator;

private AnrAssetService $anrAssetService;

public function __construct(
GetAssetsInputFormatter $getAssetsInputFormatter,
PostAssetDataInputValidator $postAssetDataInputValidator,
AnrAssetService $anrAssetService
private GetAssetsInputFormatter $getAssetsInputFormatter,
private PostAssetDataInputValidator $postAssetDataInputValidator,
private AnrAssetService $anrAssetService
) {
$this->getAssetsInputFormatter = $getAssetsInputFormatter;
$this->postAssetDataInputValidator = $postAssetDataInputValidator;
$this->anrAssetService = $anrAssetService;
}

public function getList()
Expand Down
147 changes: 52 additions & 95 deletions src/Controller/ApiAnrObjectsCategoriesController.php
Original file line number Diff line number Diff line change
@@ -1,127 +1,84 @@
<?php
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2020 SMILE GIE Securitymadein.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Controller;

use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\Core\InputFormatter\ObjectCategory\ObjectCategoriesInputFormatter;
use Monarc\Core\Validator\InputValidator\ObjectCategory\PostObjectCategoryDataInputValidator;
use Monarc\FrontOffice\Model\Entity\Anr;
use Monarc\FrontOffice\Service\AnrObjectCategoryService;
use Laminas\View\Model\JsonModel;

/**
* Api ANR Objects Categories Controller
*
* Class ApiAnrObjectsCategoriesController
* @package Monarc\FrontOffice\Controller
*/
class ApiAnrObjectsCategoriesController extends ApiAnrAbstractController
class ApiAnrObjectsCategoriesController extends AbstractRestfulControllerRequestHandler
{
protected $name = 'categories';
protected $dependencies = ['parent', 'root', 'anr'];
use ControllerRequestResponseHandlerTrait;

public function __construct(
private AnrObjectCategoryService $anrObjectCategoryService,
private ObjectCategoriesInputFormatter $objectCategoriesInputFormatter,
private PostObjectCategoryDataInputValidator $postObjectCategoryDataInputValidator
) {
}

/**
* @inheritdoc
*/
public function getList()
{
$page = $this->params()->fromQuery('page');
$limit = $this->params()->fromQuery('limit');
$order = $this->params()->fromQuery('order');
if (empty($order)) {
$order = "position";
}
$filter = $this->params()->fromQuery('filter');
$lock = $this->params()->fromQuery('lock') == "false" ? false : true;

/** @var AnrObjectCategoryService $service */
$service = $this->getService();

$anrId = (int)$this->params()->fromRoute('anrid');
if (empty($anrId)) {
throw new \Monarc\Core\Exception\Exception('Anr id missing', 412);
}
$filterAnd = ['anr' => $anrId];
$catid = (int)$this->params()->fromQuery('catid');
$parentId = (int)$this->params()->fromQuery('parentId');
if (!empty($catid)) {
$filterAnd['id'] = [
'op' => 'NOT IN',
'value' => [$catid],
];
if ($parentId > 0) {
$filterAnd['id']['value'][] = $parentId;
$filterAnd['parent'] = $parentId;
} else {
$filterAnd['parent'] = null;
}
} elseif ($parentId > 0) {
$filterAnd['parent'] = $parentId;
} elseif (!$lock) {
$filterAnd['parent'] = null;
}

$objectCategories = $service->getListSpecific($page, $limit, $order, $filter, $filterAnd);

$fields = ['id', 'label1', 'label2', 'label3', 'label4', 'position'];

if ($parentId > 0 && $lock) {
$recursiveArray = $this->getCleanFields($objectCategories, $fields);
} else {
$recursiveArray = $this->recursiveArray($objectCategories, null, 0, $fields);
}

return new JsonModel([
'count' => $this->getService()->getFilteredCount($filter, $filterAnd),
$this->name => $recursiveArray
$formattedParams = $this->getFormattedInputParams($this->objectCategoriesInputFormatter);
$this->objectCategoriesInputFormatter->prepareCategoryFilter();

return $this->getPreparedJsonResponse([
'count' => $this->anrObjectCategoryService->getCount($formattedParams),
'categories' => $this->anrObjectCategoryService->getList($formattedParams),
]);
}

public function get($id)
{
return $this->getPreparedJsonResponse($this->anrObjectCategoryService->getObjectCategoryData((int)$id));
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

return $this->getPreparedJsonResponse($this->anrObjectCategoryService->getObjectCategoryData($anr, (int)$id));
}

/**
* Helper method that cleans up an entity by only keeping the fields that are listed in the $fields parameter
* @param array $items The items to filter
* @param array $fields The fields to keep
* @return array The filtered items
* @param array $data
*/
public function getCleanFields($items, $fields)
public function create($data)
{
$output = [];
foreach ($items as $item) {
$item_output = [];

foreach ($item as $key => $value) {
if (in_array($key, $fields)) {
$item_output[$key] = $value;
}
}

$output[] = $item_output;
}
return $output;
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$category = $this->anrObjectCategoryService->create($anr, $data);

return $this->getSuccessfulJsonResponse(['categ' => $category->getId()]);
}

/**
* @inheritdoc
* @param array $data
*/
public function create($data)
public function update($id, $data)
{
$anrId = (int)$this->params()->fromRoute('anrid');
if (empty($anrId)) {
throw new \Monarc\Core\Exception\Exception('Anr id missing', 412);
}
$data['anr'] = $anrId;
$this->validatePostParams($this->postObjectCategoryDataInputValidator, $data);

$obj = $this->getService()->create($data);
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

return new JsonModel([
'status' => 'ok',
'categ' => $obj,
]);
$this->anrObjectCategoryService->update($anr, (int)$id, $data);

return $this->getSuccessfulJsonResponse();
}

public function delete($id)
{
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$this->anrObjectCategoryService->delete($anr, (int)$id);

return $this->getSuccessfulJsonResponse();
}
}
11 changes: 3 additions & 8 deletions src/Controller/ApiAnrObjectsDuplicationController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

Expand All @@ -17,15 +17,10 @@ class ApiAnrObjectsDuplicationController extends AbstractRestfulControllerReques
{
use ControllerRequestResponseHandlerTrait;

private AnrObjectService $anrObjectService;
private DuplicateObjectDataInputValidator $duplicateObjectDataInputValidator;

public function __construct(
AnrObjectService $anrObjectService,
DuplicateObjectDataInputValidator $duplicateObjectDataInputValidator
private AnrObjectService $anrObjectService,
private DuplicateObjectDataInputValidator $duplicateObjectDataInputValidator
) {
$this->anrObjectService = $anrObjectService;
$this->duplicateObjectDataInputValidator = $duplicateObjectDataInputValidator;
}

public function create($data)
Expand Down
90 changes: 34 additions & 56 deletions src/Controller/ApiAnrObjectsObjectsController.php
Original file line number Diff line number Diff line change
@@ -1,96 +1,74 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2022 SMILE GIE Securitymadein.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Controller;

use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\Core\Exception\Exception;
use Monarc\Core\Model\Entity\AbstractEntity;
use Laminas\View\Model\JsonModel;
use Monarc\Core\Validator\InputValidator\ObjectComposition\CreateDataInputValidator;
use Monarc\Core\Validator\InputValidator\ObjectComposition\MovePositionDataInputValidator;
use Monarc\FrontOffice\Model\Entity\Anr;
use Monarc\FrontOffice\Service\AnrObjectObjectService;

class ApiAnrObjectsObjectsController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;

private AnrObjectObjectService $anrObjectObjectService;

private MovePositionDataInputValidator $movePositionDataInputValidator;

public function __construct(
AnrObjectObjectService $anrObjectObjectService,
MovePositionDataInputValidator $movePositionDataInputValidator
private AnrObjectObjectService $anrObjectObjectService,
private CreateDataInputValidator $createDataInputValidator,
private MovePositionDataInputValidator $movePositionDataInputValidator
) {
$this->anrObjectObjectService = $anrObjectObjectService;
$this->movePositionDataInputValidator = $movePositionDataInputValidator;
}

public function update($id, $data)
/**
* @param array $data
*/
public function create($data)
{
$this->validatePostParams($this->movePositionDataInputValidator, $data);
$this->validatePostParams($this->createDataInputValidator, $data);

$this->anrObjectObjectService->shiftPositionInComposition(
$id,
$this->movePositionDataInputValidator->getValidData()
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$objectComposition = $this->anrObjectObjectService->create(
$anr,
$this->createDataInputValidator->getValidData()
);

return $this->getSuccessfulJsonResponse();
return $this->getSuccessfulJsonResponse(['id' => $objectComposition->getId()]);
}

/**
* @inheritdoc
* @param array $data
*/
public function create($data)
public function update($id, $data)
{
$anrId = (int)$this->params()->fromRoute('anrid');
if (empty($anrId)) {
throw new Exception('Anr id missing', 412);
}
$data['anr'] = $anrId;
$data['child'] = ['anr' => $anrId, 'uuid' => $data['child']];
$data['parent'] = ['anr' => $anrId, 'uuid' => $data['parent']];
$this->validatePostParams($this->movePositionDataInputValidator, $data);

$id = $this->getService()->create($data, true, AbstractEntity::FRONT_OFFICE);
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

return new JsonModel([
'status' => 'ok',
'id' => $id,
]);
}
$this->anrObjectObjectService->shiftPositionInComposition(
$anr,
$id,
$this->movePositionDataInputValidator->getValidData()
);

// TODO: only the 3 methods are used in the controller. (delete was added to get rid of the abstract controller)
public function delete($id)
{
return parent::delete($id); // TODO: Change the autogenerated stub
return $this->getSuccessfulJsonResponse();
}

/**
* @inheritdoc
*/
public function get($id)
public function delete($id)
{
return $this->methodNotAllowed();
}
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

/**
* @inheritdoc
*/
public function getList()
{
return $this->methodNotAllowed();
}
$this->anrObjectObjectService->delete($anr, (int)$id);

/**
* @inheritdoc
*/
public function patch($id, $data)
{
return $this->methodNotAllowed();
return $this->getSuccessfulJsonResponse();
}
}
Loading

0 comments on commit 8b63132

Please sign in to comment.