AnnotationDriver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 53
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isGeocodeable() 0 6 1
B loadMetadataFromObject() 0 36 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Bazinga\GeocoderBundle\Mapping\Driver;
14
15
use Bazinga\GeocoderBundle\Mapping\Annotations;
16
use Bazinga\GeocoderBundle\Mapping\ClassMetadata;
17
use Bazinga\GeocoderBundle\Mapping\Exception;
18
use Doctrine\Common\Annotations\Reader;
19
20
/**
21
 * @author Markus Bachmann <[email protected]>
22
 */
23
class AnnotationDriver implements DriverInterface
24
{
25
    private $reader;
26
27 3
    public function __construct(Reader $reader)
28
    {
29 3
        $this->reader = $reader;
30 3
    }
31
32 1
    public function isGeocodeable($object): bool
33
    {
34 1
        $reflection = new \ReflectionObject($object);
35
36 1
        return (bool) $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class);
37
    }
38
39 2
    public function loadMetadataFromObject($object)
40
    {
41 2
        $reflection = new \ReflectionObject($object);
42 2
        if (!$annotation = $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class)) {
43 1
            throw new Exception\MappingException(sprintf('The class %s is not geocodeable', get_class($object)));
44
        }
45
46 1
        $metadata = new ClassMetadata();
47
48 1
        foreach ($reflection->getProperties() as $property) {
49 1
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
50 1
                if ($annotation instanceof Annotations\Latitude) {
51 1
                    $property->setAccessible(true);
52 1
                    $metadata->latitudeProperty = $property;
53 1
                } elseif ($annotation instanceof Annotations\Longitude) {
54 1
                    $property->setAccessible(true);
55 1
                    $metadata->longitudeProperty = $property;
56 1
                } elseif ($annotation instanceof Annotations\Address) {
57 1
                    $property->setAccessible(true);
58 1
                    $metadata->addressProperty = $property;
59
                }
60
            }
61
        }
62
63 1
        foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
64
            if ($this->reader->getMethodAnnotation($method, Annotations\Address::class)) {
65
                if (0 !== $method->getNumberOfRequiredParameters()) {
66
                    throw new \Exception('You can not use a method requiring parameters with @Address annotation!');
67
                }
68
69
                $metadata->addressGetter = $method;
70
            }
71
        }
72
73 1
        return $metadata;
74
    }
75
}
76