EntityAnnotationCacheWarmer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
c 2
b 1
f 0
lcom 1
cbo 5
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isOptional() 0 4 1
A warmUp() 0 11 2
A getEntities() 0 21 4
A getResources() 0 11 2
A getMetadata() 0 6 1
1
<?php
2
3
namespace Padam87\AttributeBundle\CacheWarmer;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Symfony\Component\Config\ConfigCache;
9
use Symfony\Component\Config\Resource\FileResource;
10
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
11
12
class EntityAnnotationCacheWarmer implements CacheWarmerInterface
13
{
14
    /**
15
     * @var ManagerRegistry
16
     */
17
    private $doctrine;
18
19
    /**
20
     * @var bool
21
     */
22
    private $debug;
23
24
    /**
25
     * @param ManagerRegistry $doctrine
26
     * @param                 $debug
27
     */
28
    public function __construct(ManagerRegistry $doctrine, $debug)
29
    {
30
        $this->doctrine = $doctrine;
31
        $this->debug = $debug;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function isOptional()
38
    {
39
        return false;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function warmUp($cacheDir)
46
    {
47
        $filename = $cacheDir . '/padam87/attribute_bundle/Entity.cache.php';
48
49
        $cache = new ConfigCache($filename, $this->debug);
50
51
        if (!$cache->isFresh()) {
52
            $content  = '<?php return ' . var_export($this->getEntities(), true) . ';';
53
            $cache->write($content, $this->getResources());
54
        }
55
    }
56
57
    protected function getEntities()
58
    {
59
        $entities = [];
60
61
        $reader = new AnnotationReader();
62
63
        /** @var ClassMetadata $metadata */
64
        foreach ($this->getMetadata() as $metadata) {
65
            $refl = $metadata->getReflectionClass();
66
67
            if ($refl === null) {
68
                $refl = new \ReflectionClass($metadata->getName());
69
            }
70
71
            if ($reader->getClassAnnotation($refl, 'Padam87\AttributeBundle\Annotation\Entity') != null) {
72
                $entities[$metadata->getName()] = true;
73
            }
74
        }
75
76
        return $entities;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    protected function getResources()
83
    {
84
        $res = [];
85
86
        /** @var ClassMetadata $m */
87
        foreach ($this->getMetadata() as $m) {
88
            $res[] = new FileResource($m->getReflectionClass()->getFileName());
89
        }
90
91
        return $res;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    protected function getMetadata()
98
    {
99
        $em = $this->doctrine->getManager();
100
101
        return $em->getMetadataFactory()->getAllMetadata();
102
    }
103
}
104