Completed
Push — master ( 709dbb...f12a68 )
by Tobias
09:11
created

AnnotationDriver::loadMetadataFromObject()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 7
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BazingaGeocoderBundle package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
11
namespace Bazinga\GeocoderBundle\Mapping\Driver;
12
13
use Bazinga\GeocoderBundle\Mapping\Annotations;
14
use Bazinga\GeocoderBundle\Mapping\ClassMetadata;
15
use Bazinga\GeocoderBundle\Mapping\Exception;
16
use Doctrine\Common\Annotations\Reader;
17
18
/**
19
 * @author Markus Bachmann <[email protected]>
20
 */
21
class AnnotationDriver implements DriverInterface
22
{
23
    private $reader;
24
25
    public function __construct(Reader $reader)
26
    {
27
        $this->reader = $reader;
28
    }
29
30
    public function isGeocodeable($object)
31
    {
32
        $reflection = new \ReflectionObject($object);
33
34
        return (bool) $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class);
35
    }
36
37
    public function loadMetadataFromObject($object)
38
    {
39
        $reflection = new \ReflectionObject($object);
40
        if (!$annotation = $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class)) {
41
            throw new Exception\MappingException(sprintf(
42
                'The class %s is not geocodeable', get_class($object)
43
            ));
44
        }
45
46
        $metadata = new ClassMetadata();
47
48
        foreach ($reflection->getProperties() as $property) {
49
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
50
                if ($annotation instanceof Annotations\Latitude) {
51
                    $property->setAccessible(true);
52
                    $metadata->latitudeProperty = $property;
53
                } elseif ($annotation instanceof Annotations\Longitude) {
54
                    $property->setAccessible(true);
55
                    $metadata->longitudeProperty = $property;
56
                } elseif ($annotation instanceof Annotations\Address) {
57
                    $property->setAccessible(true);
58
                    $metadata->addressProperty = $property;
59
                }
60
            }
61
        }
62
63
        return $metadata;
64
    }
65
}
66