src/Utils/Region/CountryService.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Region;
  3. use App\Entity\Gos\Country;
  4. use App\Entity\Gos\Currency;
  5. use App\Entity\Gos\Language;
  6. use App\Entity\Gos\User;
  7. use App\Utils\FileService;
  8. use App\Utils\Geolocation;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. class CountryService
  12. {
  13.     private static string $ENTITY_TYPE 'country';
  14.     private static string $JSON_FILETYPE 'json';
  15.     private EntityManagerInterface $em;
  16.     private FileService $fileService;
  17.     private Geolocation $geolocation;
  18.     private $countryRepository;
  19.     private $currencyRepository;
  20.     private $languageRepository;
  21.     public function __construct(EntityManagerInterface $emFileService $fileServiceGeolocation $geolocation)
  22.     {
  23.         $this->em                   $em;
  24.         $this->fileService          $fileService;
  25.         $this->geolocation          $geolocation;
  26.         $this->countryRepository    $em->getRepository(Country::class);
  27.         $this->currencyRepository   $em->getRepository(Currency::class);
  28.         $this->languageRepository   $em->getRepository(Language::class);
  29.     }
  30.     /**
  31.      * Try to look for country by geolocation or by (if there's an override)
  32.      */
  33.     public function getCountry(Request $request$user null)
  34.     {
  35.         $countryReq $request->request->get('country'null);
  36.         $countryRep $this->em->getRepository(Country::class);
  37.         if ($user and $user instanceof User)
  38.         {
  39.             if ($user->getCountry())
  40.             {
  41.                 return $user->getCountry();
  42.             }
  43.         }
  44.         if ($request->getSession()->get('fixCountryCode'))
  45.         {
  46.             $countryAlpha $request->getSession()->get('fixCountryCode');
  47.             $country      $countryRep->findOneByAlpha2($countryAlpha);
  48.         }
  49.         else
  50.         {
  51.             if (empty($countryReq))
  52.             {
  53.                 $myCountryCode =
  54.                     $request->getSession()->get('myCountryCode')
  55.                     ??
  56.                     $this->geolocation->geolocation()['countryCode'];
  57.                 if ($myCountryCode)
  58.                 {
  59.                     $country $countryRep->findOneByAlpha2($myCountryCode);
  60.                 }
  61.             }
  62.             else
  63.             {
  64.                 $country $countryRep->findOneByAlpha2($countryReq);
  65.             }
  66.         }
  67.         if ($country)
  68.         {
  69.             return $country;
  70.         }
  71.         return false;
  72.     }
  73.     public function import()
  74.     {
  75.         $this->setCountries($this->getRawData());
  76.         return true;
  77.     }
  78.     private function setCountries($data)
  79.     {
  80.         foreach ($data as $ctr)
  81.         {
  82.             if ($ctr['status'] == 'deleted' || strlen(trim($ctr['ioc'])) == 0)
  83.                 continue;
  84.             $country $this->countryRepository->findOneByName($ctr['name']);
  85.             $country $country ?? new Country();
  86.             $country->setIsActive(1);
  87.             $country->setAlpha2($ctr['alpha2']);
  88.             $country->setAlpha3($ctr['alpha3']);
  89.             if (isset($ctr['currencies']) && count($ctr['currencies']) > 0)
  90.             {
  91.                 $currency $this->currencyRepository->findOneByCode(reset($ctr['currencies']));
  92.                 $country->setCurrency($currency);
  93.             }
  94.             if (isset($ctr['languages']) && count($ctr['languages']) > 0)
  95.             {
  96.                 $language $this->languageRepository->findOneByShort(reset($ctr['languages']));
  97.                 $country->setLanguage($language);
  98.             }
  99.             $country->setEmoji(isset($ctr['emoji']) ? $ctr['emoji'] : null);
  100.             $country->setIoc($ctr['ioc']);
  101.             $country->setName($ctr['name']);
  102.             $this->em->persist($country);
  103.         }
  104.         $this->em->flush();
  105.         return true;
  106.     }
  107.     private function getRawData()
  108.     {
  109.         $data $this->fileService->getFile(self::$ENTITY_TYPEself::$JSON_FILETYPE);
  110.         return json_decode($datatrue);
  111.     }
  112.     public function removeOld()
  113.     {
  114.         $countries $this->countryRepository->findAll();
  115.         foreach ($countries as $country)
  116.         {
  117.             $this->em->remove($country);
  118.         }
  119.         $this->em->flush();
  120.         return true;
  121.     }
  122. }