Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
created

MappingCollector::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DoctrineORMModule\Collector;
4
5
use Serializable;
6
7
use ZendDeveloperTools\Collector\CollectorInterface;
8
use ZendDeveloperTools\Collector\AutoHideInterface;
9
10
use Zend\Mvc\MvcEvent;
11
12
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
13
14
/**
15
 * Collector to be used in ZendDeveloperTools to record and display mapping information
16
 *
17
 * @license MIT
18
 * @link    www.doctrine-project.org
19
 * @author  Marco Pivetta <[email protected]>
20
 */
21
class MappingCollector implements CollectorInterface, AutoHideInterface, Serializable
22
{
23
    /**
24
     * Collector priority
25
     */
26
    const PRIORITY = 10;
27
28
    /**
29
     * @var string
30
     */
31
    protected $name;
32
33
    /**
34
     * @var ClassMetadataFactory|null
35
     */
36
    protected $classMetadataFactory = [];
37
38
    /**
39
     * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata[] indexed by class name
40
     */
41
    protected $classes = [];
42
43
    /**
44
     * @param ClassMetadataFactory $classMetadataFactory
45
     * @param string               $name
46
     */
47
    public function __construct(ClassMetadataFactory $classMetadataFactory, $name)
48
    {
49
        $this->classMetadataFactory = $classMetadataFactory;
50
        $this->name                 = (string) $name;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 1
    public function getName()
57
    {
58 1
        return $this->name;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 1
    public function getPriority()
65
    {
66 1
        return static::PRIORITY;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 2
    public function collect(MvcEvent $mvcEvent)
73
    {
74 2
        if (! $this->classMetadataFactory) {
75 1
            return;
76
        }
77
78
        /* @var $metadata \Doctrine\Common\Persistence\Mapping\ClassMetadata[] */
79 2
        $metadata      = $this->classMetadataFactory->getAllMetadata();
80 2
        $this->classes = [];
81
82 2
        foreach ($metadata as $class) {
83 2
            $this->classes[$class->getName()] = $class;
84
        }
85 2
        ksort($this->classes);
86 2
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function canHide()
92
    {
93 1
        return empty($this->classes);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 1
    public function serialize()
100
    {
101 1
        return serialize(
102
            [
103 1
                'name'    => $this->name,
104 1
                'classes' => $this->classes,
105
            ]
106
        );
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 1
    public function unserialize($serialized)
113
    {
114 1
        $data          = unserialize($serialized);
115 1
        $this->name    = $data['name'];
116 1
        $this->classes = $data['classes'];
117 1
    }
118
119
    /**
120
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata[]
121
     */
122 1
    public function getClasses()
123
    {
124 1
        return $this->classes;
125
    }
126
}
127