Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ImportModelCommand | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
configure | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
importAIPDModel | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace App\Domain\AIPD\Command; |
4 | |
5 | use App\Domain\AIPD\Model\ModeleAnalyse; |
6 | use App\Domain\AIPD\Model\ModeleScenarioMenace; |
7 | use Doctrine\ORM\EntityManagerInterface; |
8 | use JMS\Serializer\SerializerBuilder; |
9 | use Symfony\Component\Console\Command\Command; |
10 | use Symfony\Component\Console\Input\InputArgument; |
11 | use Symfony\Component\Console\Input\InputInterface; |
12 | use Symfony\Component\Console\Output\OutputInterface; |
13 | use Symfony\Component\Console\Style\SymfonyStyle; |
14 | use Symfony\Component\Finder\Finder; |
15 | |
16 | class ImportModelCommand extends Command |
17 | { |
18 | protected static $defaultName = 'aipd:model:import'; |
19 | protected static $defaultDescription = 'Import an AIPD model'; |
20 | |
21 | private SymfonyStyle $io; |
22 | private EntityManagerInterface $entityManager; |
23 | |
24 | public function __construct(EntityManagerInterface $entityManager) |
25 | { |
26 | parent::__construct(); |
27 | $this->entityManager = $entityManager; |
28 | } |
29 | |
30 | protected function configure(): void |
31 | { |
32 | $this |
33 | ->setDescription(self::$defaultDescription) |
34 | ->addArgument('folder', InputArgument::REQUIRED, 'Path to the folder containing files to import') |
35 | ; |
36 | } |
37 | |
38 | protected function execute(InputInterface $input, OutputInterface $output): int |
39 | { |
40 | $this->io = new SymfonyStyle($input, $output); |
41 | |
42 | $finder = new Finder(); |
43 | $finder->files()->in($input->getArgument('folder')); |
44 | |
45 | if ($finder->hasResults()) { |
46 | foreach ($finder as $file) { |
47 | $this->importAIPDModel($file->getRealPath()); |
48 | } |
49 | } |
50 | |
51 | return Command::SUCCESS; |
52 | } |
53 | |
54 | protected function importAIPDModel(string $file): int |
55 | { |
56 | $this->io->title('Importing AIPD model from ' . $file); |
57 | $content = file_get_contents($file); |
58 | $serializer = SerializerBuilder::create()->build(); |
59 | /** @var ModeleAnalyse $object */ |
60 | $object = $serializer->deserialize($content, ModeleAnalyse::class, 'xml'); |
61 | $object->deserialize(); |
62 | |
63 | $existing = $this->entityManager->getRepository(ModeleAnalyse::class)->findBy(['nom' => $object->getNom(), 'description' => $object->getDescription()]); |
64 | |
65 | if (count($existing)) { |
66 | $this->io->warning('AIPD model "' . $object->getNom() . '" already exists'); |
67 | |
68 | return 0; |
69 | } |
70 | |
71 | $sm = []; |
72 | foreach ($object->getScenarioMenaces() as $scenarioMenace) { |
73 | $this->io->writeln('Importing scenario menace ' . $scenarioMenace->getNom()); |
74 | /** @var ModeleScenarioMenace $scenarioMenace */ |
75 | $mesures = []; |
76 | foreach ($scenarioMenace->getMesuresProtections() as $mesureProtection) { |
77 | $this->io->writeln('Importing $mesureProtection ' . $mesureProtection->getNom()); |
78 | // Check if this mesure already exists |
79 | $mm = $this->entityManager->find(\App\Domain\AIPD\Model\ModeleMesureProtection::class, $mesureProtection->getId()); |
80 | if ($mm) { |
81 | $mesures[] = $mm; |
82 | } else { |
83 | // If not, save it now |
84 | $this->entityManager->persist($mesureProtection); |
85 | $mesures[] = $mesureProtection; |
86 | } |
87 | } |
88 | $scenarioMenace->setMesuresProtections($mesures); |
89 | $sm[] = $scenarioMenace; |
90 | } |
91 | |
92 | $object->setScenarioMenaces($sm); |
93 | $object->setCreatedAt(new \DateTimeImmutable()); |
94 | $this->entityManager->persist($object); |
95 | $this->entityManager->flush(); |
96 | |
97 | $this->io->success('AIPD model imported'); |
98 | |
99 | return 0; |
100 | } |
101 | } |