Skip to content

Commit

Permalink
Fixed the processor to support the old format and object import type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Jul 24, 2024
1 parent d893f51 commit d9f1840
Show file tree
Hide file tree
Showing 27 changed files with 224 additions and 336 deletions.
2 changes: 1 addition & 1 deletion src/Export/Service/AnrExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private function prepareInformationInstanceRiskData(
$recommendation = $recommendationRisk->getRecommendation();
$recommendationsData[] = array_merge(
$this->prepareRecommendationData($recommendation),
['commentAfter' => $recommendationRisk->getCommentAfter()]);
['commentAfter' => $recommendationRisk->getCommentAfter()]
);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Import/Helper/ImportCacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/Import/Processor/AnrMethodStepImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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'];
Expand All @@ -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']
) {
Expand All @@ -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(), '[]');
Expand Down
2 changes: 1 addition & 1 deletion src/Import/Processor/AssetImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Import/Processor/InformationRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
44 changes: 16 additions & 28 deletions src/Import/Processor/InstanceConsequenceImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'],
);
}
}
}
87 changes: 27 additions & 60 deletions src/Import/Processor/InstanceImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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'])) {
Expand All @@ -85,46 +84,33 @@ 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) {
$instance->refreshInheritedImpact();
}

$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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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'],
);
}
}
}
Loading

0 comments on commit d9f1840

Please sign in to comment.