<commit_before><?php

namespace Furniture\GoogleServicesBundle\Entity\Subscribers;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Furniture\GoogleServicesBundle\Entity\Interfaces\AddressMarkerInterface;
use Furniture\GoogleServicesBundle\Api\Maps\Geocoding;
use Sylius\Component\Translation\Model\TranslatableInterface;
use Sylius\Component\Translation\Provider\LocaleProviderInterface;

class AddressMarkerSubscriber implements EventSubscriber
{
    /**
     * @var LocaleProviderInterface
     */
    private $localeProvider;

    /**
     * @var Geocoding
     */
    private $geocoding;

    /**
     * Constructor.
     *
     * @param Geocoding               $geocoding
     * @param LocaleProviderInterface $localeProvider
     */
    public function __construct(Geocoding $geocoding, LocaleProviderInterface $localeProvider)
    {
        $this->geocoding = $geocoding;
        $this->localeProvider = $localeProvider;
    }
    
    /**
     * On flush
     *
     * @param OnFlushEventArgs $event
     */
    public function onFlush(OnFlushEventArgs $event)
    {
        $em = $event->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            $this->setCoord($em, $entity);
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            $this->setCoord($em, $entity);
        }
    }

    /**
     * Upload for entities
     *
     * @param EntityManagerInterface $em
     * @param object                 $entity
     */
    private function setCoord(EntityManagerInterface $em, $entity)
    {
        if ($entity instanceof AddressMarkerInterface) {
            if ($entity instanceof TranslatableInterface) {
                $entity->setCurrentLocale($this->localeProvider->getCurrentLocale());
                $entity->setFallbackLocale($this->localeProvider->getFallbackLocale());
            }

            $address = $entity->getAddress();
            $uow = $em->getUnitOfWork();
            
            if(!empty($address) && $geoData = $this->geocoding->getGeocode($address)){
                $location = $geoData[0]['geometry']['location'];
                $entity->setLat($location['lat']);
                $entity->setLng($location['lng']);
                //echo '<pre>'.print_r($geoData[0]['address_components'], true).'</pre>';die;
                foreach($geoData[0]['address_components'] as $addressComponent){
                    if(in_array( 'route', $addressComponent['types'])){
                        $entity->setRoute($addressComponent['short_name']);
                        continue;
                    }

                    if(in_array( 'country', $addressComponent['types'])){<commit_msg>remove empty address check in subscriber<commit_after><?php

namespace Furniture\GoogleServicesBundle\Entity\Subscribers;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Furniture\GoogleServicesBundle\Entity\Interfaces\AddressMarkerInterface;
use Furniture\GoogleServicesBundle\Api\Maps\Geocoding;
use Sylius\Component\Translation\Model\TranslatableInterface;
use Sylius\Component\Translation\Provider\LocaleProviderInterface;

class AddressMarkerSubscriber implements EventSubscriber
{
    /**
     * @var LocaleProviderInterface
     */
    private $localeProvider;

    /**
     * @var Geocoding
     */
    private $geocoding;

    /**
     * Constructor.
     *
     * @param Geocoding               $geocoding
     * @param LocaleProviderInterface $localeProvider
     */
    public function __construct(Geocoding $geocoding, LocaleProviderInterface $localeProvider)
    {
        $this->geocoding = $geocoding;
        $this->localeProvider = $localeProvider;
    }
    
    /**
     * On flush
     *
     * @param OnFlushEventArgs $event
     */
    public function onFlush(OnFlushEventArgs $event)
    {
        $em = $event->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            $this->setCoord($em, $entity);
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            $this->setCoord($em, $entity);
        }
    }

    /**
     * Upload for entities
     *
     * @param EntityManagerInterface $em
     * @param object                 $entity
     */
    private function setCoord(EntityManagerInterface $em, $entity)
    {
        if ($entity instanceof AddressMarkerInterface) {
            if ($entity instanceof TranslatableInterface) {
                $entity->setCurrentLocale($this->localeProvider->getCurrentLocale());
                $entity->setFallbackLocale($this->localeProvider->getFallbackLocale());
            }

            $address = $entity->getAddress();
            $uow = $em->getUnitOfWork();
            
            if($geoData = $this->geocoding->getGeocode($address)){
                $location = $geoData[0]['geometry']['location'];
                $entity->setLat($location['lat']);
                $entity->setLng($location['lng']);
                //echo '<pre>'.print_r($geoData[0]['address_components'], true).'</pre>';die;
                foreach($geoData[0]['address_components'] as $addressComponent){
                    if(in_array( 'route', $addressComponent['types'])){
                        $entity->setRoute($addressComponent['short_name']);
                        continue;
                    }

                    if(in_array( 'country', $addressComponent['types'])){
{'fraction_non_alphanumeric': 0.126235477718051, 'fraction_numerical': 0.0010404022888850356, 'mean_word_length': 2.5042527339003646, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6443501', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1535, 'n_words': 251}