EventListener::postLoadClassMetadata()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Geolocation\Doctrine\ODM\MongoDB\EventListener;
13
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs;
15
use Cubiche\Infrastructure\Geolocation\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver;
16
use Cubiche\Infrastructure\Geolocation\Doctrine\ODM\MongoDB\Types\CoordinateType;
17
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
18
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
19
use Doctrine\ODM\MongoDB\Types\Type;
20
21
/**
22
 * Event Listener Class.
23
 *
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 */
26
class EventListener
27
{
28
    /**
29
     * @param RegisterDriverMetadataEventArgs $eventArgs
30
     */
31
    public function registerDriverMetadata(RegisterDriverMetadataEventArgs $eventArgs)
32
    {
33
        $eventArgs->driverFactory()->registerXmlDriver(XmlDriver::class);
34
    }
35
36
    /**
37
     * @param LoadClassMetadataEventArgs $eventArgs
38
     */
39
    public function postLoadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
40
    {
41
        $this->checkCoordinateType($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...
42
    }
43
44
    /**
45
     * @param ClassMetadata $classMetadata
46
     */
47
    protected function checkCoordinateType(ClassMetadata $classMetadata)
48
    {
49
        foreach ($classMetadata->fieldMappings as $fieldName => $mapping) {
50
            if (isset($mapping['cubiche:coordinate'])) {
51
                $type = 'Coordinate';
52
                if (!Type::hasType($type)) {
53
                    Type::registerType($type, CoordinateType::class);
54
                }
55
56
                $classMetadata->fieldMappings[$fieldName]['type'] = $type;
57
            }
58
        }
59
    }
60
}
61