DoctrinePHPCRODMListenerContainerAware   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 7 1
A preRemove() 0 6 1
A preUpdate() 0 6 1
A load() 0 8 2
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\CacheBundle\Invalidation;
15
16
use Doctrine\Common\EventSubscriber;
17
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
18
use Doctrine\ODM\PHPCR\Event;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
class DoctrinePHPCRODMListenerContainerAware implements EventSubscriber
22
{
23
    protected $listener;
24
25
    protected $service;
26
27
    protected $container;
28
29
    public function __construct(ContainerInterface $container, string $service)
30
    {
31
        $this->container = $container;
32
        $this->service = $service;
33
    }
34
35
    public function getSubscribedEvents(): array
36
    {
37
        return [
38
            Event::preRemove,
39
            Event::preUpdate,
40
        ];
41
    }
42
43
    public function preRemove(LifecycleEventArgs $args): void
44
    {
45
        $this->load();
46
47
        $this->listener->preRemove($args);
48
    }
49
50
    public function preUpdate(LifecycleEventArgs $args): void
51
    {
52
        $this->load();
53
54
        $this->listener->preUpdate($args);
55
    }
56
57
    private function load(): void
58
    {
59
        if ($this->listener) {
60
            return;
61
        }
62
63
        $this->listener = $this->container->get($this->service);
64
    }
65
}
66