Completed
Push — master ( 0ea764...7125e2 )
by Tomas
15:59 queued 17s
created

Doctrine/ORM/GeocoderListener.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Doctrine\ORM;
14
15
use Bazinga\GeocoderBundle\Mapping\ClassMetadata;
16
use Bazinga\GeocoderBundle\Mapping\Driver\DriverInterface;
17
use Doctrine\Common\EventSubscriber;
18
use Doctrine\ORM\Event\OnFlushEventArgs;
19
use Doctrine\ORM\Events;
20
use Geocoder\Provider\Provider;
21
use Geocoder\Query\GeocodeQuery;
22
23
/**
24
 * @author Markus Bachmann <[email protected]>
25
 */
26
class GeocoderListener implements EventSubscriber
27
{
28
    /**
29
     * @var DriverInterface
30
     */
31
    private $driver;
32
33
    /**
34
     * @var Provider
35
     */
36
    private $geocoder;
37
38
    public function __construct(Provider $geocoder, DriverInterface $driver)
39
    {
40
        $this->driver = $driver;
41
        $this->geocoder = $geocoder;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getSubscribedEvents()
48
    {
49
        return [
50
            Events::onFlush,
51
        ];
52
    }
53
54
    public function onFlush(OnFlushEventArgs $args)
55
    {
56
        $em = $args->getEntityManager();
57
        $uow = $em->getUnitOfWork();
58
59 View Code Duplication
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            if (!$this->driver->isGeocodeable($entity)) {
61
                continue;
62
            }
63
64
            $this->geocodeEntity($entity);
65
66
            $uow->recomputeSingleEntityChangeSet(
67
                $em->getClassMetadata(get_class($entity)),
68
                $entity
69
            );
70
        }
71
72 View Code Duplication
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            if (!$this->driver->isGeocodeable($entity)) {
74
                continue;
75
            }
76
77
            $this->geocodeEntity($entity);
78
79
            $uow->recomputeSingleEntityChangeSet(
80
                $em->getClassMetadata(get_class($entity)),
81
                $entity
82
            );
83
        }
84
    }
85
86
    private function geocodeEntity($entity)
87
    {
88
        /** @var ClassMetadata $metadata */
89
        $metadata = $this->driver->loadMetadataFromObject($entity);
90
91
        if (null !== $metadata->addressGetter) {
92
            $address = $metadata->addressGetter->invoke($entity);
93
        } else {
94
            $address = $metadata->addressProperty->getValue($entity);
95
        }
96
97
        if (empty($address)) {
98
            return;
99
        }
100
101
        $results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address));
102
103
        if (!empty($results)) {
104
            $result = $results->first();
105
            $metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude());
106
            $metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude());
107
        }
108
    }
109
}
110