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

DoctrineListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A postPersist() 0 4 1
A postRemove() 0 4 1
A postUpdate() 0 4 1
A getKeyFromEntity() 0 15 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
     * @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