<?phpnamespace App\Utils\Region;use App\Entity\Gos\Country;use App\Entity\Gos\Currency;use App\Entity\Gos\Language;use App\Entity\Gos\User;use App\Utils\FileService;use App\Utils\Geolocation;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Request;class CountryService{ private static string $ENTITY_TYPE = 'country'; private static string $JSON_FILETYPE = 'json'; private EntityManagerInterface $em; private FileService $fileService; private Geolocation $geolocation; private $countryRepository; private $currencyRepository; private $languageRepository; public function __construct(EntityManagerInterface $em, FileService $fileService, Geolocation $geolocation) { $this->em = $em; $this->fileService = $fileService; $this->geolocation = $geolocation; $this->countryRepository = $em->getRepository(Country::class); $this->currencyRepository = $em->getRepository(Currency::class); $this->languageRepository = $em->getRepository(Language::class); } /** * Try to look for country by geolocation or by (if there's an override) */ public function getCountry(Request $request, $user = null) { $countryReq = $request->request->get('country', null); $countryRep = $this->em->getRepository(Country::class); if ($user and $user instanceof User) { if ($user->getCountry()) { return $user->getCountry(); } } if ($request->getSession()->get('fixCountryCode')) { $countryAlpha = $request->getSession()->get('fixCountryCode'); $country = $countryRep->findOneByAlpha2($countryAlpha); } else { if (empty($countryReq)) { $myCountryCode = $request->getSession()->get('myCountryCode') ?? $this->geolocation->geolocation()['countryCode']; if ($myCountryCode) { $country = $countryRep->findOneByAlpha2($myCountryCode); } } else { $country = $countryRep->findOneByAlpha2($countryReq); } } if ($country) { return $country; } return false; } public function import() { $this->setCountries($this->getRawData()); return true; } private function setCountries($data) { foreach ($data as $ctr) { if ($ctr['status'] == 'deleted' || strlen(trim($ctr['ioc'])) == 0) continue; $country = $this->countryRepository->findOneByName($ctr['name']); $country = $country ?? new Country(); $country->setIsActive(1); $country->setAlpha2($ctr['alpha2']); $country->setAlpha3($ctr['alpha3']); if (isset($ctr['currencies']) && count($ctr['currencies']) > 0) { $currency = $this->currencyRepository->findOneByCode(reset($ctr['currencies'])); $country->setCurrency($currency); } if (isset($ctr['languages']) && count($ctr['languages']) > 0) { $language = $this->languageRepository->findOneByShort(reset($ctr['languages'])); $country->setLanguage($language); } $country->setEmoji(isset($ctr['emoji']) ? $ctr['emoji'] : null); $country->setIoc($ctr['ioc']); $country->setName($ctr['name']); $this->em->persist($country); } $this->em->flush(); return true; } private function getRawData() { $data = $this->fileService->getFile(self::$ENTITY_TYPE, self::$JSON_FILETYPE); return json_decode($data, true); } public function removeOld() { $countries = $this->countryRepository->findAll(); foreach ($countries as $country) { $this->em->remove($country); } $this->em->flush(); return true; }}