EventListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerDriverMetadata() 0 4 1
A postLoadClassMetadata() 0 4 1
B checkTypes() 0 21 5
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
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 Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\EventListener;
13
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs;
15
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver;
16
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\EmailAddressType;
17
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\HostNameType;
18
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\IPAddressType;
19
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\PathType;
20
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\PortType;
21
use Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Types\UrlType;
22
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
23
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
24
use Doctrine\ODM\MongoDB\Types\Type;
25
26
/**
27
 * Event Listener Class.
28
 *
29
 * @author Karel Osorio Ramírez <[email protected]>
30
 * @author Ivannis Suárez Jerez <[email protected]>
31
 */
32
class EventListener
33
{
34
    /**
35
     * @var array
36
     */
37
    protected $typeMapping = array(
38
        'email' => EmailAddressType::class,
39
        'hostname' => HostNameType::class,
40
        'ip' => IPAddressType::class,
41
        'path' => PathType::class,
42
        'port' => PortType::class,
43
        'url' => UrlType::class,
44
    );
45
46
    /**
47
     * @param RegisterDriverMetadataEventArgs $eventArgs
48
     */
49
    public function registerDriverMetadata(RegisterDriverMetadataEventArgs $eventArgs)
50
    {
51
        $eventArgs->driverFactory()->registerXmlDriver(XmlDriver::class);
52
    }
53
54
    /**
55
     * @param LoadClassMetadataEventArgs $eventArgs
56
     */
57
    public function postLoadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
58
    {
59
        $this->checkTypes($eventArgs->getClassMetadata());
0 ignored issues
show
Compatibility introduced by
$eventArgs->getClassMetadata() of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
    }
61
62
    /**
63
     * @param ClassMetadata $classMetadata
64
     */
65
    protected function checkTypes(ClassMetadata $classMetadata)
66
    {
67
        $types = array_keys($this->typeMapping);
68
        foreach ($classMetadata->fieldMappings as $fieldName => $mapping) {
69
            foreach ($types as $type) {
70
                if (isset($mapping['cubiche:'.$type])) {
71
                    $typeName = substr(
72
                        $this->typeMapping[$type],
73
                        strrpos($this->typeMapping[$type], '\\') + 1
74
                    );
75
76
                    if (!Type::hasType($typeName)) {
77
                        Type::registerType($typeName, $this->typeMapping[$type]);
78
                    }
79
80
                    $classMetadata->fieldMappings[$fieldName]['type'] = $typeName;
81
                    break;
82
                }
83
            }
84
        }
85
    }
86
}
87