Passed
Push — master ( d70c89...2d756e )
by Christian
01:55
created

AbstractListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A containsTrait() 0 17 5
A createDateTimeField() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Doctrine\EventListener\ORM;
13
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ORM\Mapping\ClassMetadata;
16
use ReflectionClass;
17
18
abstract class AbstractListener implements EventSubscriber
19
{
20
    /**
21
     * @param ReflectionClass $reflection
22
     * @param string          $class
23
     *
24
     * @return bool
25
     */
26
    final protected function containsTrait(ReflectionClass $reflection, string $class): bool
27
    {
28
        do {
29
            $traits = $reflection->getTraitNames();
30
31
            if (\in_array($class, $traits, true)) {
32
                return true;
33
            }
34
35
            foreach ($reflection->getTraits() as $reflTraits) {
36
                if ($this->containsTrait($reflTraits, $class)) {
37
                    return true;
38
                }
39
            }
40
        } while ($reflection = $reflection->getParentClass());
41
42
        return false;
43
    }
44
45
    /**
46
     * @param ClassMetadata $metadata
47
     * @param string        $field
48
     * @param bool          $nullable
49
     */
50
    final protected function createDateTimeField(ClassMetadata $metadata, string $field, bool $nullable): void
51
    {
52
        if ($metadata->hasField($field)) {
53
            return;
54
        }
55
56
        $metadata->mapField([
57
            'type'      => 'datetime',
58
            'fieldName' => $field,
59
            'nullable'  => $nullable,
60
        ]);
61
    }
62
}
63