Completed
Push — 2.x-dev-kit ( 6f250c )
by
unknown
02:44
created

DoctrinePHPCRODMListenerContainerAware::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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