Completed
Pull Request — master (#602)
by Tom
07:15
created

MappingCollector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
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 106
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0
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 Laminas\DeveloperTools to record and display mapping information
19
 *
20
 * @link    www.doctrine-project.org
21
 */
22
class MappingCollector implements CollectorInterface, AutoHideInterface, Serializable
23
{
24
    /**
25
     * Collector priority
26
     */
27
    public const PRIORITY = 10;
28
29
    protected string $name;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
30
31
    protected ?ClassMetadataFactory $classMetadataFactory = null;
32
33
    /** @var ClassMetadata[] indexed by class name */
34
    protected array $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
    public function getName()
46
    {
47
        return $this->name;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function getPriority()
54
    {
55
        return self::PRIORITY;
56 1
    }
57
58 1
    /**
59
     * {@inheritDoc}
60
     */
61
    public function collect(MvcEvent $mvcEvent)
62
    {
63
        if (! $this->classMetadataFactory) {
64 1
            return;
65
        }
66 1
67
        /** @var ClassMetadata[] $metadata */
68
        $metadata      = $this->classMetadataFactory->getAllMetadata();
69
        $this->classes = [];
70
71
        foreach ($metadata as $class) {
72 2
            $this->classes[$class->getName()] = $class;
73
        }
74 2
75 1
        ksort($this->classes);
76
    }
77
78
    /**
79 2
     * {@inheritDoc}
80 2
     */
81
    public function canHide()
82 2
    {
83 2
        return empty($this->classes);
84
    }
85 2
86 2
    /**
87
     * {@inheritDoc}
88
     */
89
    public function serialize()
90
    {
91 1
        return serialize(
92
            [
93 1
                'name'    => $this->name,
94
                'classes' => $this->classes,
95
            ]
96
        );
97
    }
98
99 1
    /**
100
     * {@inheritDoc}
101 1
     */
102
    public function unserialize($serialized)
103 1
    {
104 1
        $data          = unserialize($serialized);
105
        $this->name    = $data['name'];
106
        $this->classes = $data['classes'];
107
    }
108
109
    /**
110
     * @return ClassMetadata[]
111
     */
112 1
    public function getClasses() : array
113
    {
114 1
        return $this->classes;
115 1
    }
116
}
117