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

ControllerGenerator::generate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 27

Duplication

Lines 9
Ratio 23.68 %

Importance

Changes 0
Metric Value
dl 9
loc 38
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 27
nc 3
nop 2
1
<?php
2
3
namespace Starkerxp\StructureBundle\Generator;
4
5
6
use Doctrine\ORM\EntityManager;
7
8
class ControllerGenerator extends AbstractGenerator
9
{
10
    /**
11
     * @var EntityManager
12
     */
13
    protected $entityManager;
14
15
    public function generate($controller, $entite)
16
    {
17
        // On vérifie que l'entite existe sinon une exception est levé
18
        $this->entityManager->getClassMetadata($entite);
19
20
        $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...
21
        $bundle = $this->kernel->getBundle($controller[0]);
22
        $libelle = ucfirst($controller[1]);
23
24
        $entite = explode(':', $entite);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $entite. This often makes code more readable.
Loading history...
25
        $bundleEntite = $this->kernel->getBundle($entite[0]);
26
        $libelleEntite = ucfirst($entite[1]);
27
        $parameters = [
28
            'nomController'             => $libelle,
29
            'nomControllerCamelize'     => preg_replace('#\B([A-Z])#', '_\1', $libelle),
30
            'namespaceController'       => $bundle->getNamespace(),
31
            'namespaceControllerBundle' => '@'.$bundle->getName(),
32
            'namespaceControllerFQC'    => str_replace('\\', '\\\\', $bundle->getNamespace()),
33
34
            'nomEntity'             => $libelleEntite,
35
            'namespaceEntity'       => $bundleEntite->getNamespace(),
36
            'namespaceEntityBundle' => '@'.$bundleEntite->getName(),
37
            'namespaceEntityFQC'    => str_replace('\\', '\\\\', $bundleEntite->getNamespace()),
38
        ];
39
40
        $parameters['nomService'] = strtolower(
41
            str_replace(['_Bundle', '@'], '', preg_replace('#\B([A-Z])#', '_\1', $parameters['namespaceBundle']))
42
        );
43 View Code Duplication
        foreach ($this->getFichiers() as $template) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            try {
45
                $this->kernel->locateResource("@StarkerxpStructureBundle/Resources/views/Gabarit/".$template.".twig");
46
            } catch (\InvalidArgumentException $e) {
47
                throw new \InvalidArgumentException('Il manque un fichier de template');
48
            }
49
            $fichierACreerModifier = $bundle->getPath().str_replace("_nomController_", $libelle, $template);
50
            $this->traiterLeFichier($template, $fichierACreerModifier, $parameters);
51
        }
52
    }
53
54
    public function getFichiers()
55
    {
56
        return [
57
            '/Controller/_nomController_Controller.php',
58
            '/Tests/Controller/_nomController_ControllerTest.php',
59
            '/Form/Type/_nomController_Type.php',
60
            '/Resources/config/routing.yml',  // Il faut récupérer la locale par défaut afin de générer le bon fichier.
61
            //'/Resources/translations/_lnomController_._defaultLocale_.yml',  // Il faut récupérer la locale par défaut afin de générer le bon fichier.
62
        ];
63
    }
64
65
    /**
66
     * @param EntityManager $entityManager
67
     */
68
    public function setEntityManager($entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
69
    {
70
        $this->entityManager = $entityManager;
71
    }
72
}
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...
73