Completed
Push — master ( 35fbe8...c9e152 )
by Tomas
04:31
created

AnnotationDriver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 55
ccs 25
cts 29
cp 0.8621
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 38 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(
44 1
                'The class %s is not geocodeable', get_class($object)
45
            ));
46
        }
47
48 1
        $metadata = new ClassMetadata();
49
50 1
        foreach ($reflection->getProperties() as $property) {
51 1
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
52 1
                if ($annotation instanceof Annotations\Latitude) {
53 1
                    $property->setAccessible(true);
54 1
                    $metadata->latitudeProperty = $property;
55 1
                } elseif ($annotation instanceof Annotations\Longitude) {
56 1
                    $property->setAccessible(true);
57 1
                    $metadata->longitudeProperty = $property;
58 1
                } elseif ($annotation instanceof Annotations\Address) {
59 1
                    $property->setAccessible(true);
60 1
                    $metadata->addressProperty = $property;
61
                }
62
            }
63
        }
64
65 1
        foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
66
            if ($this->reader->getMethodAnnotation($method, Annotations\Address::class)) {
67
                if (0 !== $method->getNumberOfRequiredParameters()) {
68
                    throw new \Exception('You can not use a method requiring parameters with @Address annotation!');
69
                }
70
71
                $metadata->addressGetter = $method;
72
            }
73
        }
74
75 1
        return $metadata;
76
    }
77
}
78