Completed
Push — 2.x-dev-kit ( 3d52ae )
by
unknown
09:33
created

DoctrinePHPCRODMListenerContainerAware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 6
c 4
b 1
f 1
lcom 1
cbo 0
dl 0
loc 56
rs 10

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
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\CacheBundle\Invalidation;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
16
use Doctrine\ODM\PHPCR\Event;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
class DoctrinePHPCRODMListenerContainerAware implements EventSubscriber
20
{
21
    protected $listener;
22
23
    protected $service;
24
25
    /**
26
     * @param ContainerInterface $container
27
     * @param $service
28
     */
29
    public function __construct(ContainerInterface $container, $service)
30
    {
31
        $this->container = $container;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->service = $service;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getSubscribedEvents()
39
    {
40
        return array(
41
            Event::preRemove,
42
            Event::preUpdate,
43
        );
44
    }
45
46
    /**
47
     * @param $args
48
     */
49
    public function preRemove(LifecycleEventArgs $args)
50
    {
51
        $this->load();
52
53
        $this->listener->preRemove($args);
54
    }
55
56
    /**
57
     * @param LifecycleEventArgs $args
58
     */
59
    public function preUpdate(LifecycleEventArgs $args)
60
    {
61
        $this->load();
62
63
        $this->listener->preUpdate($args);
64
    }
65
66
    private function load()
67
    {
68
        if ($this->listener) {
69
            return;
70
        }
71
72
        $this->listener = $this->container->get($this->service);
73
    }
74
}
75