Completed
Push — master ( 8c9a02...d7d88b )
by Tobias
11:28
created

AnnotationDriver::loadMetadataFromObject()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 7
nop 1
crap 7
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
        return $metadata;
66
    }
67
}
68