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

DoctrineEntityManagerResetter::resetLogger()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 12.7639

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 2
cts 11
cp 0.1818
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 12.7639
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