Skip to content

Commit

Permalink
Fixed the recommendations risks fetching and recommendations patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Mar 23, 2024
1 parent cc59653 commit c2fd490
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,8 @@
InputValidator\Object\DuplicateObjectDataInputValidator::class => ReflectionBasedAbstractFactory::class,
InputValidator\Recommendation\PostRecommendationDataInputValidator::class =>
ReflectionBasedAbstractFactory::class,
InputValidator\Recommendation\PatchRecommendationDataInputValidator::class =>
ReflectionBasedAbstractFactory::class,
InputValidator\RecommendationSet\PostRecommendationSetDataInputValidator::class =>
ReflectionBasedAbstractFactory::class,
InputValidator\RecommendationRisk\ValidateRecommendationRiskDataInputValidator::class =>
Expand Down
8 changes: 5 additions & 3 deletions src/Controller/ApiAnrRecommendationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Monarc\FrontOffice\InputFormatter\Recommendation\GetRecommendationsInputFormatter;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Service\AnrRecommendationService;
use Monarc\FrontOffice\Validator\InputValidator\Recommendation\PatchRecommendationDataInputValidator;
use Monarc\FrontOffice\Validator\InputValidator\Recommendation\PostRecommendationDataInputValidator;

class ApiAnrRecommendationsController extends AbstractRestfulControllerRequestHandler
Expand All @@ -21,7 +22,8 @@ class ApiAnrRecommendationsController extends AbstractRestfulControllerRequestHa
public function __construct(
private AnrRecommendationService $anrRecommendationService,
private GetRecommendationsInputFormatter $getRecommendationsInputFormatter,
private PostRecommendationDataInputValidator $postRecommendationDataInputValidator
private PostRecommendationDataInputValidator $postRecommendationDataInputValidator,
private PatchRecommendationDataInputValidator $patchRecommendationDataInputValidator
) {}

Check failure on line 27 in src/Controller/ApiAnrRecommendationsController.php

View workflow job for this annotation

GitHub Actions / build

Closing brace must be on a line by itself

public function getList()
Expand Down Expand Up @@ -70,11 +72,11 @@ public function create($data)
*/
public function patch($id, $data)
{
$this->validatePostParams($this->postRecommendationDataInputValidator, $data);
$this->validatePostParams($this->patchRecommendationDataInputValidator, $data);
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$this->anrRecommendationService->patch($anr, $id, $this->postRecommendationDataInputValidator->getValidData());
$this->anrRecommendationService->patch($anr, $id, $this->patchRecommendationDataInputValidator->getValidData());

return $this->getSuccessfulJsonResponse();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Service/AnrRecommendationRiskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function getList(FormattedInputParams $formattedInputParams): array
/** @var Entity\RecommendationRisk[] $recommendationRisks */
$recommendationRisks = $this->recommendationRiskTable->findByParams($formattedInputParams);

$hasRecommendationFilter = $formattedInputParams->hasFilterFor('recommendation');
$recommendationRisksData = [];
$globalObjectsUuids = [];
foreach ($recommendationRisks as $recommendationRisk) {
if ($recommendationRisk->getGlobalObject() !== null
if ($hasRecommendationFilter
&& $recommendationRisk->getGlobalObject() !== null
&& isset($globalObjectsUuids[$recommendationRisk->getGlobalObject()->getUuid()])
) {
continue;
Expand All @@ -57,7 +59,7 @@ public function getList(FormattedInputParams $formattedInputParams): array
$formattedInputParams->hasFilterFor('recommendation')
);

if ($recommendationRisk->getGlobalObject() !== null) {
if ($hasRecommendationFilter && $recommendationRisk->getGlobalObject() !== null) {
$globalObjectsUuids[$recommendationRisk->getGlobalObject()->getUuid()] = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @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\Validator\InputValidator\Recommendation;

use Laminas\Filter\DateTimeFormatter;
use Laminas\Filter\StringTrim;
use Laminas\Filter\ToInt;
use Laminas\Validator\InArray;
use Laminas\Validator\StringLength;
use Monarc\Core\Service\Interfaces\PositionUpdatableServiceInterface;
use Monarc\Core\Validator\InputValidator\AbstractInputValidator;
use Monarc\FrontOffice\Entity\Recommendation;

class PatchRecommendationDataInputValidator extends AbstractInputValidator
{
protected function getRules(): array
{
return [
[
'name' => 'recommendationSet',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'code',
'required' => false,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 1,
'max' => 100,
]
],
],
],
[
'name' => 'description',
'required' => false,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [],
],
[
'name' => 'comment',
'required' => false,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [],
],
[
'name' => 'duedate',
'required' => false,
'filters' => [
['name' => DateTimeFormatter::class],
],
'validators' => [],
],
[
'name' => 'importance',
'required' => false,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => InArray::class,
'options' => [
'haystack' => array_keys(Recommendation::getImportances()),
]
],
],
],
[
'name' => 'implicitPosition',
'required' => false,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => InArray::class,
'options' => [
'haystack' => [
PositionUpdatableServiceInterface::IMPLICIT_POSITION_START,
PositionUpdatableServiceInterface::IMPLICIT_POSITION_END,
PositionUpdatableServiceInterface::IMPLICIT_POSITION_AFTER,
],
],
],
],
],
[
'name' => 'previous',
'required' => false,
'filters' => [],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 36,
'max' => 36,
]
],
],
],
[
'name' => 'responsable',
'required' => false,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 1,
'max' => 255,
]
],
],
],
[
'name' => 'status',
'required' => false,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => InArray::class,
'options' => [
'haystack' => [0, 1],
],
],
],
],
];
}
}

0 comments on commit c2fd490

Please sign in to comment.