MappingCollector   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 97
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getPriority() 0 4 1
A collect() 0 16 3
A canHide() 0 4 1
A serialize() 0 9 1
A unserialize() 0 6 1
A getClasses() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Collector;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
9
use Laminas\DeveloperTools\Collector\AutoHideInterface;
10
use Laminas\DeveloperTools\Collector\CollectorInterface;
11
use Laminas\Mvc\MvcEvent;
12
use Serializable;
13
use function ksort;
14
use function serialize;
15
use function unserialize;
16
17
/**
18
 * Collector to be used in DeveloperTools to record and display mapping information
19
 */
20
class MappingCollector implements CollectorInterface, AutoHideInterface, Serializable
21
{
22
    /**
23
     * Collector priority
24
     */
25
    public const PRIORITY = 10;
26
27
    /** @var string */
28
    protected $name;
29
30
    /** @var ClassMetadataFactory|null */
31
    protected $classMetadataFactory = [];
32
33
    /** @var ClassMetadata[] indexed by class name */
34
    protected $classes = [];
35
36
    public function __construct(ClassMetadataFactory $classMetadataFactory, string $name)
37
    {
38
        $this->classMetadataFactory = $classMetadataFactory;
39
        $this->name                 = (string) $name;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 1
    public function getName()
46
    {
47 1
        return $this->name;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 1
    public function getPriority()
54
    {
55 1
        return self::PRIORITY;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 2
    public function collect(MvcEvent $mvcEvent)
62
    {
63 2
        if (! $this->classMetadataFactory) {
64 1
            return;
65
        }
66
67
        /** @var ClassMetadata[] $metadata */
68 2
        $metadata      = $this->classMetadataFactory->getAllMetadata();
69 2
        $this->classes = [];
70
71 2
        foreach ($metadata as $class) {
72 2
            $this->classes[$class->getName()] = $class;
73
        }
74
75 2
        ksort($this->classes);
76 2
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 1
    public function canHide()
82
    {
83 1
        return empty($this->classes);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 1
    public function serialize()
90
    {
91 1
        return serialize(
92
            [
93 1
                'name'    => $this->name,
94 1
                'classes' => $this->classes,
95
            ]
96
        );
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 1
    public function unserialize($serialized)
103
    {
104 1
        $data          = unserialize($serialized);
105 1
        $this->name    = $data['name'];
106 1
        $this->classes = $data['classes'];
107 1
    }
108
109
    /**
110
     * @return ClassMetadata[]
111
     */
112 1
    public function getClasses() : array
113
    {
114 1
        return $this->classes;
115
    }
116
}
117