Skip to content

Commit

Permalink
Some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanbaidan committed Oct 2, 2024
1 parent ed72ee3 commit a5b2203
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 64 deletions.
14 changes: 0 additions & 14 deletions src/Entity/RecommendationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,4 @@ public function getLabel(): string
{
return $this->label;
}

public static function getCustomImportLabelByLanguageCode(string $languageCode): string
{
switch ($languageCode) {
case 'fr':
return 'Recommandations importées';
case 'de':
return 'Importierte empfehlungen';
case 'nl':
return 'Geïmporteerde aanbevelingen';
default:
return 'Imported recommendations';
}
}
}
8 changes: 4 additions & 4 deletions src/Service/AnrInstanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ private function getOtherInstances(Entity\Instance $instance): array
$anr = $instance->getAnr();
$otherInstances = $this->instanceTable->findByAnrAndObject($anr, $instance->getObject());
$names = [
'name1' => $anr->getLabel(1),
'name2' => $anr->getLabel(2),
'name3' => $anr->getLabel(3),
'name4' => $anr->getLabel(4),
'name1' => $anr->getLabel(),
'name2' => $anr->getLabel(),
'name3' => $anr->getLabel(),
'name4' => $anr->getLabel(),
];
$otherInstancesData = [];
foreach ($otherInstances as $otherInstance) {
Expand Down
4 changes: 1 addition & 3 deletions src/Service/AnrModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Monarc\FrontOffice\Service;

use Monarc\Core\Service\ConfigService;
use Monarc\Core\Table as CoreTable;
use Monarc\Core\Model\Table as CoreDeprecatedTable;
use Monarc\Core\Entity as CoreEntity;
Expand All @@ -26,8 +25,7 @@ public function __construct(
private CoreTable\RolfRiskTable $coreRolfRiskTable,
private CoreTable\RolfTagTable $coreRolfTagTable,
private CoreDeprecatedTable\QuestionTable $coreQuestionTable,
private CoreDeprecatedTable\QuestionChoiceTable $coreQuestionChoiceTable,
private ConfigService $configService
private CoreDeprecatedTable\QuestionChoiceTable $coreQuestionChoiceTable
) {
}

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

/* Recreate AnrInstanceMetadataFields */
$anrInstanceMetadataFieldOldIdsToNewObjects = $this
->duplicateAnrMetadataInstanceFields($sourceAnr, $newAnr, $isSourceCommon,);
->duplicateAnrMetadataInstanceFields($sourceAnr, $newAnr, $isSourceCommon);

/* Recreate Instances, InstanceRisks, InstanceConsequences and InstanceMetadata. */
$this->duplicateInstancesTreeRisksSequencesRecommendationsMetadataAndScales(
Expand Down
36 changes: 14 additions & 22 deletions src/Service/DeliverableGenerationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private function buildRiskAssessmentValues()
]
]);

$values = array_merge_recursive(
return array_merge_recursive(
$values,
[
'xml' => [
Expand All @@ -501,8 +501,6 @@ private function buildRiskAssessmentValues()
],
]
);

return $values;
}

/**
Expand All @@ -511,14 +509,12 @@ private function buildRiskAssessmentValues()
*/
private function buildImplementationPlanValues()
{
$values = [
return [
'table' => [
'TABLE_IMPLEMENTATION_PLAN' => $this->generateTableImplementationPlan(),
'TABLE_IMPLEMENTATION_HISTORY' => $this->generateTableImplementationHistory(),
],
];

return $values;
}

/**
Expand Down Expand Up @@ -552,7 +548,7 @@ private function buildStatementOfAppplicabilityValues(string $referentialUuid, $
*/
private function buildRecordOfProcessingActivitiesValues($record)
{
$values = [
return [
'xml' => [
'TABLE_RECORD_INFORMATION' => $this->generateTableRecordGDPR($record),
'TABLE_RECORD_ACTORS' => $this->generateTableRecordActors($record),
Expand All @@ -562,8 +558,6 @@ private function buildRecordOfProcessingActivitiesValues($record)
'TABLE_RECORD_PROCESSORS' => $this->generateTableRecordProcessors($record),
],
];

return $values;
}

/**
Expand Down Expand Up @@ -629,7 +623,7 @@ private function generateInformationalRiskImpactsTable(Entity\Scale $impactScale
break;
}
}
if (\in_array($type, ScaleImpactTypeSuperClass::getScaleImpactTypesCid(), true)) {
if (in_array($type, ScaleImpactTypeSuperClass::getScaleImpactTypesCid(), true)) {
$table->addCell(PhpWord\Shared\Converter::cmToTwip(2.80), $this->restartAndTopCell)
->addText(_WT($commentText), $this->normalFont, $this->leftParagraph);
} else {
Expand Down Expand Up @@ -674,7 +668,7 @@ private function generateInformationalRiskAcceptanceThresholdsTable(
for ($t = $threatsScale->getMin(); $t <= $threatsScale->getMax(); ++$t) {
for ($v = $vulnsScale->getMin(); $v <= $vulnsScale->getMax(); ++$v) {
$prod = $t * $v;
if (!\in_array($prod, $header, true)) {
if (!in_array($prod, $header, true)) {
$header[] = $prod;
}
}
Expand Down Expand Up @@ -1292,7 +1286,7 @@ private function generateTableAudit()
$tableWord->addTitleStyle($i + 3, $this->titleFont);
}

if (\in_array('true', $global, true)) {
if (in_array('true', $global, true)) {
$section->addTitle($this->anrTranslate('Global assets'), 3);
}

Expand Down Expand Up @@ -1340,7 +1334,7 @@ private function generateTableAudit()
$table->addCell(PhpWord\Shared\Converter::cmToTwip(1.00), $this->continueAndBlackCell);
$table->addCell(PhpWord\Shared\Converter::cmToTwip(1.00), $this->continueAndBlackCell);
} else {
$treeNum = \count($data['tree']);
$treeNum = count($data['tree']);
for ($i = 0; $i < $treeNum; $i++) {
if ($i <= $maxLevelTitle - 1 && $title[$i] !== $data['tree'][$i]['id']) {
$section->addTitle(_WT($data['tree'][$i]['name' . $this->currentLangAnrIndex]), $i + 3);
Expand Down Expand Up @@ -1492,7 +1486,7 @@ private function generateTableAuditOp()
$instance = $operationalInstanceRisk->getInstance();
if (!isset($lst[$instance->getId()])) {
$ascendants = $instance->getHierarchyArray();
$levelTree = \count($ascendants);
$levelTree = count($ascendants);
if ($levelTree > $maxLevelDeep) {
$maxLevelDeep = $levelTree;
}
Expand Down Expand Up @@ -1641,7 +1635,7 @@ private function generateTableAuditOp()
$table->addCell(PhpWord\Shared\Converter::cmToTwip(10.00), $this->continueAndBlackCell);
if ($this->anr->showRolfBrut()) {
$table->addCell(PhpWord\Shared\Converter::cmToTwip(1.00), $this->restartAndBlackCell)
->addText($this->anrTranslate('Prob.'), $this->whiteFont,);
->addText($this->anrTranslate('Prob.'), $this->whiteFont);
$table->addCell(
PhpWord\Shared\Converter::cmToTwip($sizeCellImpact),
$this->setColSpanCell(count($opRisksImpactsScales), '444444')
Expand Down Expand Up @@ -1685,7 +1679,7 @@ private function generateTableAuditOp()
PhpWord\Shared\Converter::cmToTwip(0.70),
array_merge($this->rotate90TextCell, ['bgcolor' => '444444'])
)
->addText($label, $this->whiteFont,);
->addText($label, $this->whiteFont);
}
$table->addCell(PhpWord\Shared\Converter::cmToTwip(1.00), $this->continueAndBlackCell);
}
Expand Down Expand Up @@ -3219,7 +3213,7 @@ private function generateTableRecordActors($recordId)
->addText(_WT($jc->get('label')), $this->normalFont, $this->leftParagraph);
$table->addCell(PhpWord\Shared\Converter::cmToTwip(6.00), $this->vAlignCenterCell)
->addText(_WT($jc->get('contact')), $this->normalFont, $this->leftParagraph);
if ($i !== \count($jointControllers) - 1) {
if ($i !== count($jointControllers) - 1) {
$table->addRow(400);
$table->addCell(PhpWord\Shared\Converter::cmToTwip(6.00), $this->grayCell);
}
Expand Down Expand Up @@ -3960,8 +3954,8 @@ private function generateWordXmlFromHtml($input)
if (preg_match_all("'<ol>(.*?)</ol>'", $input, $groups)) {
foreach ($groups as $group) {
$index = 0;
while (strpos($group[0], '<li>') !== false) {
$index += 1;
while (str_contains($group[0], '<li>')) {
++$index;
$group[0] = preg_replace(
["'<li><!--block-->'", "'</li>'"],
["<!--block-->&nbsp;&nbsp;[$index]&nbsp;", '<!--block-->'],
Expand Down Expand Up @@ -4025,14 +4019,12 @@ private function singleLevelArray($multiLevelArray)
{
$singleLevelArray = [];
foreach ($multiLevelArray as $a) {
$singleLevelArray[] = $a;
if (isset($a['children'])) {
$singleLevelArray[] = $a;
$children_array = $this->singleLevelArray($a['children']);
foreach ($children_array as $children) {
$singleLevelArray[] = $children;
}
} else {
$singleLevelArray[] = $a;
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Service/OperationalRiskScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ public function updateLevelsNumberOfOperationalRiskScale(Entity\Anr $anr, array
foreach ($operationalRiskScaleComments as $operationalRiskScaleComment) {
if ($operationalRiskScaleComment->getScaleIndex() < $levelsNumber) {
$operationalRiskScaleComment->setIsHidden(false);
$this->operationalRiskScaleCommentTable->save($operationalRiskScaleComment, false);
} else {
$operationalRiskScaleComment->setIsHidden(true);
if ($operationalRiskScaleComment->getScaleValue() <= $maxScaleValue) {
$operationalRiskScaleComment->setScaleValue(++$maxScaleValue);
}
$this->operationalRiskScaleCommentTable->save($operationalRiskScaleComment, false);
}
$this->operationalRiskScaleCommentTable->save($operationalRiskScaleComment, false);
if ($operationalRiskScaleComment->getScaleValue() > $maxScaleValue) {
$maxScaleValue = $operationalRiskScaleComment->getScaleValue();
}
Expand Down
26 changes: 9 additions & 17 deletions src/Stats/Service/StatsAnrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Monarc\FrontOffice\Entity\SoaCategory;
use Monarc\FrontOffice\Entity\User;
use Monarc\FrontOffice\Entity\UserRole;
use Monarc\FrontOffice\Import\Traits\EvaluationConverterTrait;
use Monarc\FrontOffice\Table;
use Monarc\FrontOffice\Stats\DataObject\StatsDataObject;
use Monarc\FrontOffice\Stats\Exception\StatsAlreadyCollectedException;
Expand All @@ -31,6 +32,8 @@

class StatsAnrService
{
use EvaluationConverterTrait;

public const LOW_RISKS = 'Low risks';
public const MEDIUM_RISKS = 'Medium risks';
public const HIGH_RISKS = 'High risks';
Expand Down Expand Up @@ -1047,9 +1050,10 @@ private function formatCartographyStatsData(array $statsData): array
$minProbability = empty($data['scales']['probability']) ? 0 : min($data['scales']['probability']);

foreach ($scalesImpact as $impactValue) {
$y = $this->approximate($impactValue, $minImpact, $maxImpact, 0, 4);
$y = $this->convertValueWithinNewScalesRange($impactValue, $minImpact, $maxImpact, 0, 4);
foreach ($data['scales']['likelihood'] as $likelihoodValue) {
$x = $this->approximate($likelihoodValue, $minLikelihood, $maxLikelihood, 0, 20);
$x = $this
->convertValueWithinNewScalesRange($likelihoodValue, $minLikelihood, $maxLikelihood, 0, 20);
$seriesKey = $y . $x;
if (!isset($formattedResult[$anrUuid]['informational']['currentInfo']['series'][$seriesKey])) {
$formattedResult[$anrUuid]['informational']['currentInfo']['series'][$seriesKey] = [
Expand Down Expand Up @@ -1077,7 +1081,8 @@ private function formatCartographyStatsData(array $statsData): array
}

foreach ($data['scales']['probability'] as $probabilityValue) {
$x = $this->approximate($probabilityValue, $minProbability, $maxProbability, 0, 4);
$x = $this
->convertValueWithinNewScalesRange($probabilityValue, $minProbability, $maxProbability, 0, 4);
$seriesKey = $y . $x;
if (!isset($formattedResult[$anrUuid]['operational']['currentOp']['series'][$seriesKey])) {
$formattedResult[$anrUuid]['operational']['currentOp']['series'][$seriesKey] = [
Expand Down Expand Up @@ -1137,7 +1142,7 @@ private function formatCartographyStatsData(array $statsData): array
});
}
unset($value);
foreach ($formattedResult['operational'] as $key => &$value) {
foreach ($formattedResult['operational'] as &$value) {
usort($value, static function ($a, $b) {
return $a['category'] <=> $b['category'];
});
Expand All @@ -1147,19 +1152,6 @@ private function formatCartographyStatsData(array $statsData): array
return $formattedResult;
}

private function approximate($x, $minorig, $maxorig, $mindest, $maxdest, $defaultvalue = -1)
{
if ($x === $maxorig) {
return $maxdest;
}

if ($x !== -1 && ($maxorig - $minorig) !== -1) {
return min(max(round(($x / ($maxorig - $minorig + 1)) * ($maxdest - $mindest + 1)), $mindest), $maxdest);
}

return $defaultvalue;
}

/**
* @param StatsDataObject[] $statsData
*
Expand Down
7 changes: 6 additions & 1 deletion src/Validator/FieldValidator/AnrExistenceValidator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

namespace Monarc\FrontOffice\Validator\FieldValidator;

Expand All @@ -24,7 +29,7 @@ public function isValid($value)
$anrTable = $this->getOption('anrTable');
try {
$anrTable->findById((int)($value['id'] ?? $value));
} catch (EntityNotFoundException $e) {
} catch (EntityNotFoundException) {
$this->error(self::ANR_DOES_NOT_EXIST, $value);

return false;
Expand Down

0 comments on commit a5b2203

Please sign in to comment.