AbstractGenerator::traiterLeFichier()   F
last analyzed

Complexity

Conditions 18
Paths 1602

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
rs 2.5878
cc 18
eloc 32
nc 1602
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Starkerxp\StructureBundle\Generator;
4
5
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
6
use Symfony\Component\HttpKernel\KernelInterface;
7
use Symfony\Component\Yaml\Yaml;
8
9
abstract class AbstractGenerator extends Generator
10
{
11
    /**
12
     * @var KernelInterface
13
     */
14
    protected $kernel;
15
16
    /**
17
     * AbstractGenerator constructor.
18
     * @param KernelInterface $kernel
19
     */
20
    public function __construct(KernelInterface $kernel)
21
    {
22
        $this->kernel = $kernel;
23
    }
24
25
    /**
26
     * @param KernelInterface $kernel
27
     * @return AbstractGenerator
28
     */
29
    public function setKernel(KernelInterface $kernel)
30
    {
31
        $this->kernel = $kernel;
32
33
        return $this;
34
    }
35
36
    protected function traiterLesFichiers($bundle, $parameters, $search, $replace)
37
    {
38
        foreach ($this->getFichiers() as $template) {
39
            try {
40
                $this->kernel->locateResource("@StarkerxpStructureBundle/Resources/views/Gabarit/".$template.".twig");
41
            } catch (\InvalidArgumentException $e) {
42
                throw new \InvalidArgumentException("Il manque un fichier de template ".$template);
43
            }
44
            $fichierACreerModifier = $bundle->getPath().str_replace(
45
                    $search,
46
                    $replace,
47
                    $template
48
                );
49
            $this->traiterLeFichier($template, $fichierACreerModifier, $parameters);
50
        }
51
    }
52
53
    public abstract function getFichiers();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
54
55
    public function traiterLeFichier($fichier, $target, $parameters)
56
    {
57
        if (file_exists($target) && explode('.', basename($target))[1] == 'yml') {
58
            $currentServices = Yaml::parse(file_get_contents($target));
59
            $newServices = Yaml::parse($this->render($fichier.'.twig', $parameters));
60
            // gestion de fichiers sans services ni parameters => routing
61
            $listeDesParametres = !empty($currentServices['parameters']) ? array_keys($currentServices['parameters']) : [];
62
            $listeDesServices = !empty($currentServices['services']) ? array_keys($currentServices['services']) : [];
63
            $listeDesDeclarations = !empty($currentServices) ? array_keys($currentServices) : [];
64
            $listeDesNouveauxParametres = !empty($newServices['parameters']) ? array_keys($newServices['parameters']) : [];
65
            $listeDesNouveauxServices = !empty($newServices['services']) ? array_keys($newServices['services']) : [];
66
            $listeDesNouvellesDeclarations = !empty($newServices) ? array_keys($newServices) : [];
67
68
            $diffServices = array_diff($listeDesNouveauxServices, $listeDesServices);
69
            $diffParametres = array_diff($listeDesNouveauxParametres, $listeDesParametres);
70
            $diffDeclarations = array_diff($listeDesNouvellesDeclarations, $listeDesDeclarations);
71
            if (empty($diffServices) && empty($diffParametres) && empty($diffDeclarations)) {
72
                return false;
73
            }
74
            foreach ($diffServices as $libelle) {
75
                $currentServices['services'][$libelle] = $newServices['services'][$libelle];
76
            }
77
            foreach ($diffParametres as $libelle) {
78
                $currentServices['parameters'][$libelle] = $newServices['parameters'][$libelle];
79
            }
80
            foreach ($diffDeclarations as $libelle) {
81
                $currentServices[$libelle] = $newServices[$libelle];
82
            }
83
            $content = Yaml::dump($currentServices, 3, 4);
84
            $flink = fopen($target, 'w');
85
            if ($flink) {
86
                $write = fwrite($flink, $content);
87
                if ($write) {
88
                    fclose($flink);
89
                }
90
            }
91
92
            return true;
93
        }
94
        // On ne modifie pas un fichier existant.
95
        if (file_exists($target)) {
96
            return false;
97
        }
98
        $this->renderFile($fichier.'.twig', $target, $parameters);
99
100
        return true;
101
    }
102
103
    protected function genererParameters($clef, $controller)
104
    {
105
        $controller = explode(':', $controller);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $controller. This often makes code more readable.
Loading history...
106
        $bundle = $this->kernel->getBundle($controller[0]);
107
        $libelle = ucfirst($controller[1]);
108
109
        $clef = ucfirst(strtolower($clef));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $clef. This often makes code more readable.
Loading history...
110
        $parameters = [
111
            'nom'.$clef => $libelle,
112
            'nom'.$clef.'Camelize' => preg_replace('#\B([A-Z])#', '_\1', $libelle),
113
            'namespace'.$clef => $bundle->getNamespace(),
114
            'namespace'.$clef.'Bundle' => '@'.$bundle->getName(),
115
            'namespace'.$clef.'FQC' => str_replace('\\', '\\\\', $bundle->getNamespace()),
116
        ];
117
        $parameters['nomService'.$clef] = strtolower(
118
            str_replace(['_Bundle', '@'], '', preg_replace('#\B([A-Z])#', '_\1', $parameters['namespace'.$clef.'Bundle']))
119
        );
120
121
        return $parameters;
122
123
    }
124
125
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
126