Completed
Push — master ( b88100...c1b2a0 )
by Guillaume
02:38
created

AbstractGenerator::traiterLeFichier()   D

Complexity

Conditions 14
Paths 210

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 4.518
c 0
b 0
f 0
cc 14
eloc 27
nc 210
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\Bundle\Bundle;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Symfony\Component\Yaml\Yaml;
9
10
abstract class AbstractGenerator extends Generator
11
{
12
    /**
13
     * @var KernelInterface
14
     */
15
    protected $kernel;
16
17
    /**
18
     * AbstractGenerator constructor.
19
     * @param KernelInterface $kernel
20
     */
21
    public function __construct(KernelInterface $kernel)
22
    {
23
        $this->kernel = $kernel;
24
    }
25
26
    public abstract function getFichiers();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
27
28
    public function traiterLeFichier($fichier, $target, $parameters)
29
    {
30
        if (file_exists($target) && explode('.', basename($target))[1] == 'yml') {
31
            $currentServices = Yaml::parse(file_get_contents($target));
32
            $newServices = Yaml::parse($this->render($fichier.'.twig', $parameters));
33
            $listeDesParametres = !empty($currentServices['parameters'])?array_keys($currentServices['parameters']):[];
34
            $listeDesServices = !empty($currentServices['services'])?array_keys($currentServices['services']):[];
35
            $listeDesNouveauxParametres = !empty($newServices['parameters'])?array_keys($newServices['parameters']):[];
36
            $listeDesNouveauxServices = !empty($newServices['services'])?array_keys($newServices['services']):[];
37
            $diffServices = array_diff($listeDesNouveauxServices, $listeDesServices);
38
            $diffParametres = array_diff($listeDesNouveauxParametres, $listeDesParametres);
39
            if(empty($diffServices) && empty($diffParametres) ){
40
                return false;
41
            }
42
            foreach ($diffServices as $libelle){
43
                $currentServices['services'][$libelle] = $newServices['services'][$libelle];
44
            }
45
            foreach ($diffParametres as $libelle){
46
                $currentServices['parameters'][$libelle] = $newServices['parameters'][$libelle];
47
            }
48
            $content = Yaml::dump($currentServices, 3, 4);
49
            $flink = fopen($target, 'w');
50
            if ($flink) {
51
                $write = fwrite($flink, $content);
52
                if ($write) {
53
                    fclose($flink);
54
                }
55
            }
56
            return true;
57
        }
58
        // On ne modifie pas un fichier existant.
59
        if(file_exists($target)){
60
            return false;
61
        }
62
        $this->renderFile($fichier.'.twig', $target, $parameters);
63
        return true;
64
    }
65
66
    /**
67
     * @param KernelInterface $kernel
68
     * @return AbstractGenerator
69
     */
70
    public function setKernel(KernelInterface $kernel)
71
    {
72
        $this->kernel = $kernel;
73
74
        return $this;
75
    }
76
77
78
}
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...
79