DoctrineOptimizeListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iterate() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Event;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
18
/**
19
 * Doctrine context optimizer
20
 * Used to clear the doctrine context on each command iteration.
21
 * Do not use with doctrine backend, use DoctrineBackendOptimizeListener instead.
22
 *
23
 * @author Kevin Nedelec <[email protected]>
24
 */
25
class DoctrineOptimizeListener implements IterationListener
26
{
27
    /**
28
     * @var Registry
29
     */
30
    protected $doctrine;
31
32
    public function __construct(ManagerRegistry $doctrine)
33
    {
34
        $this->doctrine = $doctrine;
0 ignored issues
show
Documentation Bug introduced by
It seems like $doctrine of type object<Doctrine\Persistence\ManagerRegistry> is incompatible with the declared type object<Sonata\NotificationBundle\Event\Registry> of property $doctrine.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function iterate(IterateEvent $event): void
41
    {
42
        foreach ($this->doctrine->getManagers() as $name => $manager) {
43
            if (!$manager->isOpen()) {
44
                throw new \RuntimeException(sprintf('The doctrine manager: %s is closed', $name));
45
            }
46
47
            $manager->getUnitOfWork()->clear();
48
        }
49
    }
50
}
51