Skip to content

Commit

Permalink
Added the inheritance of the AbstractRestfulControllerRequestHandler …
Browse files Browse the repository at this point in the history
…to AsbstractController, moved the scales cache to the helper.
  • Loading branch information
ruslanbaidan committed Feb 11, 2024
1 parent 304c950 commit 1688e62
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
29 changes: 15 additions & 14 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

namespace Monarc\Core\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 Monarc\Core\Service\AbstractServiceFactory;
use Laminas\Mvc\Controller\AbstractRestfulController;
use Laminas\View\Model\JsonModel;

/**
* Abstract Controller used on every REST API controllers
* @package Monarc\Core\Controller
*/
abstract class AbstractController extends AbstractRestfulController
abstract class AbstractController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;

/**
* The service used by the controller.
* @var \Monarc\Core\Service\AbstractService
Expand Down Expand Up @@ -85,10 +87,10 @@ public function getList()
}
}

return new JsonModel(array(
return $this->getPreparedJsonResponse([
'count' => $service->getFilteredCount($filter),
$this->name => $entities
));
]);
}

/**
Expand All @@ -104,7 +106,7 @@ public function get($id)
$this->formatDependencies($entity, $this->dependencies);
}

return new JsonModel($entity);
return $this->getPreparedJsonResponse($entity);
}

/**
Expand All @@ -124,8 +126,7 @@ public function create($data)
$id = $this->getService()->create($new_data);
array_push($created_objects, $id);
}
return new JsonModel([
'status' => 'ok',
return $this->getSuccessfulJsonResponse([
'id' => count($created_objects)==1 ? $created_objects[0]: $created_objects,
]);
}
Expand All @@ -138,9 +139,9 @@ public function create($data)
public function delete($id)
{
if($this->getService()->delete($id)){
return new JsonModel(array('status' => 'ok'));
return $this->getSuccessfulJsonResponse();
}else{
return new JsonModel(array('status' => 'ko')); // Todo: peux être retourner un message d'erreur
return $this->getPreparedJsonResponse(['status' => 'ko']); // Todo: peux être retourner un message d'erreur
}
}

Expand All @@ -152,9 +153,9 @@ public function delete($id)
public function deleteList($data)
{
if($this->getService()->deleteList($data)){
return new JsonModel(array('status' => 'ok'));
return $this->getSuccessfulJsonResponse();
}else{
return new JsonModel(array('status' => 'ko')); // Todo: peux être retourner un message d'erreur
return $this->getPreparedJsonResponse(['status' => 'ko']); // Todo: peux être retourner un message d'erreur
}
}

Expand All @@ -168,7 +169,7 @@ public function update($id, $data)
{
$this->getService()->update($id, $data);

return new JsonModel(array('status' => 'ok'));
return $this->getSuccessfulJsonResponse();
}

/**
Expand All @@ -182,7 +183,7 @@ public function patch($id, $data)
{
$this->getService()->patch($id, $data);

return new JsonModel(array('status' => 'ok'));
return $this->getSuccessfulJsonResponse();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Monarc\Core\Controller\Handler;

use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
use Laminas\Mvc\Controller\AbstractRestfulController;
use Laminas\Mvc\MvcEvent;
use Laminas\Router\RouteMatch;
Expand Down
25 changes: 23 additions & 2 deletions src/Service/Helper/ScalesCacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace Monarc\Core\Service\Helper;

use Monarc\Core\Model\Entity\AnrSuperClass;
use Monarc\Core\Model\Entity\OperationalRiskScaleSuperClass;
use Monarc\Core\Model\Entity\ScaleImpactTypeSuperClass;
use Monarc\Core\Model\Entity\ScaleSuperClass;
use Monarc\Core\Table\OperationalRiskScaleTable;
use Monarc\Core\Table\ScaleImpactTypeTable;
use Monarc\Core\Table\ScaleTable;

Expand All @@ -19,8 +21,13 @@ class ScalesCacheHelper

private array $cachedScaleImpactTypes = [];

public function __construct(private ScaleTable $scaleTable, private ScaleImpactTypeTable $scaleImpactTypeTable)
{
private array $cachedOperationalRiskScales = [];

public function __construct(
private ScaleTable $scaleTable,
private ScaleImpactTypeTable $scaleImpactTypeTable,
private OperationalRiskScaleTable $operationalRiskScaleTable
) {
}

public function getCachedScaleByType(AnrSuperClass $anr, int $scaleType): ScaleSuperClass
Expand Down Expand Up @@ -70,4 +77,18 @@ public function getCachedScaleImpactTypes(AnrSuperClass $anr): array

return $this->cachedScaleImpactTypes;
}

public function getCachedLikelihoodScale(AnrSuperClass $anr): OperationalRiskScaleSuperClass
{
$typeLikelihood = OperationalRiskScaleSuperClass::TYPE_LIKELIHOOD;
if (!isset($this->cachedOperationalRiskScales[$typeLikelihood])) {
$this->cachedOperationalRiskScales[$typeLikelihood] = $this->operationalRiskScaleTable->findByAnrAndType(
$anr,
$typeLikelihood
);
}

/* There is only one scale of the TYPE_LIKELIHOOD. */
return current($this->cachedOperationalRiskScales[$typeLikelihood]);
}
}
5 changes: 3 additions & 2 deletions src/Service/InstanceRiskOpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Monarc\Core\Service;

use Monarc\Core\Model\Entity;
use Monarc\Core\Service\Helper\ScalesCacheHelper;
use Monarc\Core\Service\Traits\OperationalRiskScaleVerificationTrait;
use Monarc\Core\Table;

Expand All @@ -24,7 +25,7 @@ public function __construct(
private Table\TranslationTable $translationTable,
private Table\OperationalRiskScaleTypeTable $operationalRiskScaleTypeTable,
private ConfigService $configService,
private OperationalRiskScaleService $operationalRiskScaleService,
private ScalesCacheHelper $scalesCacheHelper,
ConnectedUserService $connectedUserService
) {
$this->connectedUser = $connectedUserService->getConnectedUser();
Expand Down Expand Up @@ -164,7 +165,7 @@ public function update(Entity\Anr $anr, int $id, array $data): Entity\InstanceRi
/** @var Entity\InstanceRiskOp $operationalInstanceRisk */
$operationalInstanceRisk = $this->instanceRiskOpTable->findByIdAndAnr($id, $anr);

$likelihoodScale = $this->operationalRiskScaleService->getFromCacheOrFindLikelihoodScale($anr);
$likelihoodScale = $this->scalesCacheHelper->getCachedLikelihoodScale($anr);
if (isset($data['kindOfMeasure'])) {
$operationalInstanceRisk->setKindOfMeasure((int)$data['kindOfMeasure']);
}
Expand Down
16 changes: 0 additions & 16 deletions src/Service/OperationalRiskScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class OperationalRiskScaleService
{
private Entity\UserSuperClass $connectedUser;

private array $operationalRiskScales = [];

public function __construct(
private Table\OperationalRiskScaleTable $operationalRiskScaleTable,
private Table\OperationalRiskScaleTypeTable $operationalRiskScaleTypeTable,
Expand Down Expand Up @@ -361,20 +359,6 @@ public function updateMinMaxForOperationalRiskProbability(Entity\Anr $anr, $data
}
}

public function getFromCacheOrFindLikelihoodScale(Entity\Anr $anr): Entity\OperationalRiskScale
{
$typeLikelihood = Entity\OperationalRiskScaleSuperClass::TYPE_LIKELIHOOD;
if (!isset($this->operationalRiskScales[$typeLikelihood])) {
$this->operationalRiskScales[$typeLikelihood] = $this->operationalRiskScaleTable->findByAnrAndType(
$anr,
$typeLikelihood
);
}

/* There is only one scale of the TYPE_LIKELIHOOD. */
return current($this->operationalRiskScales[$typeLikelihood]);
}

protected function getCreatedOperationalRiskScaleTypeObject(
Entity\AnrSuperClass $anr,
Entity\OperationalRiskScale $operationalRiskScale
Expand Down

0 comments on commit 1688e62

Please sign in to comment.