Completed
Push — master ( 941ba1...1ac524 )
by Peter
03:41
created

DoctrineListener::getKeyFromEntity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
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\Keeper;
12
use Doctrine\ORM\Event\LifecycleEventArgs;
13
14
class DoctrineListener
15
{
16
    /**
17
     * @var Keeper
18
     */
19
    protected $keeper;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $track_individually_entity;
25
26
    /**
27
     * @param Keeper $keeper
28
     * @param bool $track_individually_entity
29
     */
30 21
    public function __construct(Keeper $keeper, $track_individually_entity)
31
    {
32 21
        $this->keeper = $keeper;
33 21
        $this->track_individually_entity = $track_individually_entity;
34 21
    }
35
36
    /**
37
     * @param LifecycleEventArgs $args
38
     */
39 7
    public function postPersist(LifecycleEventArgs $args)
40
    {
41 7
        $this->update($args, false);
42 6
    }
43
44
    /**
45
     * @param LifecycleEventArgs $args
46
     */
47 7
    public function postRemove(LifecycleEventArgs $args)
48
    {
49 7
        $this->update($args, true);
50 6
    }
51
52
    /**
53
     * @param LifecycleEventArgs $args
54
     */
55 7
    public function postUpdate(LifecycleEventArgs $args)
56
    {
57 7
        $this->update($args, false);
58 6
    }
59
60
    /**
61
     * @param LifecycleEventArgs $args
62
     * @param bool $remove
63
     */
64 21
    protected function update(LifecycleEventArgs $args, $remove)
65
    {
66 21
        $alias = $this->getEntityAlias($args);
67 18
        $this->keeper->set($alias, new \DateTime());
68
69 18
        if ($this->track_individually_entity && ($ids = $this->getEntityIdentifier($args))) {
70 6
            if ($remove) {
71 2
                $this->keeper->remove($alias.$ids);
72 2
            } else {
73 4
                $this->keeper->set($alias.$ids, new \DateTime());
74
            }
75 6
        }
76 18
    }
77
78
    /**
79
     * @param LifecycleEventArgs $args
80
     *
81
     * @return string
82
     */
83 21
    protected function getEntityAlias(LifecycleEventArgs $args)
84
    {
85 21
        $class = get_class($args->getEntity());
86
87 21
        foreach ($args->getEntityManager()->getConfiguration()->getEntityNamespaces() as $ns_alias => $ns) {
88 21
            if (strpos($class, $ns) === 0) {
89 18
                return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\');
90
            }
91 21
        }
92
93 3
        throw new \RuntimeException(sprintf('Entity "%s" is not supported from EntityManager', $class));
94
    }
95
96
    /**
97
     * @param LifecycleEventArgs $args
98
     *
99
     * @return string
100
     */
101 12
    protected function getEntityIdentifier(LifecycleEventArgs $args)
102
    {
103
        $ids = $args
104 12
            ->getEntityManager()
105 12
            ->getClassMetadata(get_class($args->getEntity()))
106 12
            ->getIdentifierValues($args->getEntity());
107 12
        return $ids ? Keeper::IDENTIFIER_PREFIX.implode(Keeper::IDENTIFIER_SEPARATOR, $ids) : '';
108
    }
109
}
110