Skip to content

Commit

Permalink
Fixed the import process issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Jul 25, 2024
1 parent d9f1840 commit 208aac1
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/Import/Processor/AnrMethodStepImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function processAnrMethodStepsData(Entity\Anr $anr, array $methodStepsDat

/* Set data of text-boxes. */
if (!empty($methodStepsData['data'])) {
$anr->setContextAnaRisk($methodStepsData['data']['contextAnaRisk'])
->setContextGestRisk($methodStepsData['data']['contextGestRisk'])
->setSynthThreat($methodStepsData['data']['synthThreat'])
->setSynthAct($methodStepsData['data']['synthAct'])
$anr->setContextAnaRisk($methodStepsData['data']['contextAnaRisk'] ?? '')
->setContextGestRisk($methodStepsData['data']['contextGestRisk'] ?? '')
->setSynthThreat($methodStepsData['data']['synthThreat'] ?? '')
->setSynthAct($methodStepsData['data']['synthAct'] ?? '')
->setUpdater($this->connectedUser->getEmail());
$this->anrTable->save($anr, false);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Import/Processor/InstanceImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function processInstancesData(
Entity\Anr $anr,
array $instancesData,
?Entity\Instance $parentInstance,
string $importMode,
bool $withEval
string $importMode
): array {
$maxPositionInsideOfParentInstance = 0;
/* The query to get the max position is executed only if the parent instances is set and stored in the DB. */
Expand Down Expand Up @@ -77,7 +76,7 @@ public function processInstanceData(
$objectCategory = null;
if (isset($instanceData['object']['category'])) {
$objectCategory = $this->objectCategoryImportProcessor
->processObjectCategoryData($anr, $objectCategory, $importMode);
->processObjectCategoryData($anr, $instanceData['object']['category'], $importMode);
}
$instanceData['object'] = $this->objectImportProcessor
->processObjectData($anr, $objectCategory, $instanceData['object'], $importMode);
Expand Down
6 changes: 3 additions & 3 deletions src/Import/Processor/InstanceRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ private function processInstanceRiskData(Entity\Instance $instance, array $insta
->setCacheMaxRisk($instanceRiskData['cacheMaxRisk'])
->setCacheTargetedRisk($instanceRiskData['cacheTargetedRisk'])
->setKindOfMeasure($instanceRiskData['kindOfMeasure'])
->setComment($instanceRiskData['comment'])
->setCommentAfter($instanceRiskData['commentAfter'])
->setComment($instanceRiskData['comment'] ?? '')
->setCommentAfter($instanceRiskData['commentAfter'] ?? '')
->setIsThreatRateNotSetOrModifiedExternally(
(bool)$instanceRiskData['isThreatRateNotSetOrModifiedExternally']
)
->setContext($instanceRiskData['context']);
->setContext($instanceRiskData['context'] ?? '');
if (!empty($instanceRiskData['riskOwner'])) {
$this->instanceRiskOwnerService
->processRiskOwnerNameAndAssign($instanceRiskData['riskOwner'], $instanceRisk);
Expand Down
20 changes: 11 additions & 9 deletions src/Import/Processor/ObjectImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private function prepareUniqueObjectName(string $objectName, int $index = 1): st
return $objectName;
}

/* Merges the amvs (information risks) of the existing object. */
/** Merges the amvs (information risks) of the existing object. */
private function mergeAssetInformationRisks(Entity\MonarcObject $object, array $informationRisksData): void
{
$existingAmvs = [];
Expand Down Expand Up @@ -224,17 +224,19 @@ private function mergeAssetInformationRisks(Entity\MonarcObject $object, array $
}
}

/* Merges the rolfRisks (operational risks) of the existing object. */
/** Merges the rolfRisks (operational risks) of the existing object. */
private function mergeRolfTagOperationalRisks(Entity\MonarcObject $object, ?array $rolfTagData): void
{
// 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) {

// TODO: 1:
/* if there was no rolfTag and a new one is set. */

Check failure on line 233 in src/Import/Processor/ObjectImportProcessor.php

View workflow job for this annotation

GitHub Actions / build

Blank line found at end of control structure
} elseif (empty($rolfTagData) && $object->getRolfTag() !== null) {
// TODO: 2. was a rolf tag and now removed, then remove all the instance risks op have to be removed.
} elseif ($object->getRolfTag()->getCode() !== $rolfTagData['code']) {
// TODO: 3. if a new rolf is changed, then all the op risks have to be updated.
} else {
// TODO: 4. rolfTag stays the same (set) then we have to validate the difference of rolf risks.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function updateOperationalRisksScalesAndRelatedInstances(Entity\Anr $anr,

$maxIndexForLikelihood = 0;
/* This is currently applicable only for likelihood scales type */
foreach ($externalScaleData['object']->getOperationalRiskScaleComments() as $scaleCommentData) {
foreach ($externalScaleData['operationalRiskScaleComments'] as $scaleCommentData) {
$this->createOrUpdateOperationalRiskScaleComment(
$anr,
true,
Expand Down
31 changes: 6 additions & 25 deletions src/Import/Processor/SoaImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function processSoasData(Entity\Anr $anr, array $soasData): void
}
}

public function mergeSoaScaleComments(Entity\Anr $anr, array $newSoaScaleCommentsData, bool $isOldStructure): void
public function mergeSoaScaleComments(Entity\Anr $anr, array $newSoaScaleCommentsData): void
{
$currentSoaScaleCommentsNumber = 0;
$newSoaScaleCommentsNumber = \count($newSoaScaleCommentsData);
Expand Down Expand Up @@ -102,38 +102,19 @@ public function mergeSoaScaleComments(Entity\Anr $anr, array $newSoaScaleComment
$newSoaScaleCommentsNumber - 1
);
}

/* To make exported prior v2.13.1 files working, a map with soa_scale_comments_ids_to_indexes is created. */
if ($isOldStructure) {
foreach ($newSoaScaleCommentsData as $oldId => $newSoaScaleCommentData) {
$this->importCacheHelper->addItemToArrayCache(
'soa_scale_comments_ids_to_indexes',
$newSoaScaleCommentData['scaleIndex'],
$oldId
);
}
}
}

private function processSoaData(Entity\Anr $anr, array $soaData): Entity\Soa
{
/* Support the old structure field name prior v2.13.1. */
$measureUuid = $soaData['measure_id'] ?? $soaData['measureUuid'];
/* New SOAs were created and cached during the new measures process, and existed SOA's cache is initialising. */
$soa = $this->getSoaFromCache($anr, $measureUuid);
/* New SOAs were created and cached during the new measures process, and existed SOAs cache also initialised. */
if (isset($soaData['soaScaleCommentIndex']) && $soaData['soaScaleCommentIndex'] !== null) {
$soaData['soaScaleComment'] = $this->importCacheHelper
->getItemFromArrayCache('soa_scale_comments_by_index', $soaData['soaScaleCommentIndex']);
}
if ($soa !== null) {
if (isset($soaData['soaScaleComment'])) {
/* Support the old structure field name prior v2.13.1. */
$soaData['soaScaleCommentIndex'] = $this->importCacheHelper->getItemFromArrayCache(
'soa_scale_comments_ids_to_indexes',
$soaData['soaScaleComment']
);
}
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_by_index', $soaData['soaScaleCommentIndex']);
}
$this->soaService->patchSoaObject($anr, $soa, $soaData, false);
} else {
$measure = $this->referentialImportProcessor->getMeasureFromCache($anr, $measureUuid);
Expand Down
9 changes: 6 additions & 3 deletions src/Import/Service/InstanceImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ private function processAnrImport(

/* Process the Instances, Instance Risks, Consequences and evaluations data. */
$result = $this->instanceImportProcessor
->processInstancesData($anr, $data['instances'], $parentInstance, $importMode, $data['withEval']);
->processInstancesData($anr, $data['instances'], $parentInstance, $importMode);

/* Process Soa and SoaScaleComments data. */
if (!empty($data['soaScaleComments'])) {
$this->soaImportProcessor->mergeSoaScaleComments($anr, $data['soaScaleComments'], false);
$this->soaImportProcessor->mergeSoaScaleComments($anr, $data['soaScaleComments']);
}
if (!empty($data['soas'])) {
$this->soaImportProcessor->processSoasData($anr, $data['soas']);
Expand Down Expand Up @@ -278,11 +278,14 @@ private function importAnrFromArray(
/* Import SOA Scale Comments if they are passed. Only in the new structure, when the functionality exists. */
if (!empty($data['soaScaleComment'])) {
$soaScaleCommentsData = $this->adaptOldSoaScaleCommentsToNewFormat($data['soaScaleComment']);
$this->soaImportProcessor->mergeSoaScaleComments($anr, $soaScaleCommentsData, true);
$this->soaImportProcessor->mergeSoaScaleComments($anr, $soaScaleCommentsData);
}

/* Import the Statement of Applicability (SOA). */
if (!empty($data['soas'])) {
if (!empty($data['soaScaleComment'])) {
$data['soas'] = $this->adaptOldSoasToNewFormatWithSoaScaleCommentIndex($data);
}
$this->soaImportProcessor->processSoasData($anr, $data['soas']);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Import/Service/ObjectImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function importFromArray(

/* 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);
$data = $this->adaptOldObjectDataStructureToNewFormat($data, $anr->getLanguage());
}

$objectCategory = $this->objectCategoryImportProcessor
Expand Down
41 changes: 31 additions & 10 deletions src/Import/Traits/ImportDataStructureAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public function adaptOldSoaScaleCommentsToNewFormat(array $soaScaleCommentsData)
return $newStructure;
}

public function adaptOldSoasToNewFormatWithSoaScaleCommentIndex(array $data): array
{
$newStructure = [];
foreach ($data['soas'] as $soaData) {
$newStructure[] = array_merge($soaData, isset($soaData['soaScaleComment']) ? [
'soaScaleCommentIndex' => $data['soaScaleComment'][$soaData['soaScaleComment']]['scaleIndex'] ?? -1,
] : []);
}

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
{
Expand All @@ -70,8 +82,8 @@ public function adaptOldInstanceDataToNewFormat(array $data, int $languageIndex)
'isConfidentialityInherited' => $data['instance']['ch'],
'isIntegrityInherited' => $data['instance']['ih'],
'isAvailabilityInherited' => $data['instance']['dh'],
'asset' => $data['instance']['object']['asset']['asset'],
'object' => $this->adaptOldObjectDataStructureToNewFormat($data['instance'])['object'],
'asset' => $data['object']['asset']['asset'],
'object' => $this->adaptOldObjectDataStructureToNewFormat($data['object'], $languageIndex)['object'],
'instanceMetadata' => $this->prepareInstanceMetadataData($data),
'instanceRisks' => $this->prepareInstanceRisksData($data, $languageIndex),
'operationalInstanceRisks' => $this->prepareOperationalInstanceRisksData($data, $languageIndex),
Expand All @@ -81,8 +93,15 @@ public function adaptOldInstanceDataToNewFormat(array $data, int $languageIndex)
}

/** Converts all the object's related data from the structure prior v2.13.1 to the new one. */
public function adaptOldObjectDataStructureToNewFormat(array $data): array
public function adaptOldObjectDataStructureToNewFormat(array $data, int $languageIndex): array
{
$newStructure['object'] = [
'uuid' => $data['object']['uuid'],
'name' => $data['object']['name' . $languageIndex],
'label' => $data['object']['label' . $languageIndex],
'mode' => $data['object']['mode'],
'scope' => $data['object']['scope'],
];
$newStructure['object']['category'] = $this->adaptOldCategoryStructureToNewFormat($data);
$newStructure['object']['asset'] = $data['asset']['asset'];
$newStructure['object']['asset']['informationRisks'] = [];
Expand All @@ -108,7 +127,7 @@ public function adaptOldObjectDataStructureToNewFormat(array $data): array
];
}
$newStructure['object']['rolfTag'] = null;
if (!empty($data['object']['rolfTag']) && !empty($data['rolfTags'][$data['object']['rolfTag']])) {
if (isset($data['object']['rolfTag']) && !empty($data['rolfTags'][$data['object']['rolfTag']])) {
$newStructure['object']['rolfTag'] = $data['rolfTags'][$data['object']['rolfTag']];
$newStructure['object']['rolfTag']['rolfRisks'] = [];
foreach ($newStructure['object']['rolfTag']['risks'] ?? [] as $rolfRiskId) {
Expand All @@ -126,9 +145,10 @@ public function adaptOldObjectDataStructureToNewFormat(array $data): array
}
}
$newStructure['object']['children'] = [];
foreach ($data['object']['children'] as $childObjectData) {
foreach ($data['children'] ?? [] as $childObjectData) {
$newStructure['object']['children'][] = $this->adaptOldObjectDataStructureToNewFormat(
$childObjectData
$childObjectData,
$languageIndex
)['object'];
}

Expand All @@ -153,7 +173,7 @@ public function adaptOldRecommendationSetsDataToNewFormat(array $data): array
private function adaptOldCategoryStructureToNewFormat(array $data): array
{
$newCategoryStructure = [];
if (isset($data['categories'][$data['object']['category']])) {
if (isset($data['object']['category'], $data['categories'][$data['object']['category']])) {
$newCategoryStructure = $data['categories'][$data['object']['category']];
$newCategoryStructure['parent'] = $this
->prepareNewStructureOfParentsHierarchy($data['categories'], (int)$newCategoryStructure['parent']);
Expand Down Expand Up @@ -195,7 +215,7 @@ private function prepareInstanceRisksData(array $data, int $languageIndex): arra
{
$instanceRisksData = [];
foreach ($data['risks'] ?? [] as $instanceRiskDatum) {
$instanceRiskData = null;
$informationRiskData = null;
$threatData = $data['threats'][$instanceRiskDatum['threat']] ?? ['uuid' => $instanceRiskDatum['threat']];
if (!empty($threatData['theme'])) {
$threatData['theme'] = $data['object']['asset']['themes'][$threatData['theme']];
Expand All @@ -210,8 +230,9 @@ private function prepareInstanceRisksData(array $data, int $languageIndex): arra
$measuresData[] = $data['measures'][$measureUuid];
}
}
$instanceRiskData = [
$informationRiskData = [
'uuid' => $instanceRiskDatum['amv'],
'asset' => $data['object']['asset']['asset'],
'threat' => $threatData,
'vulnerability' => $vulnerabilityData,
'measures' => $measuresData,
Expand All @@ -227,7 +248,7 @@ private function prepareInstanceRisksData(array $data, int $languageIndex): arra
}

$instanceRisksData[] = [
'informationRisk' => $instanceRiskData,
'informationRisk' => $informationRiskData,
'threat' => $threatData,
'vulnerability' => $vulnerabilityData,
'specific' => $instanceRiskDatum['specific'],
Expand Down
3 changes: 3 additions & 0 deletions src/Service/AnrMeasureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public function createMeasureObject(
->setReferential($referential)
->setCategory($soaCategory)
->setCreator($this->connectedUser->getEmail());
if (!empty($data['uuid'])) {
$measure->setUuid($data['uuid']);
}

$this->measureTable->save($measure, $saveInDb);

Expand Down
21 changes: 14 additions & 7 deletions src/Service/AnrRecommendationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ public function create(Entity\Anr $anr, array $data, bool $saveInDb = true): Ent
if (isset($data['responsible']) || isset($data['responsable'])) {
$recommendation->setResponsible($data['responsible'] ?? $data['responsable']);
}
if (isset($data['duedate'])) {
if (!empty($data['duedate']) && !$data['duedate'] instanceof DateTime) {
$recommendation->setDueDateFromString($data['duedate']);
} else {
if (!empty($data['duedate'])) {
if ($data['duedate'] instanceof DateTime) {
$recommendation->setDueDate($data['duedate']);
} else {
if (\is_array($data['duedate'])) {
$data['duedate'] = $data['duedate']['date'];
}
$recommendation->setDueDateFromString($data['duedate']);
}
}
if (isset($data['counterTreated'])) {
Expand Down Expand Up @@ -122,9 +125,13 @@ public function patch(Entity\Anr $anr, string $uuid, array $data): Entity\Recomm
$recommendation = $this->recommendationTable->findByUuidAndAnr($uuid, $anr);

if (!empty($data['duedate'])) {
try {
$recommendation->setDueDate(new DateTime($data['duedate']));
} catch (Throwable) {
if ($data['duedate'] instanceof DateTime) {
$recommendation->setDueDate($data['duedate']);
} else {
if (\is_array($data['duedate'])) {
$data['duedate'] = $data['duedate']['date'];
}
$recommendation->setDueDateFromString($data['duedate']);
}
} elseif (isset($data['duedate']) && $data['duedate'] === '') {
$recommendation->setDueDate(null);
Expand Down
3 changes: 3 additions & 0 deletions src/Service/AnrReferentialService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function create(Anr $anr, array $data, bool $saveInDb = true): Referentia
{
/** @var Referential $referential */
$referential = (new Referential())->setAnr($anr)->setLabels($data);
if (!empty($data['uuid'])) {
$referential->setUuid($data['uuid']);
}

$this->referentialTable->save($referential, $saveInDb);

Expand Down
2 changes: 1 addition & 1 deletion src/Service/InstanceRiskOwnerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function create(Anr $anr, string $ownerName, bool $saveIdDb = false): Ins

public function getOrCreateInstanceRiskOwner(Anr $sourceAnr, Anr $anr, string $ownerName): InstanceRiskOwner
{
if ($this->cachedData['isInstanceRiskOwnersCacheLoaded']) {
if (isset($this->cachedData['isInstanceRiskOwnersCacheLoaded'])) {
$this->cachedData['isInstanceRiskOwnersCacheLoaded'] = true;
/** @var InstanceRiskOwner $instanceRiskOwner */
foreach ($this->instanceRiskOwnerTable->findByAnr($sourceAnr) as $instanceRiskOwner) {
Expand Down

0 comments on commit 208aac1

Please sign in to comment.