DoctrineListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 1
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Event\Listener;
11
12
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder;
13
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Keeper;
14
use Doctrine\ORM\Event\LifecycleEventArgs;
15
16
class DoctrineListener
17
{
18
    /**
19
     * @var Keeper
20
     */
21
    protected $keeper;
22
23
    /**
24
     * @var CacheKeyBuilder
25
     */
26
    protected $builder;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $track_individually_entity;
32
33
    /**
34
     * @param Keeper $keeper
35
     * @param CacheKeyBuilder $builder
36
     * @param bool $track_individually_entity
37
     */
38 9
    public function __construct(Keeper $keeper, CacheKeyBuilder $builder, $track_individually_entity)
39
    {
40 9
        $this->keeper = $keeper;
41 9
        $this->builder = $builder;
42 9
        $this->track_individually_entity = $track_individually_entity;
43 9
    }
44
45
    /**
46
     * @param LifecycleEventArgs $args
47
     */
48 3
    public function postPersist(LifecycleEventArgs $args)
49
    {
50 3
        $this->update($args, false);
51 3
    }
52
53
    /**
54
     * @param LifecycleEventArgs $args
55
     */
56 3
    public function postRemove(LifecycleEventArgs $args)
57
    {
58 3
        $this->update($args, true);
59 3
    }
60
61
    /**
62
     * @param LifecycleEventArgs $args
63
     */
64 3
    public function postUpdate(LifecycleEventArgs $args)
65
    {
66 3
        $this->update($args, false);
67 3
    }
68
69
    /**
70
     * @param LifecycleEventArgs $args
71
     * @param bool $remove
72
     */
73 9
    protected function update(LifecycleEventArgs $args, $remove)
74
    {
75 9
        $alias = $this->builder->getEntityAlias($args->getEntity());
76 9
        $this->keeper->set($alias, new \DateTime());
77
78 9
        if ($this->track_individually_entity) {
79 6
            $ids = $this->builder->getEntityIdentifier($args->getEntity());
80 6
            if ($ids !== null) {
81 6
                if ($remove) {
82 2
                    $this->keeper->remove($alias.$ids);
83 2
                } else {
84 4
                    $this->keeper->set($alias.$ids, new \DateTime());
85
                }
86 6
            }
87 6
        }
88 9
    }
89
}
90