DoctrineCommand   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getEntityManager() 0 4 1
1
<?php namespace Nord\Lumen\Doctrine\ORM\Console;
2
3
use Doctrine\ORM\EntityManager;
4
use Illuminate\Console\Command;
5
6
abstract class DoctrineCommand extends Command
7
{
8
9
    /**
10
     * @var EntityManager
11
     */
12
    private $entityManager;
13
14
15
    /**
16
     * DoctrineCommand constructor.
17
     *
18
     * @param EntityManager $entityManager
19
     */
20
    public function __construct(EntityManager $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...
21
    {
22
        parent::__construct();
23
24
        $this->entityManager = $entityManager;
25
    }
26
27
28
    /**
29
     * @return EntityManager
30
     */
31
    protected function getEntityManager()
32
    {
33
        return $this->entityManager;
34
    }
35
}
36