Completed
Push — master ( 7ca800...81e3aa )
by Andrew
12s
created

DoctrineEntityManagerResetter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 47.62%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 45
ccs 10
cts 21
cp 0.4762
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reset() 0 10 2
A resetLogger() 0 13 4
1
<?php
2
3
namespace AndrewCarterUK\NoMoreLeaksBundle\Resetter;
4
5
use AndrewCarterUK\NoMoreLeaksBundle\Util;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\DBAL\Logging\LoggerChain;
9
10
class DoctrineEntityManagerResetter implements ResetterInterface
11
{
12
    /**
13
     * @var EntityManager
14
     */
15
    private $entityManager;
16
17
    /**
18
     * Constructor.
19
     * 
20
     * @param EntityManager $entityManager
21
     */
22 1
    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...
23
    {
24 1
        $this->entityManager = $entityManager;
25 1
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 1
    public function reset()
31
    {
32 1
        $this->entityManager->clear();
33
34 1
        $sqlLogger = $this->entityManager->getConnection()->getConfiguration()->getSQLLogger();
35
36 1
        if (null !== $sqlLogger) {
37
            $this->resetLogger($sqlLogger);
38
        }
39 1
    }
40
41 1
    private function resetLogger($sqlLogger)
42
    {
43
        if ($sqlLogger instanceof DebugStack) {
44
            $sqlLogger->queries = array();
45
            $sqlLogger->currentQuery = 0;
46
        } elseif ($sqlLogger instanceof LoggerChain) {
47 1
            $sqlLoggers = Util::readProperty($sqlLogger, 'loggers');
48
49
            foreach ($sqlLoggers as $sqlLogger) {
50
                $this->resetLogger($sqlLogger);
51
            }
52
        }
53
    }
54
}
55