|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * @link https://github.com/monarc-project for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2016-2026 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 |
| 5 | + * @license MONARC is licensed under GNU Affero General Public License version 3 |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Monarc\FrontOffice\Import\Processor; |
| 9 | + |
| 10 | +use Monarc\Core\Service\ConnectedUserService; |
| 11 | +use Monarc\FrontOffice\Entity; |
| 12 | +use Monarc\FrontOffice\Import\Helper\ImportCacheHelper; |
| 13 | +use Monarc\FrontOffice\Table\RiskSourceTable; |
| 14 | + |
| 15 | +class RiskSourceImportProcessor |
| 16 | +{ |
| 17 | + private Entity\User $connectedUser; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private RiskSourceTable $riskSourceTable, |
| 21 | + private ImportCacheHelper $importCacheHelper, |
| 22 | + ConnectedUserService $connectedUserService |
| 23 | + ) { |
| 24 | + /** @var Entity\User $connectedUser */ |
| 25 | + $connectedUser = $connectedUserService->getConnectedUser(); |
| 26 | + $this->connectedUser = $connectedUser; |
| 27 | + } |
| 28 | + |
| 29 | + public function processRiskSourcesData(Entity\Anr $anr, array $riskSourcesData): void |
| 30 | + { |
| 31 | + foreach ($riskSourcesData as $riskSourceData) { |
| 32 | + $this->processRiskSourceData($anr, $riskSourceData); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function processRiskSourceData(Entity\Anr $anr, ?array $riskSourceData): ?Entity\RiskSource |
| 37 | + { |
| 38 | + $label = trim((string)($riskSourceData['label'] ?? '')); |
| 39 | + if ($label === '') { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + $normalizedLabel = mb_strtolower($label); |
| 44 | + $riskSource = $this->getRiskSourceFromCache($anr, $normalizedLabel); |
| 45 | + if ($riskSource === null) { |
| 46 | + $riskSource = (new Entity\RiskSource()) |
| 47 | + ->setAnr($anr) |
| 48 | + ->setLabel($label) |
| 49 | + ->setIsDefault((bool)($riskSourceData['isDefault'] ?? false)) |
| 50 | + ->setIsActive((bool)($riskSourceData['isActive'] ?? true)) |
| 51 | + ->setCreator($this->connectedUser->getEmail()); |
| 52 | + |
| 53 | + $this->riskSourceTable->save($riskSource, false); |
| 54 | + $this->importCacheHelper->addItemToArrayCache('risk_sources_by_label', $riskSource, $normalizedLabel); |
| 55 | + |
| 56 | + return $riskSource; |
| 57 | + } |
| 58 | + |
| 59 | + $isUpdated = false; |
| 60 | + if (array_key_exists('isDefault', $riskSourceData) |
| 61 | + && $riskSource->isDefault() !== (bool)$riskSourceData['isDefault'] |
| 62 | + ) { |
| 63 | + $riskSource->setIsDefault((bool)$riskSourceData['isDefault']); |
| 64 | + $isUpdated = true; |
| 65 | + } |
| 66 | + if (array_key_exists('isActive', $riskSourceData) |
| 67 | + && $riskSource->isActive() !== (bool)$riskSourceData['isActive'] |
| 68 | + ) { |
| 69 | + $riskSource->setIsActive((bool)$riskSourceData['isActive']); |
| 70 | + $isUpdated = true; |
| 71 | + } |
| 72 | + |
| 73 | + if ($isUpdated) { |
| 74 | + $this->riskSourceTable->save($riskSource, false); |
| 75 | + } |
| 76 | + |
| 77 | + return $riskSource; |
| 78 | + } |
| 79 | + |
| 80 | + private function getRiskSourceFromCache(Entity\Anr $anr, string $normalizedLabel): ?Entity\RiskSource |
| 81 | + { |
| 82 | + if (!$this->importCacheHelper->isCacheKeySet('is_risk_sources_cache_loaded')) { |
| 83 | + $this->importCacheHelper->setArrayCacheValue('is_risk_sources_cache_loaded', true); |
| 84 | + /** @var Entity\RiskSource $riskSource */ |
| 85 | + foreach ($this->riskSourceTable->findByAnr($anr) as $riskSource) { |
| 86 | + $this->importCacheHelper->addItemToArrayCache( |
| 87 | + 'risk_sources_by_label', |
| 88 | + $riskSource, |
| 89 | + mb_strtolower($riskSource->getLabel()) |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $this->importCacheHelper->getItemFromArrayCache('risk_sources_by_label', $normalizedLabel); |
| 95 | + } |
| 96 | +} |
0 commit comments