|
| 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 Doctrine\ORM\UnitOfWork; |
| 21 | +use Geocoder\Query\GeocodeQuery; |
| 22 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Markus Bachmann <markus.bachmann@bachi.biz> |
| 26 | + * @author Pierre du Plessis <pdples@gmail.com> |
| 27 | + */ |
| 28 | +class GeocodeEntityListener implements EventSubscriber |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var DriverInterface |
| 32 | + */ |
| 33 | + private $driver; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ServiceLocator |
| 37 | + */ |
| 38 | + private $providerLocator; |
| 39 | + |
| 40 | + public function __construct(ServiceLocator $providerLocator, DriverInterface $driver) |
| 41 | + { |
| 42 | + $this->driver = $driver; |
| 43 | + $this->providerLocator = $providerLocator; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@inheritdoc} |
| 48 | + */ |
| 49 | + public function getSubscribedEvents() |
| 50 | + { |
| 51 | + return [ |
| 52 | + Events::onFlush, |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + public function onFlush(OnFlushEventArgs $args) |
| 57 | + { |
| 58 | + $em = $args->getEntityManager(); |
| 59 | + $uow = $em->getUnitOfWork(); |
| 60 | + |
| 61 | + foreach ($uow->getScheduledEntityInsertions() as $entity) { |
| 62 | + if (!$this->driver->isGeocodeable($entity)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + /** @var ClassMetadata $metadata */ |
| 67 | + $metadata = $this->driver->loadMetadataFromObject($entity); |
| 68 | + |
| 69 | + $this->geocodeEntity($metadata, $entity); |
| 70 | + |
| 71 | + $uow->recomputeSingleEntityChangeSet( |
| 72 | + $em->getClassMetadata(get_class($entity)), |
| 73 | + $entity |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + foreach ($uow->getScheduledEntityUpdates() as $entity) { |
| 78 | + if (!$this->driver->isGeocodeable($entity)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + /** @var ClassMetadata $metadata */ |
| 83 | + $metadata = $this->driver->loadMetadataFromObject($entity); |
| 84 | + |
| 85 | + if (!$this->shouldGeocode($metadata, $uow, $entity)) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $this->geocodeEntity($metadata, $entity); |
| 90 | + |
| 91 | + $uow->recomputeSingleEntityChangeSet( |
| 92 | + $em->getClassMetadata(get_class($entity)), |
| 93 | + $entity |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param object $entity |
| 100 | + */ |
| 101 | + private function geocodeEntity(ClassMetadata $metadata, $entity) |
| 102 | + { |
| 103 | + if (null !== $metadata->addressGetter) { |
| 104 | + $address = $metadata->addressGetter->invoke($entity); |
| 105 | + } else { |
| 106 | + $address = $metadata->addressProperty->getValue($entity); |
| 107 | + } |
| 108 | + |
| 109 | + if (empty($address)) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + $serviceId = sprintf('bazinga_geocoder.provider.%s', $metadata->provider); |
| 114 | + |
| 115 | + if (!$this->providerLocator->has($serviceId)) { |
| 116 | + throw new \RuntimeException(sprintf('The provider "%s" is invalid for object "%s".', $metadata->provider, get_class($entity))); |
| 117 | + } |
| 118 | + |
| 119 | + $results = $this->providerLocator->get($serviceId)->geocodeQuery(GeocodeQuery::create($address)); |
| 120 | + |
| 121 | + if (!$results->isEmpty()) { |
| 122 | + $result = $results->first(); |
| 123 | + $metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude()); |
| 124 | + $metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param object $entity |
| 130 | + */ |
| 131 | + private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, $entity): bool |
| 132 | + { |
| 133 | + if (null !== $metadata->addressGetter) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + $changeSet = $unitOfWork->getEntityChangeSet($entity); |
| 138 | + |
| 139 | + return isset($changeSet[$metadata->addressProperty->getName()]); |
| 140 | + } |
| 141 | +} |
0 commit comments