Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ImportReferentielCommand | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
56 | |
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 | |||
importReferentiel | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace App\Domain\Maturity\Command; |
4 | |
5 | use App\Domain\Maturity\Model\Referentiel; |
6 | use Doctrine\ORM\EntityManagerInterface; |
7 | use JMS\Serializer\SerializerBuilder; |
8 | use Symfony\Component\Console\Command\Command; |
9 | use Symfony\Component\Console\Input\InputArgument; |
10 | use Symfony\Component\Console\Input\InputInterface; |
11 | use Symfony\Component\Console\Output\OutputInterface; |
12 | use Symfony\Component\Console\Style\SymfonyStyle; |
13 | use Symfony\Component\Finder\Finder; |
14 | |
15 | class ImportReferentielCommand extends Command |
16 | { |
17 | protected static $defaultName = 'maturity:referentiel:import'; |
18 | protected static $defaultDescription = 'Import a maturity referentiel'; |
19 | |
20 | private SymfonyStyle $io; |
21 | private EntityManagerInterface $entityManager; |
22 | |
23 | public function __construct(EntityManagerInterface $entityManager) |
24 | { |
25 | parent::__construct(); |
26 | $this->entityManager = $entityManager; |
27 | } |
28 | |
29 | protected function configure(): void |
30 | { |
31 | $this |
32 | ->setDescription(self::$defaultDescription) |
33 | ->addArgument('folder', InputArgument::REQUIRED, 'Path to the folder containing files to import') |
34 | ; |
35 | } |
36 | |
37 | protected function execute(InputInterface $input, OutputInterface $output): int |
38 | { |
39 | $this->io = new SymfonyStyle($input, $output); |
40 | |
41 | $finder = new Finder(); |
42 | $finder->files()->in($input->getArgument('folder')); |
43 | |
44 | if ($finder->hasResults()) { |
45 | foreach ($finder as $file) { |
46 | $this->importReferentiel($file->getRealPath()); |
47 | } |
48 | } |
49 | |
50 | return Command::SUCCESS; |
51 | } |
52 | |
53 | protected function importReferentiel(string $file): int |
54 | { |
55 | $this->io->title('Importing Référentiel from ' . $file); |
56 | $content = file_get_contents($file); |
57 | $serializer = SerializerBuilder::create()->build(); |
58 | /** @var Referentiel $object */ |
59 | $object = $serializer->deserialize($content, Referentiel::class, 'xml'); |
60 | $object->deserialize(); |
61 | |
62 | $existing = $this->entityManager->getRepository(Referentiel::class)->findBy(['name' => $object->getName()]); |
63 | |
64 | if (count($existing)) { |
65 | $this->io->warning('Référentiel "' . $object->getName() . '" already exists'); |
66 | |
67 | return 0; |
68 | } |
69 | $object->setCreatedAt(new \DateTimeImmutable()); |
70 | $this->entityManager->persist($object); |
71 | $this->entityManager->flush(); |
72 | |
73 | $this->io->success('Référentiel "' . $object->getName() . '" imported'); |
74 | |
75 | return 0; |
76 | } |
77 | } |