BiCoreBundleConfiguratorexportCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 114
ccs 45
cts 54
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A exportEntity() 0 21 2
A execute() 0 22 4
A export() 0 15 3
A exportEntityToFile() 0 20 3
A configure() 0 5 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Yaml\Yaml;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Cdf\BiCoreBundle\Utils\Entity\BiCoreSystemTablesUtils;
12
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils;
13
14
class BiCoreBundleConfiguratorexportCommand extends Command
15
{
16
    protected static $defaultName = 'bicorebundle:configuratorexport';
17
18
    private $entities = array();
19
    private $em;
20
    private $systementity;
21
    private $output;
22
    private $entityutility;
23
24 1
    protected function configure()
25
    {
26 1
        $this
27 1
                ->setDescription('Esporta configurazione per BiCore')
28 1
                ->setHelp('Esporta la configurazione di bi');
29
    }
30
31 1
    public function __construct(EntityManagerInterface $em, EntityUtils $entityutility, BiCoreSystemTablesUtils $systementity)
32
    {
33 1
        $this->em = $em;
34 1
        $this->entityutility = $entityutility;
35 1
        $this->systementity = $systementity;
36
37
        // you *must* call the parent constructor
38 1
        parent::__construct();
39
    }
40
41
    /**
42
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
43
     */
44 1
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46 1
        $fs = new Filesystem();
47 1
        $this->output = $output;
48
49
        try {
50
            //$fixturefile = $this->getContainer()->get('kernel')->locateResource('@BiCoreBundle/Resources/config/fixtures.yml');
51 1
            $fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'fixtures.yml';
52 1
            $fs->remove($fixturefile);
53 1
            $systementities = $this->systementity->getSystemEntities();
54 1
            foreach ($systementities as $entity => $details) {
55 1
                $ret = $this->export($fixturefile, $entity);
56 1
                if (1 == $ret) {
57
                    return 1;
58
                }
59
            }
60
61 1
            return 0;
62
        } catch (\Exception $exc) {
63
            echo $exc->getMessage() . ' at line ' . $exc->getLine();
64
        }
65
        return 0;
66
    }
67
68 1
    protected function export($fixturefile, $entity)
69
    {
70 1
        $entityclass = $entity;
71 1
        $ret = $this->exportEntity($fixturefile, $entityclass);
72 1
        if (0 == $ret) {
73 1
            foreach ($this->entities as $entity) {
0 ignored issues
show
introduced by
$entity is overwriting one of the parameters of this function.
Loading history...
74
                $this->output->writeln('<info>Esporto ' . $entity . ' su file</info>');
75
                $this->exportEntityToFile($fixturefile, $entity);
76
            }
77 1
            $this->exportEntityToFile($fixturefile, $entityclass);
78
79 1
            return 0;
80
        }
81
82
        return 1;
83
    }
84
85 1
    private function exportEntity($fixturefile, $entityclass)
0 ignored issues
show
Unused Code introduced by
The parameter $fixturefile is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    private function exportEntity(/** @scrutinizer ignore-unused */ $fixturefile, $entityclass)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87 1
        $this->output->writeln('<info>Export Entity: ' . $entityclass . '</info>');
88 1
        if ($this->entityutility->entityExists($entityclass)) {
89
            /* $hasEntityCollegate = $entityutility->entityHasJoinTables($entityclass);
90
              if ($hasEntityCollegate) {
91
              $this->output->writeln("<info>Entity " . $entityclass . " ha tabelle in join</info>");
92
              $entityCollegata = $entityutility->getEntityJoinTables($entityclass);
93
              foreach ($entityCollegata as $key => $tabella) {
94
              $this->entities[] = $key;
95
              $this->output->writeln("<info>Prima esporto " . $key . " -> " . $tabella["entity"]["fieldName"] . "</info>");
96
              $this->exportEntity($fixturefile, $key);
97
              }
98
              } */
99
        } else {
100
            $this->output->writeln('<error>Entity not found: ' . $entityclass . ' </error>');
101
102
            return 1;
103
        }
104
105 1
        return 0;
106
    }
107
108 1
    private function exportEntityToFile($fixturefile, $entityclass)
109
    {
110 1
        $entityDump = array();
111
112 1
        $query = $this->em->createQueryBuilder()
113 1
                ->select('p')
114 1
                ->from($entityclass, 'p')
115 1
                ->orderBy('p.id', 'asc')
116 1
                ->getQuery()
117 1
        ;
118 1
        $repo = $query->getArrayResult();
119
120
        //$repo = $this->em->getRepository($entityclass)->findAll();
121 1
        $this->output->writeln('<info>Trovate ' . count($repo) . " records per l'entity " . $entityclass . '</info>');
122 1
        foreach ($repo as $row) {
123 1
            $entityDump[$entityclass][] = $row;
124
        }
125 1
        if (count($entityDump) > 0) {
126 1
            $yml = Yaml::dump($entityDump);
127 1
            file_put_contents($fixturefile, $yml, FILE_APPEND);
128
        }
129
    }
130
}
131