diff --git a/src/Export/Service/AnrExportService.php b/src/Export/Service/AnrExportService.php index d675f54a..c3c48542 100644 --- a/src/Export/Service/AnrExportService.php +++ b/src/Export/Service/AnrExportService.php @@ -399,7 +399,7 @@ private function prepareSoasData(Entity\Anr $anr): array $result = []; /** @var Entity\Soa $soa */ foreach ($this->soaTable->findByAnr($anr) as $soa) { - $result = [ + $result[] = [ 'remarks' => $soa->getRemarks(), 'evidences' => $soa->getEvidences(), 'actions' => $soa->getActions(), diff --git a/src/Export/Service/Traits/InformationInstanceRiskExportTrait.php b/src/Export/Service/Traits/InformationInstanceRiskExportTrait.php index 1b81b660..ac3e1436 100644 --- a/src/Export/Service/Traits/InformationInstanceRiskExportTrait.php +++ b/src/Export/Service/Traits/InformationInstanceRiskExportTrait.php @@ -39,7 +39,8 @@ private function prepareInformationInstanceRiskData( $recommendation = $recommendationRisk->getRecommendation(); $recommendationsData[] = array_merge( $this->prepareRecommendationData($recommendation), - ['commentAfter' => $recommendationRisk->getCommentAfter()]); + ['commentAfter' => $recommendationRisk->getCommentAfter()] + ); } } diff --git a/src/Import/Helper/ImportCacheHelper.php b/src/Import/Helper/ImportCacheHelper.php index 0af1a822..baa4ebf0 100644 --- a/src/Import/Helper/ImportCacheHelper.php +++ b/src/Import/Helper/ImportCacheHelper.php @@ -41,4 +41,16 @@ public function getItemFromArrayCache(string $cacheKey, $itemKey = null) return $this->arrayCache[$cacheKey][$itemKey] ?? null; } + + /** Sets a single array cache value. */ + public function setArrayCacheValue(string $cacheKey, $value): void + { + $this->arrayCache[$cacheKey] = $value; + } + + /** Returns the array cache value or null if not set. */ + public function getValueFromArrayCache(string $cacheKey) + { + return $this->arrayCache[$cacheKey] ?? null; + } } diff --git a/src/Import/Processor/AnrInstanceMetadataFieldImportProcessor.php b/src/Import/Processor/AnrInstanceMetadataFieldImportProcessor.php index 6dc4f6f6..c4ffba72 100644 --- a/src/Import/Processor/AnrInstanceMetadataFieldImportProcessor.php +++ b/src/Import/Processor/AnrInstanceMetadataFieldImportProcessor.php @@ -57,7 +57,7 @@ private function getAnrInstanceMetadataFieldsFromCache( string $label ): ?Entity\AnrInstanceMetadataField { if (!$this->importCacheHelper->isCacheKeySet('is_anr_instance_metadata_fields_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_anr_instance_metadata_fields_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_anr_instance_metadata_fields_cache_loaded', true); /** @var Entity\AnrInstanceMetadataField $anrInstanceMetadataField */ foreach ($this->anrInstanceMetadataFieldTable->findByAnr($anr) as $anrInstanceMetadataField) { $this->importCacheHelper->addItemToArrayCache( diff --git a/src/Import/Processor/AnrMethodStepImportProcessor.php b/src/Import/Processor/AnrMethodStepImportProcessor.php index 3565395b..485aa305 100644 --- a/src/Import/Processor/AnrMethodStepImportProcessor.php +++ b/src/Import/Processor/AnrMethodStepImportProcessor.php @@ -33,7 +33,7 @@ public function processAnrMethodStepsData(Entity\Anr $anr, array $methodStepsDat { if (!empty($methodStepsData['questions'])) { /* The only place where the data is flushed, so has to be called first. */ - $this->processQuestionsData($anr, $methodStepsData['questions']); + $this->processQuestionsData($anr, $methodStepsData); } /* Set method steps checkboxes. */ @@ -116,13 +116,13 @@ public function processInterviewsData(Entity\Anr $anr, array $interviewsData): v } } - private function processQuestionsData(Entity\Anr $anr, $questionsData): void + private function processQuestionsData(Entity\Anr $anr, array $methodStepsData): void { foreach ($this->questionTable->findByAnr($anr) as $question) { $this->questionTable->deleteEntity($question, false); } - foreach ($questionsData as $position => $questionData) { + foreach ($methodStepsData['questions'] as $position => $questionData) { /* In the new data structure there is only "label" field set. */ if (isset($questionData['label'])) { $questionData['label' . $anr->getLanguage()] = $questionData['label']; @@ -142,7 +142,7 @@ private function processQuestionsData(Entity\Anr $anr, $questionsData): void $choicesOldIdsToNewObjects = []; /* Support the old structure format, prior v2.13.1 */ $questionChoicesData = $methodStepsData['questionChoice'] ?? $questionData['questionChoices']; - foreach ($questionChoicesData as $questionChoiceData) { + foreach ($questionChoicesData as $questionChoiceId => $questionChoiceData) { if (!isset($questionChoiceData['question']) || $questionChoiceData['question'] === $questionData['id'] ) { @@ -153,7 +153,7 @@ private function processQuestionsData(Entity\Anr $anr, $questionsData): void ->setLabels($questionChoiceData)->setPosition($questionChoiceData['position']) ->setCreator($this->connectedUser->getEmail()); $this->questionChoiceTable->saveEntity($questionChoice, false); - $choicesOldIdsToNewObjects[$questionChoiceData['id']] = $questionChoice; + $choicesOldIdsToNewObjects[$questionChoiceData['id'] ?? $questionChoiceId] = $questionChoice; } } $response = trim($question->getResponse(), '[]'); diff --git a/src/Import/Processor/AssetImportProcessor.php b/src/Import/Processor/AssetImportProcessor.php index 6287133a..5ea7027b 100644 --- a/src/Import/Processor/AssetImportProcessor.php +++ b/src/Import/Processor/AssetImportProcessor.php @@ -63,7 +63,7 @@ public function processAssetData(Entity\Anr $anr, array $assetData): Entity\Asse public function getAssetFromCache(Entity\Anr $anr, string $uuid): ?Entity\Asset { if (!$this->importCacheHelper->isCacheKeySet('is_assets_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_assets_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_assets_cache_loaded', true); /** @var Entity\Asset $asset */ foreach ($this->assetTable->findByAnr($anr) as $asset) { $this->importCacheHelper->addItemToArrayCache('assets_by_uuid', $asset, $asset->getUuid()); diff --git a/src/Import/Processor/InformationRiskImportProcessor.php b/src/Import/Processor/InformationRiskImportProcessor.php index 9fa94cc5..22a9fe38 100644 --- a/src/Import/Processor/InformationRiskImportProcessor.php +++ b/src/Import/Processor/InformationRiskImportProcessor.php @@ -87,7 +87,7 @@ public function processInformationRiskData(Entity\Anr $anr, array $informationRi private function getInformationRiskFromCache(Entity\Anr $anr, string $uuid): ?Entity\Amv { if (!$this->importCacheHelper->isCacheKeySet('is_amvs_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_amvs_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_amvs_cache_loaded', true); /** @var Entity\Amv $amv */ foreach ($this->amvTable->findByAnr($anr) as $amv) { $this->importCacheHelper->addItemToArrayCache('amvs_by_uuid', $amv, $amv->getUuid()); diff --git a/src/Import/Processor/InstanceConsequenceImportProcessor.php b/src/Import/Processor/InstanceConsequenceImportProcessor.php index df0931a6..bb0445e6 100644 --- a/src/Import/Processor/InstanceConsequenceImportProcessor.php +++ b/src/Import/Processor/InstanceConsequenceImportProcessor.php @@ -10,6 +10,7 @@ use Monarc\Core\Entity\ScaleSuperClass; use Monarc\FrontOffice\Entity; use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; +use Monarc\FrontOffice\Import\Service\InstanceImportService; use Monarc\FrontOffice\Import\Traits\EvaluationConverterTrait; use Monarc\FrontOffice\Service\AnrInstanceConsequenceService; @@ -27,13 +28,11 @@ public function __construct( public function processInstanceConsequencesData( Entity\Instance $instance, array $instanceConsequencesData, - bool $withEval, ?Entity\Instance $siblingInstance, - bool $isImportTypeAnr ): void { - if ($withEval) { + if ($this->importCacheHelper->getValueFromArrayCache('with_eval')) { foreach ($instanceConsequencesData as $instanceConsequenceData) { - $this->processInstanceConsequenceData($instance, $instanceConsequenceData, $isImportTypeAnr); + $this->processInstanceConsequenceData($instance, $instanceConsequenceData); } } elseif ($siblingInstance === null) { $this->createInstanceConsequencesBasedOnExistingImpactTypes($instance); @@ -44,8 +43,7 @@ public function processInstanceConsequencesData( public function processInstanceConsequenceData( Entity\Instance $instance, - array $instanceConsequenceData, - bool $isImportTypeAnr + array $instanceConsequenceData ): ?Entity\InstanceConsequence { /** @var Entity\Anr $anr */ $anr = $instance->getAnr(); @@ -56,7 +54,9 @@ public function processInstanceConsequenceData( } /* For the instances import the values have to be converted to local scales. */ - if (!$isImportTypeAnr) { + if ($this->importCacheHelper + ->getValueFromArrayCache('import_type') === InstanceImportService::IMPORT_TYPE_INSTANCE + ) { $this->convertInstanceConsequencesEvaluations($instanceConsequenceData); } @@ -105,26 +105,14 @@ private function convertInstanceConsequencesEvaluations(array &$instanceData): v ->getItemFromArrayCache('current_scales_data_by_type')[ScaleSuperClass::TYPE_IMPACT]; $externalScaleRange = $this->importCacheHelper ->getItemFromArrayCache('external_scales_data_by_type')[ScaleSuperClass::TYPE_IMPACT]; - $instanceData['confidentiality'] = $this->convertValueWithinNewScalesRange( - $instanceData['confidentiality'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); - $instanceData['integrity'] = $this->convertValueWithinNewScalesRange( - $instanceData['integrity'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); - $instanceData['availability'] = $this->convertValueWithinNewScalesRange( - $instanceData['availability'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); + foreach (['confidentiality', 'integrity', 'availability'] as $propertyName) { + $instanceData[$propertyName] = $this->convertValueWithinNewScalesRange( + $instanceData[$propertyName], + $externalScaleRange['min'], + $externalScaleRange['max'], + $currentScaleRange['min'], + $currentScaleRange['max'], + ); + } } } diff --git a/src/Import/Processor/InstanceImportProcessor.php b/src/Import/Processor/InstanceImportProcessor.php index aef95c64..c828e19f 100644 --- a/src/Import/Processor/InstanceImportProcessor.php +++ b/src/Import/Processor/InstanceImportProcessor.php @@ -12,6 +12,7 @@ use Monarc\Core\Service\ConnectedUserService; use Monarc\FrontOffice\Entity; use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; +use Monarc\FrontOffice\Import\Service\InstanceImportService; use Monarc\FrontOffice\Import\Traits\EvaluationConverterTrait; use Monarc\FrontOffice\Service\AnrInstanceService; use Monarc\FrontOffice\Table; @@ -60,7 +61,7 @@ public function processInstancesData( foreach ($instancesData as $instanceData) { $instanceData['position'] += $maxPositionInsideOfParentInstance++; $instanceData['setOnlyExactPosition'] = true; - $instances[] = $this->processInstanceData($anr, $instanceData, $parentInstance, $importMode, $withEval); + $instances[] = $this->processInstanceData($anr, $instanceData, $parentInstance, $importMode); } return $instances; @@ -71,9 +72,7 @@ public function processInstanceData( Entity\Anr $anr, array $instanceData, ?Entity\Instance $parentInstance, - string $importMode, - bool $withEval, - bool $isImportTypeAnr = true + string $importMode ): Entity\Instance { $objectCategory = null; if (isset($instanceData['object']['category'])) { @@ -85,16 +84,13 @@ public function processInstanceData( $instanceData['parent'] = $parentInstance; /* For the instances import the values have to be converted to local scales. */ - if ($withEval && !$isImportTypeAnr) { + if ($this->importCacheHelper->getValueFromArrayCache('with_eval') && $this->importCacheHelper + ->getValueFromArrayCache('import_type') === InstanceImportService::IMPORT_TYPE_INSTANCE + ) { $this->convertInstanceEvaluations($instanceData); } $instance = $this->instanceService->createInstance($anr, $instanceData, $parentInstance === null, false); - $this->prepareAndProcessInstanceConsequencesData( - $instance, - $instanceData['instancesConsequences'], - $withEval, - $isImportTypeAnr - ); + $this->prepareAndProcessInstanceConsequencesData($instance, $instanceData['instancesConsequences']); $instance->updateImpactBasedOnConsequences(); /* In case if there is a parent instance, the scales impacts could be adjusted, if they are not set directly. */ if ($parentInstance !== null) { @@ -102,29 +98,19 @@ public function processInstanceData( } $siblingInstances = []; - if (!$withEval && $instance->getObject()->isScopeGlobal()) { + if (!$this->importCacheHelper->getValueFromArrayCache('with_eval') && $instance->getObject()->isScopeGlobal()) { $siblingInstances = $this->getGlobalObjectInstancesFromCache($anr, $instance->getObject()->getUuid()); } - $this->instanceRiskImportProcessor->processInstanceRisksData( - $instance, - $siblingInstances, - $instanceData['instanceRisks'], - $withEval, - $isImportTypeAnr - ); + $this->instanceRiskImportProcessor + ->processInstanceRisksData($instance, $siblingInstances, $instanceData['instanceRisks']); - $this->operationalInstanceRiskImportProcessor->processOperationalInstanceRisksData( - $anr, - $instance, - $instanceData['operationalInstanceRisks'], - $withEval, - $isImportTypeAnr - ); + $this->operationalInstanceRiskImportProcessor + ->processOperationalInstanceRisksData($anr, $instance, $instanceData['operationalInstanceRisks']); $this->processInstanceMetadata($anr, $instance, $instanceData['instanceMetadata']); if (!empty($instanceData['children'])) { - $this->processInstancesData($anr, $instanceData['children'], $instance, $importMode, $withEval); + $this->processInstancesData($anr, $instanceData['children'], $instance, $importMode); } return $instance; @@ -163,27 +149,20 @@ public function processInstanceMetadata( /** A wrapper method to help of processing the instance consequences data. */ public function prepareAndProcessInstanceConsequencesData( Entity\Instance $instance, - array $instanceConsequencesData, - bool $withEval, - bool $isImportTypeAnr + array $instanceConsequencesData ): void { /** @var Entity\Anr $anr */ $anr = $instance->getAnr(); /* When the importing data are without evaluation and the object is global the evaluations are taken from a sibling. */ $siblingInstance = null; - if (!$withEval && $instance->getObject()->isScopeGlobal()) { + if (!$this->importCacheHelper->getValueFromArrayCache('with_eval') && $instance->getObject()->isScopeGlobal()) { $siblingInstances = $this->getGlobalObjectInstancesFromCache($anr, $instance->getObject()->getUuid()); $siblingInstance = $siblingInstances[0] ?? null; } - $this->instanceConsequenceImportProcessor->processInstanceConsequencesData( - $instance, - $instanceConsequencesData, - $withEval, - $siblingInstance, - $isImportTypeAnr - ); + $this->instanceConsequenceImportProcessor + ->processInstanceConsequencesData($instance, $instanceConsequencesData, $siblingInstance); } /** @@ -245,7 +224,7 @@ private function updateInstanceMetadataFromSiblings(Entity\Anr $anr, Entity\Inst private function getGlobalObjectInstancesFromCache(Entity\Anr $anr, string $objectUuid): array { if (!$this->importCacheHelper->isCacheKeySet('is_global_instances_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_global_instances_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_global_instances_cache_loaded', true); foreach ($this->monarcObjectTable->findGlobalObjectsByAnr($anr) as $object) { $instances = []; foreach ($object->getInstances() as $instance) { @@ -268,26 +247,14 @@ private function convertInstanceEvaluations(array &$instanceData): void ->getItemFromArrayCache('current_scales_data_by_type')[ScaleSuperClass::TYPE_IMPACT]; $externalScaleRange = $this->importCacheHelper ->getItemFromArrayCache('external_scales_data_by_type')[ScaleSuperClass::TYPE_IMPACT]; - $instanceData['confidentiality'] = $this->convertValueWithinNewScalesRange( - $instanceData['confidentiality'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); - $instanceData['integrity'] = $this->convertValueWithinNewScalesRange( - $instanceData['integrity'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); - $instanceData['availability'] = $this->convertValueWithinNewScalesRange( - $instanceData['availability'], - $externalScaleRange['min'], - $externalScaleRange['max'], - $currentScaleRange['min'], - $currentScaleRange['max'], - ); + foreach (['confidentiality', 'integrity', 'availability'] as $propertyName) { + $instanceData[$propertyName] = $this->convertValueWithinNewScalesRange( + $instanceData[$propertyName], + $externalScaleRange['min'], + $externalScaleRange['max'], + $currentScaleRange['min'], + $currentScaleRange['max'], + ); + } } } diff --git a/src/Import/Processor/InstanceRiskImportProcessor.php b/src/Import/Processor/InstanceRiskImportProcessor.php index 99f307d2..85592581 100644 --- a/src/Import/Processor/InstanceRiskImportProcessor.php +++ b/src/Import/Processor/InstanceRiskImportProcessor.php @@ -10,6 +10,7 @@ use Monarc\Core\Entity\ScaleSuperClass; use Monarc\FrontOffice\Entity; use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; +use Monarc\FrontOffice\Import\Service\InstanceImportService; use Monarc\FrontOffice\Import\Traits\EvaluationConverterTrait; use Monarc\FrontOffice\Service\AnrInstanceRiskService; use Monarc\FrontOffice\Service\AnrRecommendationRiskService; @@ -36,26 +37,20 @@ public function __construct( public function processInstanceRisksData( Entity\Instance $instance, array $siblingInstances, - array $instanceRisksData, - bool $withEval, - bool $isImportTypeAnr + array $instanceRisksData ): void { /* Create new instance risks. */ foreach ($instanceRisksData as $instanceRiskData) { - $this->processInstanceRiskData($instance, $instanceRiskData, $withEval, $isImportTypeAnr); + $this->processInstanceRiskData($instance, $instanceRiskData); } if (!empty($siblingInstances)) { /* Match the created instance risks with sibling instances' ones. */ - $this->matchCreatedInstanceRisksWithSiblingInstances($instance, $siblingInstances, $withEval); + $this->matchCreatedInstanceRisksWithSiblingInstances($instance, $siblingInstances); } } - private function processInstanceRiskData( - Entity\Instance $instance, - array $instanceRiskData, - bool $withEval, - bool $isImportTypeAnr - ): Entity\InstanceRisk { + private function processInstanceRiskData(Entity\Instance $instance, array $instanceRiskData): Entity\InstanceRisk + { /** @var Entity\Anr $anr */ $anr = $instance->getAnr(); if (!empty($instanceRiskData['informationRisk'])) { @@ -86,9 +81,11 @@ private function processInstanceRiskData( ); } - if ($withEval) { + if ($this->importCacheHelper->getValueFromArrayCache('with_eval')) { /* For the instances import the values have to be converted to local scales. */ - if (!$isImportTypeAnr) { + if ($this->importCacheHelper + ->getValueFromArrayCache('import_type') === InstanceImportService::IMPORT_TYPE_INSTANCE + ) { $this->convertInstanceRiskEvaluations($instanceRiskData); } @@ -126,11 +123,11 @@ private function processInstanceRiskData( */ private function matchCreatedInstanceRisksWithSiblingInstances( Entity\Instance $instance, - array $siblingInstances, - bool $withEval + array $siblingInstances ): void { $createdRiskKeys = []; $siblingRiskKeys = []; + $withEval = $this->importCacheHelper->getValueFromArrayCache('with_eval'); foreach ($siblingInstances as $siblingInstance) { /** @var Entity\InstanceRisk $createdInstanceRisk */ foreach ($instance->getInstanceRisks() as $createdInstanceRisk) { diff --git a/src/Import/Processor/ObjectCategoryImportProcessor.php b/src/Import/Processor/ObjectCategoryImportProcessor.php index 21f19238..0fdeb52e 100644 --- a/src/Import/Processor/ObjectCategoryImportProcessor.php +++ b/src/Import/Processor/ObjectCategoryImportProcessor.php @@ -104,7 +104,7 @@ private function getObjectCategoryFromCacheByLabel( string $categoryAndItsParentLabels ): ?Entity\ObjectCategory { if (!$this->importCacheHelper->isCacheKeySet('is_object_categories_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_object_categories_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_object_categories_cache_loaded', true); $languageIndex = $anr->getLanguage(); /** @var Entity\ObjectCategory $objectCategory */ foreach ($this->objectCategoryTable->findByAnr($anr) as $objectCategory) { diff --git a/src/Import/Processor/ObjectImportProcessor.php b/src/Import/Processor/ObjectImportProcessor.php index c797b2a1..409e85eb 100644 --- a/src/Import/Processor/ObjectImportProcessor.php +++ b/src/Import/Processor/ObjectImportProcessor.php @@ -7,26 +7,30 @@ namespace Monarc\FrontOffice\Import\Processor; +use Monarc\Core\Entity\InstanceRiskSuperClass; use Monarc\Core\Entity\ObjectSuperClass; use Monarc\FrontOffice\Entity; use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; +use Monarc\FrontOffice\Import\Service\InstanceImportService; use Monarc\FrontOffice\Import\Service\ObjectImportService; -use Monarc\FrontOffice\Service\AnrObjectObjectService; -use Monarc\FrontOffice\Service\AnrObjectService; -use Monarc\FrontOffice\Table\MonarcObjectTable; -use Monarc\FrontOffice\Table\ObjectObjectTable; +use Monarc\FrontOffice\Service; +use Monarc\FrontOffice\Table; class ObjectImportProcessor { public function __construct( - private MonarcObjectTable $monarcObjectTable, - private ObjectObjectTable $objectObjectTable, + private Table\MonarcObjectTable $monarcObjectTable, + private Table\ObjectObjectTable $objectObjectTable, + private Table\InstanceRiskTable $instanceRiskTable, + private Table\AmvTable $amvTable, private ImportCacheHelper $importCacheHelper, - private AnrObjectService $anrObjectService, - private AnrObjectObjectService $anrObjectObjectService, + private Service\AnrObjectService $anrObjectService, + private Service\AnrObjectObjectService $anrObjectObjectService, + private Service\AnrInstanceRiskService $anrInstanceRiskService, private AssetImportProcessor $assetImportProcessor, private RolfTagImportProcessor $rolfTagImportProcessor, - private ObjectCategoryImportProcessor $objectCategoryImportProcessor + private ObjectCategoryImportProcessor $objectCategoryImportProcessor, + private InformationRiskImportProcessor $informationRiskImportProcessor ) { } @@ -75,15 +79,16 @@ public function processObjectData( } } + $isImportTypeObject = $this->importCacheHelper + ->getValueFromArrayCache('import_type') === InstanceImportService::IMPORT_TYPE_OBJECT; $currentObjectUuid = $objectData['uuid']; if ($object === null) { - if (empty($objectData['asset'])) { - $a = 1; - } + /* If IMPORT_TYPE_OBJECT then the process of informationRisks/amvs is done inside. */ $asset = $this->assetImportProcessor->processAssetData($anr, $objectData['asset']); $rolfTag = null; if (!empty($objectData['rolfTag'])) { + /* If IMPORT_TYPE_OBJECT then the process of $objectData['rolfTag']['rolfRisks'] is done inside. */ $rolfTag = $this->rolfTagImportProcessor->processRolfTagData($anr, $objectData['rolfTag']); } @@ -107,9 +112,9 @@ public function processObjectData( ); $this->importCacheHelper ->addItemToArrayCache('objects_names', $objectData[$nameFiledKey], $objectData[$nameFiledKey]); - } else { + } elseif ($isImportTypeObject) { /* If asset's amvs (information risks) are different, then update them. */ - if (isset($objectData['asset']['informationRisks'])) { + if (!empty($objectData['asset']['informationRisks'])) { $this->mergeAssetInformationRisks($object, $objectData['asset']['informationRisks']); } /* Validate if the RolfTag is the same or/and the linked to it operational risks are equal. */ @@ -120,26 +125,19 @@ public function processObjectData( /* Process objects links. */ foreach ($objectData['children'] as $positionIndex => $childObjectData) { - $objectCategory = $this->objectCategoryImportProcessor->processObjectCategoryData( - $anr, - $childObjectData['category'], - $importMode - ); + $objectCategory = $this->objectCategoryImportProcessor + ->processObjectCategoryData($anr, $childObjectData['category'], $importMode); $childObject = $this->processObjectData($anr, $objectCategory, $childObjectData, $importMode); - if (!$object->hasChild($childObject) && !$this->importCacheHelper->isItemInArrayCache( - 'objects_links_uuids', - $object->getUuid() . $childObject->getUuid() - )) { + $linksCacheKey = $object->getUuid() . $childObject->getUuid(); + if (!$object->hasChild($childObject) + && !$this->importCacheHelper->isItemInArrayCache('objects_links_uuids', $linksCacheKey) + ) { $this->anrObjectObjectService->createObjectLink($object, $childObject, [ 'position' => $positionIndex + 1, 'forcePositionUpdate' => true, ], false); - $this->importCacheHelper->addItemToArrayCache( - 'objects_links_uuids', - $object->getUuid() . $childObject->getUuid(), - $object->getUuid() . $childObject->getUuid() - ); + $this->importCacheHelper->addItemToArrayCache('objects_links_uuids', $linksCacheKey, $linksCacheKey); } } @@ -154,7 +152,7 @@ private function getObjectFromCacheByParams( ?int $categoryId ): ?Entity\MonarcObject { if (!$this->importCacheHelper->isCacheKeySet('is_objects_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_objects_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_objects_cache_loaded', true); $languageIndex = $anr->getLanguage(); /** @var Entity\MonarcObject $object */ foreach ($this->monarcObjectTable->findByAnr($anr) as $object) { @@ -194,28 +192,49 @@ private function prepareUniqueObjectName(string $objectName, int $index = 1): st return $objectName; } + /* Merges the amvs (information risks) of the existing object. */ private function mergeAssetInformationRisks(Entity\MonarcObject $object, array $informationRisksData): void { + $existingAmvs = []; + /** @var Entity\Amv $amv */ + foreach ($object->getAsset()->getAmvs() as $amv) { + $existingAmvs[$amv->getUuid()] = $amv; + } + $importingAmvsData = []; foreach ($informationRisksData as $informationRiskData) { - $asset = $object->getAsset(); - $informationRiskData['uuid']; - // TODO: !!! Instance risks are supposed to be processed in the InstanceRiskImportProcessor, so here we correct only amvs. !!! - // the same is for op risks. - // TODO: + figure out (based on AssetImportService) if the asset's data have to be reprocessed if informationRisks are set. - // TODO: 1. match the difference and update them - // -> when amv is added : add new instance risks - // -> when removed: turn existing instance risks to specific and drop the amv. + $importingAmvsData[$informationRiskData['uuid']] = $informationRiskData; + } + foreach (array_diff_key($importingAmvsData, $existingAmvs) as $newImportingAmvData) { + $amv = $this->informationRiskImportProcessor + ->processInformationRiskData($object->getAnr(), $newImportingAmvData); + /* Recreated instance risks if the object is instantiated. */ + foreach ($object->getInstances() as $instance) { + $this->anrInstanceRiskService->createInstanceRisk($instance, $amv, null, null, null, false); + } + } + /** @var Entity\Amv $existingAmvToRemove */ + foreach (array_diff_key($existingAmvs, $importingAmvsData) as $existingAmvToRemove) { + /* Recreated instance risks if the object is instantiated. */ + foreach ($existingAmvToRemove->getInstanceRisks() as $instanceRiskToSetSpecific) { + $instanceRiskToSetSpecific->setSpecific(InstanceRiskSuperClass::TYPE_SPECIFIC) + ->setAmv(null); + $this->instanceRiskTable->save($instanceRiskToSetSpecific, false); + } + $this->amvTable->remove($existingAmvToRemove, false); } } + /* Merges the rolfRisks (operational risks) of the existing object. */ private function mergeRolfTagOperationalRisks(Entity\MonarcObject $object, ?array $rolfTagData): void { - if (empty($rolfTagData) && $object->getRolfTag() !== null) { - // if ($object->getRolfTag()->getCode() !== $rolfTagData['code']) {} - // TODO: 1. if there was no rolfTag and we set, then also create the rolf risks if different and add/remove instance risks op. - // TODO: 2. was a rolf tag and now removed, then remove all the instance risks op have to be removed. - // TODO: 3. if a new rolf is changed, then all the op risks have to be updated. - // TODO: 4. rolfTag stays the same (set) then we have to validate the difference of rolf risks. + // if ($object->getRolfTag()->getCode() !== $rolfTagData['code']) {} + // TODO: 1. + // TODO: 2. was a rolf tag and now removed, then remove all the instance risks op have to be removed. + // TODO: 3. if a new rolf is changed, then all the op risks have to be updated. + // TODO: 4. rolfTag stays the same (set) then we have to validate the difference of rolf risks. + /* if there was no rolfTag and a new one is set. */ + if (!empty($rolfTagData) && $object->getRolfTag() === null) { + } } } diff --git a/src/Import/Processor/OperationalInstanceRiskImportProcessor.php b/src/Import/Processor/OperationalInstanceRiskImportProcessor.php index 5b3cd2b3..26e661c0 100644 --- a/src/Import/Processor/OperationalInstanceRiskImportProcessor.php +++ b/src/Import/Processor/OperationalInstanceRiskImportProcessor.php @@ -12,7 +12,7 @@ use Monarc\Core\Service\ConnectedUserService; use Monarc\FrontOffice\Entity; use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; -use Monarc\FrontOffice\Import\Traits\EvaluationConverterTrait; +use Monarc\FrontOffice\Import\Service\InstanceImportService; use Monarc\FrontOffice\Service\AnrInstanceRiskOpService; use Monarc\FrontOffice\Service\AnrRecommendationRiskService; use Monarc\FrontOffice\Service\InstanceRiskOwnerService; @@ -50,9 +50,7 @@ public function __construct( public function processOperationalInstanceRisksData( Entity\Anr $anr, Entity\Instance $instance, - array $operationalInstanceRisksData, - bool $withEval, - bool $isImportTypeAnr + array $operationalInstanceRisksData ): void { $currentOperationalRiskScalesData = $this->operationalRiskScaleImportProcessor ->getCurrentOperationalRiskScalesData($anr); @@ -60,8 +58,12 @@ public function processOperationalInstanceRisksData( $areScalesLevelsOfLikelihoodDifferent = false; $areImpactScaleTypesValuesDifferent = false; $matchedScaleTypesMap = []; + $withEval = $this->importCacheHelper->getValueFromArrayCache('with_eval'); + $isImportTypeInstance = $this->importCacheHelper->getValueFromArrayCache( + 'import_type' + ) === InstanceImportService::IMPORT_TYPE_INSTANCE; /* For the instances import with evaluations the values have to be converted to the current analysis scales. */ - if ($withEval && !$isImportTypeAnr) { + if ($withEval && $isImportTypeInstance) { $externalOperationalRiskScalesData = $this->operationalRiskScaleImportProcessor ->getExternalOperationalRiskScalesData($anr, []); if ($externalOperationalRiskScalesData === null) { @@ -87,7 +89,7 @@ public function processOperationalInstanceRisksData( $object = $instance->getObject(); $operationalInstanceRisk = $this->anrInstanceRiskOpService ->createInstanceRiskOpObject($instance, $object, $operationalRisk); - if ($withEval) { + if ($this->importCacheHelper->getValueFromArrayCache('with_eval')) { $operationalInstanceRisk ->setBrutProb((int)$operationalInstanceRiskData['brutProb']) ->setNetProb((int)$operationalInstanceRiskData['netProb']) @@ -121,9 +123,7 @@ public function processOperationalInstanceRisksData( $externalOperationalRiskScalesData, $matchedScaleTypesMap, $operationalInstanceRiskData, - $areImpactScaleTypesValuesDifferent, - $withEval, - $isImportTypeAnr + $areImpactScaleTypesValuesDifferent ); if ($withEval) { @@ -157,10 +157,12 @@ private function processOperationalRiskScalesTypes( array $externalOperationalRiskScalesData, array $matchedScaleTypesMap, array $operationalInstanceRiskData, - bool $areImpactScaleTypesValuesDifferent, - bool $withEval, - bool $isImportTypeAnr + bool $areImpactScaleTypesValuesDifferent ): array { + $withEval = $this->importCacheHelper->getValueFromArrayCache('with_eval'); + $isImportTypeAnr = $this->importCacheHelper + ->getValueFromArrayCache('import_type') === InstanceImportService::IMPORT_TYPE_ANR; + $currentImpactScaleData = $currentOperationalRiskScalesData[OperationalRiskScaleSuperClass::TYPE_IMPACT]; foreach ($currentImpactScaleData['operationalRiskScaleTypes'] as $index => $scaleType) { /** @var Entity\OperationalRiskScaleType $operationalRiskScaleType */ diff --git a/src/Import/Processor/OperationalRiskImportProcessor.php b/src/Import/Processor/OperationalRiskImportProcessor.php index d1b56345..993fe0fa 100644 --- a/src/Import/Processor/OperationalRiskImportProcessor.php +++ b/src/Import/Processor/OperationalRiskImportProcessor.php @@ -73,7 +73,7 @@ public function processOperationalRiskData(Entity\Anr $anr, array $operationalRi private function getRolfRiskFromCache(Entity\Anr $anr, string $code): ?Entity\RolfRisk { if (!$this->importCacheHelper->isCacheKeySet('is_rolf_risks_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_rolf_risks_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_rolf_risks_loaded', true); /** @var Entity\RolfRisk $rolfRisk */ foreach ($this->rolfRiskTable->findByAnr($anr) as $rolfRisk) { $this->importCacheHelper->addItemToArrayCache('rolf_risks_by_code', $rolfRisk, $rolfRisk->getCode()); diff --git a/src/Import/Processor/RecommendationImportProcessor.php b/src/Import/Processor/RecommendationImportProcessor.php index 3964ce06..4a5c7a51 100644 --- a/src/Import/Processor/RecommendationImportProcessor.php +++ b/src/Import/Processor/RecommendationImportProcessor.php @@ -124,7 +124,7 @@ private function getRecommendationSetFromCache( private function prepareRecommendationsCache(Entity\Anr $anr): void { if (!$this->importCacheHelper->isCacheKeySet('is_recommendations_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_recommendations_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_recommendations_cache_loaded', true); $this->currentMaxRecommendationPosition = $this->recommendationTable->findMaxPosition(['anr' => $anr]); /** @var Entity\RecommendationSet $recommendationSet */ foreach ($this->recommendationSetTable->findByAnr($anr) as $recommendationSet) { diff --git a/src/Import/Processor/ReferentialImportProcessor.php b/src/Import/Processor/ReferentialImportProcessor.php index 6eeb104b..43cd89aa 100644 --- a/src/Import/Processor/ReferentialImportProcessor.php +++ b/src/Import/Processor/ReferentialImportProcessor.php @@ -161,7 +161,7 @@ public function getMeasureFromCache(Entity\Anr $anr, string $measureUuid): ?Enti private function getSoaCategoryFromCache(Entity\Anr $anr, string $refUuidAndSoaCatLabel): ?Entity\SoaCategory { if (!$this->importCacheHelper->isCacheKeySet('is_soa_categories_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_soa_categories_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_soa_categories_cache_loaded', true); /** @var Entity\SoaCategory $soaCategory */ foreach ($this->soaCategoryTable->findByAnr($anr) as $soaCategory) { $this->importCacheHelper->addItemToArrayCache( @@ -179,7 +179,7 @@ private function getSoaCategoryFromCache(Entity\Anr $anr, string $refUuidAndSoaC private function prepareReferentialsAndMeasuresCache(Entity\Anr $anr): void { if (!$this->importCacheHelper->isCacheKeySet('is_referentials_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_referentials_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_referentials_cache_loaded', true); /** @var Entity\Referential $referential */ foreach ($this->referentialTable->findByAnr($anr) as $referential) { $this->importCacheHelper->addItemToArrayCache('referentials', $referential, $referential->getUuid()); diff --git a/src/Import/Processor/RolfTagImportProcessor.php b/src/Import/Processor/RolfTagImportProcessor.php index d384ceb0..df38668a 100644 --- a/src/Import/Processor/RolfTagImportProcessor.php +++ b/src/Import/Processor/RolfTagImportProcessor.php @@ -56,7 +56,7 @@ public function processRolfTagData(Entity\Anr $anr, array $rolfTagData): Entity\ public function getRolfTagFromCache(Entity\Anr $anr, string $code): ?Entity\RolfTag { if (!$this->importCacheHelper->isCacheKeySet('is_rolf_tags_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_rolf_tags_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_rolf_tags_cache_loaded', true); /** @var Entity\RolfTag $rolfTag */ foreach ($this->rolfTagTable->findByAnr($anr) as $rolfTag) { $this->importCacheHelper->addItemToArrayCache('rolf_tags_by_code', $rolfTag, $rolfTag->getCode()); diff --git a/src/Import/Processor/SoaImportProcessor.php b/src/Import/Processor/SoaImportProcessor.php index e5e72a53..7cdaf854 100644 --- a/src/Import/Processor/SoaImportProcessor.php +++ b/src/Import/Processor/SoaImportProcessor.php @@ -29,6 +29,7 @@ public function __construct( private ImportCacheHelper $importCacheHelper, private SoaService $soaService, private SoaScaleCommentService $soaScaleCommentService, + private ReferentialImportProcessor $referentialImportProcessor, ConnectedUserService $connectedUserService ) { $this->connectedUser = $connectedUserService->getConnectedUser(); @@ -69,7 +70,7 @@ public function mergeSoaScaleComments(Entity\Anr $anr, array $newSoaScaleComment $this->soaScaleCommentTable->save($currentSoaScaleComment, false); } $this->importCacheHelper->addItemToArrayCache( - 'soa_scale_comments', + 'soa_scale_comments_by_index', $currentSoaScaleComment, $currentSoaScaleComment->getScaleIndex() ); @@ -87,7 +88,8 @@ public function mergeSoaScaleComments(Entity\Anr $anr, array $newSoaScaleComment $newSoaScaleCommentsData[$soaScaleCommentKey]['colour'], $newSoaScaleCommentsData[$soaScaleCommentKey]['comment'] ); - $this->importCacheHelper->addItemToArrayCache('soa_scale_comments', $newSoaScaleComment, $index); + $this->importCacheHelper + ->addItemToArrayCache('soa_scale_comments_by_index', $newSoaScaleComment, $index); } } } @@ -129,12 +131,16 @@ private function processSoaData(Entity\Anr $anr, array $soaData): Entity\Soa } if (isset($soaData['soaScaleCommentIndex']) && $soaData['soaScaleCommentIndex'] !== null) { /* New structure from v2.13.1 or set workaround for the old export. */ - $soaData['soaScaleComment'] = $this->importCacheHelper->getItemFromArrayCache( - 'soa_scale_comments', - $soaData['soaScaleCommentIndex'] - ); + $soaData['soaScaleComment'] = $this->importCacheHelper + ->getItemFromArrayCache('soa_scale_comments_by_index', $soaData['soaScaleCommentIndex']); } $this->soaService->patchSoaObject($anr, $soa, $soaData, false); + } else { + $measure = $this->referentialImportProcessor->getMeasureFromCache($anr, $measureUuid); + if ($measure === null) { + throw new \Exception('Measures have to be processed before the Statement of Applicability data.', 412); + } + $soa = $this->soaService->createSoaObject($anr, $measure, $soaData); } return $soa; @@ -157,10 +163,10 @@ private function adjustSoasWithScaleCommentsChanges( 0 ); if ($soaComment->getScaleIndex() !== $newScaleIndex - && $this->importCacheHelper->isItemInArrayCache('soa_scale_comments', $newScaleIndex) + && $this->importCacheHelper->isItemInArrayCache('soa_scale_comments_by_index', $newScaleIndex) ) { $soa->setSoaScaleComment( - $this->importCacheHelper->getItemFromArrayCache('soa_scale_comments', $newScaleIndex) + $this->importCacheHelper->getItemFromArrayCache('soa_scale_comments_by_index', $newScaleIndex) ); $this->soaTable->save($soa, false); } @@ -170,7 +176,7 @@ private function adjustSoasWithScaleCommentsChanges( private function getSoaFromCache(Entity\Anr $anr, string $measureUuid): ?Entity\Soa { if (!$this->importCacheHelper->isCacheKeySet('is_soa_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_soa_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_soa_cache_loaded', true); /** @var Entity\Soa $soa */ foreach ($this->soaTable->findByAnr($anr) as $soa) { $this->importCacheHelper->addItemToArrayCache( diff --git a/src/Import/Processor/ThreatImportProcessor.php b/src/Import/Processor/ThreatImportProcessor.php index 578948d5..0aba80b2 100644 --- a/src/Import/Processor/ThreatImportProcessor.php +++ b/src/Import/Processor/ThreatImportProcessor.php @@ -72,7 +72,7 @@ public function processThreatData(Entity\Anr $anr, array $threatData, array $the public function getThreatFromCache(Entity\Anr $anr, string $uuid): ?Entity\Threat { if (!$this->importCacheHelper->isCacheKeySet('is_threats_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_threats_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_threats_cache_loaded', true); /** @var Entity\Threat $threat */ foreach ($this->threatTable->findByAnr($anr) as $threat) { $this->importCacheHelper->addItemToArrayCache('threats_by_uuid', $threat, $threat->getUuid()); @@ -101,7 +101,7 @@ private function processThemeData(Entity\Anr $anr, array $themeData): Entity\The private function getThemeFromCache(Entity\Anr $anr, string $label): ?Entity\Theme { if (!$this->importCacheHelper->isCacheKeySet('is_themes_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_themes_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_themes_cache_loaded', true); /** @var Entity\Theme $theme */ foreach ($this->themeTable->findByAnr($anr) as $theme) { $this->importCacheHelper diff --git a/src/Import/Processor/VulnerabilityImportProcessor.php b/src/Import/Processor/VulnerabilityImportProcessor.php index 92814b2d..46b9d001 100644 --- a/src/Import/Processor/VulnerabilityImportProcessor.php +++ b/src/Import/Processor/VulnerabilityImportProcessor.php @@ -58,13 +58,16 @@ public function processVulnerabilityData(Entity\Anr $anr, array $vulnerabilityDa public function getVulnerabilityFromCache(Entity\Anr $anr, string $uuid): ?Entity\Vulnerability { if (!$this->importCacheHelper->isCacheKeySet('is_vulnerabilities_cache_loaded')) { - $this->importCacheHelper->addItemToArrayCache('is_vulnerabilities_cache_loaded', true); + $this->importCacheHelper->setArrayCacheValue('is_vulnerabilities_cache_loaded', true); /** @var Entity\Vulnerability $vulnerability */ foreach ($this->vulnerabilityTable->findByAnr($anr) as $vulnerability) { $this->importCacheHelper ->addItemToArrayCache('vulnerabilities_by_uuid', $vulnerability, $vulnerability->getUuid()); - $this->importCacheHelper - ->addItemToArrayCache('vulnerabilities_codes', $vulnerability->getCode(), $vulnerability->getCode()); + $this->importCacheHelper->addItemToArrayCache( + 'vulnerabilities_codes', + $vulnerability->getCode(), + $vulnerability->getCode() + ); } } diff --git a/src/Import/Service/AssetImportService.php b/src/Import/Service/AssetImportService.php deleted file mode 100644 index ffdd79a3..00000000 --- a/src/Import/Service/AssetImportService.php +++ /dev/null @@ -1,132 +0,0 @@ -connectedUser = $connectedUserService->getConnectedUser(); - } - - public function importFromArray(array $data, Anr $anr): ?Asset - { - if (!isset($data['type']) || $data['type'] !== 'asset') { - return null; - } - - $asset = $this->assetImportProcessor->processAssetData($anr, $data['asset']); - /* In the new structure 'amvs' => 'informationRisks', 'vuls' => 'vulnerabilities'. */ - if (!empty($data['amvs']) || !empty($data['informationRisks'])) { - $this->threatImportProcessor->processThreatsData($anr, $data['threats'], $data['themes'] ?? []); - $this->vulnerabilityImportProcessor - ->processVulnerabilitiesData($anr, $data['vuls'] ?? $data['vulnerabilities']); - $this->processInformationRisksData($data['amvs'] ?? $data['informationRisks'], $anr, $asset); - } - - return $asset; - } - - // TODO: use processors to create the objects. - private function processInformationRisksData(array $amvsData, Anr $anr, Asset $asset): void - { - foreach ($amvsData as $amvUuid => $amvData) { - /** @var Amv|null $amv */ - $amv = $this->amvTable->findByUuidAndAnr($amvUuid, $anr, false); - if ($amv === null) { - $amv = (new Amv()) - ->setUuid($amvUuid) - ->setAnr($anr) - ->setAsset($asset) - ->setCreator($this->connectedUser->getEmail()); - - $threat = $this->threatImportProcessor->getThreatFromCache($anr, $amvData['threat']); - $vulnerability = $this->vulnerabilityImportProcessor - ->getVulnerabilityFromCache($anr, $amvData['vulnerability']); - if ($threat === null || $vulnerability === null) { - throw new Exception(sprintf( - 'The import file is malformed. AMV\'s "%s" threats or vulnerability was not processed before.', - $amvUuid - )); - } - - $amv->setThreat($threat)->setVulnerability($vulnerability); - - $this->amvTable->save($amv, false); - - foreach ($asset->getInstances() as $instance) { - $instanceRisk = (new InstanceRisk()) - ->setAnr($anr) - ->setAmv($amv) - ->setAsset($asset) - ->setInstance($instance) - ->setThreat($threat) - ->setVulnerability($vulnerability) - ->setCreator($this->connectedUser->getEmail()); - - $this->instanceRiskTable->save($instanceRisk, false); - } - } - -// if (!empty($amvData['measures'])) { -// $this->processMeasuresAndReferentialData($amvData['measures'], $anr, $amv); -// } - } - - // TODO: perhaps we can do this before the previous foreach or find another solution. - foreach ($this->amvTable->findByAnrAndAsset($anr, $asset) as $oldAmv) { - if (!isset($amvsData[$oldAmv->getUuid()])) { - /** Set related instance risks to specific and delete the amvs later. */ - $instanceRisks = $oldAmv->getInstanceRisks(); - - // TODO: remove the double iteration when #240 is done. - // We do it due to multi-fields relation issue. When amv is set to null, anr is set to null as well. - foreach ($instanceRisks as $instanceRisk) { - $instanceRisk->setAmv(null); - $instanceRisk->setAnr(null); - $instanceRisk->setSpecific(InstanceRisk::TYPE_SPECIFIC); - $this->instanceRiskTable->save($instanceRisk, false); - } - $this->instanceRiskTable->flush(); - - foreach ($instanceRisks as $instanceRisk) { - $instanceRisk - ->setAnr($anr) - ->setUpdater($this->connectedUser->getEmail()); - $this->instanceRiskTable->save($instanceRisk, false); - } - $this->instanceRiskTable->flush(); - - $amvsToDelete[] = $oldAmv; - } - } - - if (!empty($amvsToDelete)) { - $this->amvTable->removeList($amvsToDelete); - } - } -} diff --git a/src/Import/Service/InstanceImportService.php b/src/Import/Service/InstanceImportService.php index 4557d2cc..a2413535 100755 --- a/src/Import/Service/InstanceImportService.php +++ b/src/Import/Service/InstanceImportService.php @@ -10,6 +10,7 @@ use Monarc\Core\Exception\Exception; use Monarc\Core\Service\Traits\RiskCalculationTrait; use Monarc\FrontOffice\Entity; +use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; use Monarc\FrontOffice\Import\Processor; use Monarc\FrontOffice\Import\Traits; use Monarc\FrontOffice\Table; @@ -17,6 +18,10 @@ class InstanceImportService { + public const IMPORT_TYPE_ANR = 'anr'; + public const IMPORT_TYPE_INSTANCE = 'instance'; + public const IMPORT_TYPE_OBJECT = 'object'; + use RiskCalculationTrait; use Traits\EvaluationConverterTrait; use Traits\ImportFileContentTrait; @@ -42,6 +47,7 @@ public function __construct( private Processor\OperationalRiskScaleImportProcessor $operationalRiskScaleImportProcessor, private Processor\OperationalInstanceRiskImportProcessor $operationalInstanceRiskImportProcessor, private Processor\SoaImportProcessor $soaImportProcessor, + private ImportCacheHelper $importCacheHelper, private Service\AnrRecordService $anrRecordService, private Table\InstanceTable $instanceTable, private Table\AnrTable $anrTable, @@ -106,26 +112,27 @@ private function importFromArray( $this->validateIfImportIsPossible($anr, $parentInstance, $data); $withEval = $data['withEval'] ?? $data['with_eval']; + /* To simplify the value access across all the processors. */ + $this->importCacheHelper->setArrayCacheValue('with_eval', $withEval); $createdInstances = []; - if ($data['type'] === 'anr') { + if ($data['type'] === self::IMPORT_TYPE_ANR) { + $this->importCacheHelper->setArrayCacheValue('import_type', self::IMPORT_TYPE_ANR); $createdInstances = $this->isImportingDataVersionLowerThan('2.13.1') ? $this->importAnrFromArray($data, $anr, $parentInstance, $importMode) : $this->processAnrImport($anr, $data, $parentInstance, $importMode); - } elseif ($data['type'] === 'instance') { + } elseif ($data['type'] === self::IMPORT_TYPE_INSTANCE) { + $this->importCacheHelper->setArrayCacheValue('import_type', self::IMPORT_TYPE_INSTANCE); if ($withEval) { $this->scaleImportProcessor->prepareScalesCache($anr, $data); - $this->operationalRiskScaleImportProcessor->prepareExternalOperationalRiskScalesDataCache( - $anr, - $data - ); + $this->operationalRiskScaleImportProcessor->prepareExternalOperationalRiskScalesDataCache($anr, $data); } if ($this->isImportingDataVersionLowerThan('2.13.1')) { $data['instance'] = $this->adaptOldInstanceDataToNewFormat($data, $anr->getLanguage()); } $createdInstances[] = $this->instanceImportProcessor - ->processInstanceData($anr, $data['instance'], $parentInstance, $importMode, $withEval, false); + ->processInstanceData($anr, $data['instance'], $parentInstance, $importMode); } $this->anrTable->flush(); @@ -159,7 +166,6 @@ private function processAnrImport( /* Apply the importing operational risks scales. */ $this->operationalRiskScaleImportProcessor->adjustOperationalRisksScaleValuesBasedOnNewScales($anr, $data); $this->operationalRiskScaleImportProcessor->updateOperationalRisksScalesAndRelatedInstances($anr, $data); - } if ($data['withInterviews']) { /* Process the interviews' data. */ @@ -188,6 +194,9 @@ private function processAnrImport( if (!empty($data['soaScaleComments'])) { $this->soaImportProcessor->mergeSoaScaleComments($anr, $data['soaScaleComments'], false); } + if (!empty($data['soas'])) { + $this->soaImportProcessor->processSoasData($anr, $data['soas']); + } /* import the GDPR records. */ foreach ($data['gdprRecords'] ?? [] as $gdprRecordData) { @@ -268,7 +277,8 @@ private function importAnrFromArray( /* Import SOA Scale Comments if they are passed. Only in the new structure, when the functionality exists. */ if (!empty($data['soaScaleComment'])) { - $this->soaImportProcessor->mergeSoaScaleComments($anr, $data['soaScaleComment'], true); + $soaScaleCommentsData = $this->adaptOldSoaScaleCommentsToNewFormat($data['soaScaleComment']); + $this->soaImportProcessor->mergeSoaScaleComments($anr, $soaScaleCommentsData, true); } /* Import the Statement of Applicability (SOA). */ @@ -291,7 +301,7 @@ private function importAnrFromArray( ); } - $withEval = (bool)$data['with_eval']; + $withEval = $this->importCacheHelper->getValueFromArrayCache('with_eval'); /* Import scales. */ if ($withEval && !empty($data['scales'])) { /* Adjust the values of information risks' scales. */ @@ -318,7 +328,7 @@ private function importAnrFromArray( } $instances[] = $this->instanceImportProcessor - ->processInstanceData($anr, $instanceData, $parentInstance, $modeImport, $withEval); + ->processInstanceData($anr, $instanceData, $parentInstance, $modeImport); } return $instances; diff --git a/src/Import/Service/ObjectImportService.php b/src/Import/Service/ObjectImportService.php index 10d5220f..f6f85cef 100644 --- a/src/Import/Service/ObjectImportService.php +++ b/src/Import/Service/ObjectImportService.php @@ -12,6 +12,7 @@ use Monarc\Core\Service\Export\ObjectExportService as CoreObjectExportService; use Monarc\Core\Table\MonarcObjectTable as CoreMonarcObjectTable; use Monarc\FrontOffice\Entity; +use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; use Monarc\FrontOffice\Import\Processor; use Monarc\FrontOffice\Import\Traits; use Monarc\FrontOffice\Table; @@ -30,7 +31,8 @@ public function __construct( private Processor\ObjectCategoryImportProcessor $objectCategoryImportProcessor, private Table\ClientTable $clientTable, private CoreMonarcObjectTable $coreMonarcObjectTable, - private CoreObjectExportService $coreObjectExportService + private CoreObjectExportService $coreObjectExportService, + private ImportCacheHelper $importCacheHelper ) { } @@ -146,8 +148,10 @@ public function importFromArray( $this->setAndValidateImportingDataVersion($data); - /* Convert the old structure format to the new one if needed. */ - if ($this->isImportingDataVersionLowerThan('2.13.1')) { + $this->importCacheHelper->setArrayCacheValue('import_type', InstanceImportService::IMPORT_TYPE_OBJECT); + + /* Convert the old structure format to the new one if the import is from MOSP or importing version is below. */ + if (!empty($data['mosp']) || $this->isImportingDataVersionLowerThan('2.13.1')) { $data = $this->adaptOldObjectDataStructureToNewFormat($data); } diff --git a/src/Import/Traits/ImportDataStructureAdapterTrait.php b/src/Import/Traits/ImportDataStructureAdapterTrait.php index 37cae309..6e16cd70 100644 --- a/src/Import/Traits/ImportDataStructureAdapterTrait.php +++ b/src/Import/Traits/ImportDataStructureAdapterTrait.php @@ -46,6 +46,16 @@ public function adoptOldScalesDataStructureToNewFormat(array $data, int $languag return $newStructure; } + public function adaptOldSoaScaleCommentsToNewFormat(array $soaScaleCommentsData): array + { + $newStructure = []; + foreach ($soaScaleCommentsData as $scaleCommentData) { + $newStructure[$scaleCommentData['scaleIndex']] = $scaleCommentData; + } + + return $newStructure; + } + /** Converts all the instance related data from the structure prior v2.13.1 to the new one. */ public function adaptOldInstanceDataToNewFormat(array $data, int $languageIndex): array { diff --git a/src/InputFormatter/Soa/GetSoasInputFormatter.php b/src/InputFormatter/Soa/GetSoasInputFormatter.php index dd90c19f..5fe15da6 100644 --- a/src/InputFormatter/Soa/GetSoasInputFormatter.php +++ b/src/InputFormatter/Soa/GetSoasInputFormatter.php @@ -33,7 +33,7 @@ class GetSoasInputFormatter extends AbstractInputFormatter ], ]; - protected static array $ignoredFilterFieldValues = ['category' => 0]; + protected static array $ignoredFilterFieldValues = ['category' => '0']; protected static array $orderParamsToFieldsMap = [ 'm.code' => 'measure.code|LENGTH,measure.code', diff --git a/src/Service/AnrInstanceRiskService.php b/src/Service/AnrInstanceRiskService.php index aa873538..d490a744 100644 --- a/src/Service/AnrInstanceRiskService.php +++ b/src/Service/AnrInstanceRiskService.php @@ -201,7 +201,7 @@ public function createInstanceRisks( } } else { foreach ($object->getAsset()->getAmvs() as $amv) { - $instanceRisk = $this->createInstanceRisk($instance, $amv, null, null, null, $saveInDb); + $instanceRisk = $this->createInstanceRisk($instance, $amv, null, null, null, $saveInDb); /* Process risk owner and context in case of import. */ if (!empty($params['risks'])) { diff --git a/src/Service/DeliverableGenerationService.php b/src/Service/DeliverableGenerationService.php index da1f8b4f..36983e0b 100644 --- a/src/Service/DeliverableGenerationService.php +++ b/src/Service/DeliverableGenerationService.php @@ -30,6 +30,7 @@ */ class DeliverableGenerationService { + // TODO: correct all the get ->getList usage due to the formatted param. private UserSuperClass $connectedUser; private $currentLangAnrIndex = 1;