ControllerGenerator::setEntityManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Starkerxp\StructureBundle\Generator;
4
5
6
use Doctrine\ORM\EntityManager;
7
8
9
class ControllerGenerator extends AbstractGenerator
10
{
11
    /**
12
     * @var EntityManager
13
     */
14
    protected $entityManager;
15
16
    /**
17
     * @var string
18
     */
19
    protected $locale;
20
21
    public function generate($controller, $entite)
22
    {
23
        // On vérifie que l'entite existe sinon une exception est levé
24
        $this->entityManager->getClassMetadata($entite);
25
26
        $bundle = $this->kernel->getBundle(explode(':', $controller)[0]);
27
        $libelle = ucfirst(explode(':', $controller)[1]);
28
29
        $parametersController = $this->genererParameters("Controller", $controller);
30
        $parametersEntity = $this->genererParameters("Entity", $entite);
31
        $parameters = array_merge($parametersController, $parametersEntity);
32
        $this->traiterLesFichiers($bundle, $parameters, ["_nomController_", "_lnomController_", "_locale_"], [$libelle, strtolower($libelle), $this->locale]);
33
    }
34
35
36
    public function getFichiers()
37
    {
38
        return [
39
            '/Controller/_nomController_Controller.php',
40
            '/Tests/Controller/_nomController_ControllerTest.php',
41
            '/Form/Type/_nomController_Type.php',
42
            '/Resources/config/routing.yml',  // Il faut récupérer la locale par défaut afin de générer le bon fichier.
43
            '/Resources/translations/_lnomController_._locale_.yml',  // Il faut récupérer la locale par défaut afin de générer le bon fichier.
44
        ];
45
    }
46
47
    /**
48
     * @param EntityManager $entityManager
49
     *
50
     *
51
     */
52
    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...
53
    {
54
        $this->entityManager = $entityManager;
55
    }
56
57
    /**
58
     * @param string $locale
59
     */
60
    public function setLocale($locale)
61
    {
62
        $this->locale = $locale;
63
    }
64
65
}
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...
66