Completed
Push — master ( e6f9e9...ade0a3 )
by Peter
03:33
created

DoctrineListener::postUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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
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
     * @param Keeper $keeper
23
     */
24 6
    public function __construct(Keeper $keeper)
25
    {
26 6
        $this->keeper = $keeper;
27 6
    }
28
29
    /**
30
     * @param LifecycleEventArgs $args
31
     */
32 2
    public function postPersist(LifecycleEventArgs $args)
33
    {
34 2
        $this->keeper->set($this->getKeyFromEntity($args), new \DateTime());
35 1
    }
36
37
    /**
38
     * @param LifecycleEventArgs $args
39
     */
40 2
    public function postRemove(LifecycleEventArgs $args)
41
    {
42 2
        $this->keeper->set($this->getKeyFromEntity($args), new \DateTime());
43 1
    }
44
45
    /**
46
     * @param LifecycleEventArgs $args
47
     */
48 2
    public function postUpdate(LifecycleEventArgs $args)
49
    {
50 2
        $this->keeper->set($this->getKeyFromEntity($args), new \DateTime());
51 1
    }
52
53
    /**
54
     * @param LifecycleEventArgs $args
55
     *
56
     * @return string
57
     */
58 6
    protected function getKeyFromEntity(LifecycleEventArgs $args)
59
    {
60 6
        $parts = explode('\\', get_class($args->getEntity()));
61 6
        $entity = array_pop($parts);
62 6
        $namespace = implode('\\', $parts);
63
64 6
        $namespaces = $args->getEntityManager()->getConfiguration()->getEntityNamespaces();
65 6
        foreach ($namespaces as $ns_alias => $ns) {
66 6
            if ($ns == $namespace) {
67 3
                return $ns_alias.':'.$entity;
68
            }
69 6
        }
70
71 3
        throw new \RuntimeException(sprintf('Namespace "%s" is not supported from EntityManager', $namespace));
72
    }
73
}
74