Completed
Push — master ( a7c3c6...973485 )
by Peter
04:02
created

DoctrineListener::getEntityAlias()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Event\Listener;
10
11
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder;
12
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Keeper;
13
use Doctrine\ORM\Event\LifecycleEventArgs;
14
15
class DoctrineListener
16
{
17
    /**
18
     * @var Keeper
19
     */
20
    protected $keeper;
21
22
    /**
23
     * @var CacheKeyBuilder
24
     */
25
    protected $builder;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $track_individually_entity;
31
32
    /**
33
     * @param Keeper $keeper
34
     * @param CacheKeyBuilder $builder
35
     * @param bool $track_individually_entity
36
     */
37 9
    public function __construct(Keeper $keeper, CacheKeyBuilder $builder, $track_individually_entity)
38
    {
39 9
        $this->keeper = $keeper;
40 9
        $this->builder = $builder;
41 9
        $this->track_individually_entity = $track_individually_entity;
42 9
    }
43
44
    /**
45
     * @param LifecycleEventArgs $args
46
     */
47 3
    public function postPersist(LifecycleEventArgs $args)
48
    {
49 3
        $this->update($args, false);
50 3
    }
51
52
    /**
53
     * @param LifecycleEventArgs $args
54
     */
55 3
    public function postRemove(LifecycleEventArgs $args)
56
    {
57 3
        $this->update($args, true);
58 3
    }
59
60
    /**
61
     * @param LifecycleEventArgs $args
62
     */
63 3
    public function postUpdate(LifecycleEventArgs $args)
64
    {
65 3
        $this->update($args, false);
66 3
    }
67
68
    /**
69
     * @param LifecycleEventArgs $args
70
     * @param bool $remove
71
     */
72 9
    protected function update(LifecycleEventArgs $args, $remove)
73
    {
74 9
        $alias = $this->builder->getEntityAlias($args->getEntity(), $args->getEntityManager());
75 9
        $this->keeper->set($alias, new \DateTime());
76
77 9
        if ($this->track_individually_entity) {
78 6
            $ids = $this->builder->getEntityIdentifier($args->getEntity(), $args->getEntityManager());
79 6
            if ($ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ids of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
80 6
                if ($remove) {
81 2
                    $this->keeper->remove($alias.$ids);
82 2
                } else {
83 4
                    $this->keeper->set($alias.$ids, new \DateTime());
84
                }
85 6
            }
86 6
        }
87 9
    }
88
}
89