Fifree2configuratorexportCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 82.69%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 13
eloc 53
c 2
b 0
f 2
dl 0
loc 102
ccs 43
cts 52
cp 0.8269
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 21 4
A export() 0 13 3
A exportEntity() 0 20 2
A configure() 0 6 1
A exportEntityToFile() 0 22 3
1
<?php
2
3
namespace Fi\CoreBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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
11
class Fifree2configuratorexportCommand extends ContainerAwareCommand
12
{
13
14
    private $entities = array();
15
    private $em;
16
    private $systementity;
17
    private $output;
18
19 6
    protected function configure()
20
    {
21
        $this
22 6
                ->setName('fifree2:configuratorexport')
23 6
                ->setDescription('Configuratore per Fifree')
24 6
                ->setHelp('Esporta la configurazione di fifree');
25 6
    }
26
27
    /**
28
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
29
     */
30 1
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32 1
        $fs = new Filesystem;
33 1
        $this->em = $this->getContainer()->get("doctrine")->getManager();
34 1
        $this->systementity = $this->getContainer()->get("ficorebundle.entity.system");
35 1
        $this->output = $output;
36
37
        try {
38
            //$fixturefile = $this->getContainer()->get('kernel')->locateResource('@FiCoreBundle/Resources/config/fixtures.yml');
39 1
            $fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml";
40 1
            $fs->remove($fixturefile);
41 1
            $systementities = $this->systementity->getSystemEntities();
0 ignored issues
show
Bug introduced by
The method getSystemEntities() does not exist on null. ( Ignorable by Annotation )

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

41
            /** @scrutinizer ignore-call */ 
42
            $systementities = $this->systementity->getSystemEntities();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42 1
            foreach ($systementities as $entity => $details) {
43 1
                $ret = $this->export($fixturefile, $entity);
44 1
                if ($ret == 1) {
45
                    return 1;
46
                }
47
            }
48 1
            return 0;
49
        } catch (\Exception $exc) {
50
            echo $exc->getMessage() . " at line " . $exc->getLine();
51
        }
52
    }
53
54 1
    protected function export($fixturefile, $entity)
55
    {
56 1
        $entityclass = $entity;
57 1
        $ret = $this->exportEntity($fixturefile, $entityclass);
58 1
        if ($ret == 0) {
59 1
            foreach ($this->entities as $entity) {
0 ignored issues
show
introduced by
$entity is overwriting one of the parameters of this function.
Loading history...
60
                $this->output->writeln("<info>Esporto " . $entity . " su file</info>");
61
                $this->exportEntityToFile($fixturefile, $entity);
62
            }
63 1
            $this->exportEntityToFile($fixturefile, $entityclass);
64 1
            return 0;
65
        }
66
        return 1;
67
    }
68
69 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

69
    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...
70
    {
71 1
        $entityutility = $this->getContainer()->get("ficorebundle.entity.utility");
72 1
        $this->output->writeln("<info>Export Entity: " . $entityclass . "</info>");
73 1
        if ($entityutility->entityExists($entityclass)) {
74
            /* $hasEntityCollegate = $entityutility->entityHasJoinTables($entityclass);
75
              if ($hasEntityCollegate) {
76
              $this->output->writeln("<info>Entity " . $entityclass . " ha tabelle in join</info>");
77
              $entityCollegata = $entityutility->getEntityJoinTables($entityclass);
78
              foreach ($entityCollegata as $key => $tabella) {
79
              $this->entities[] = $key;
80
              $this->output->writeln("<info>Prima esporto " . $key . " -> " . $tabella["entity"]["fieldName"] . "</info>");
81
              $this->exportEntity($fixturefile, $key);
82
              }
83
              } */
84
        } else {
85
            $this->output->writeln("<error>Entity not found: " . $entityclass . " </error>");
86
            return 1;
87
        }
88 1
        return 0;
89
    }
90
91 1
    private function exportEntityToFile($fixturefile, $entityclass)
92
    {
93 1
        $entityDump = array();
94
95
96 1
        $query = $this->em->createQueryBuilder()
97 1
                ->select('p')
98 1
                ->from($entityclass, 'p')
99 1
                ->orderBy("p.id", "asc")
100 1
                ->getQuery()
101
        ;
102 1
        $repo = $query->getArrayResult();
103
104
105
        //$repo = $this->em->getRepository($entityclass)->findAll();
106 1
        $this->output->writeln("<info>Trovate " . count($repo) . " records per l'entity " . $entityclass . "</info>");
107 1
        foreach ($repo as $row) {
108 1
            $entityDump[$entityclass][] = $row;
109
        }
110 1
        if (count($entityDump) > 0) {
111 1
            $yml = Yaml::dump($entityDump);
112 1
            file_put_contents($fixturefile, $yml, FILE_APPEND);
113
        }
114 1
    }
115
}
116