lochmueller /
language_detection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Lochmueller\LanguageDetection\Detect; |
||
| 6 | |||
| 7 | use GeoIp2\Database\Reader; |
||
| 8 | use GeoIp2\ProviderInterface; |
||
| 9 | use GeoIp2\WebService\Client; |
||
| 10 | use Lochmueller\LanguageDetection\Domain\Model\Dto\LocaleValueObject; |
||
| 11 | use Lochmueller\LanguageDetection\Domain\Model\Dto\SiteConfiguration; |
||
| 12 | use Lochmueller\LanguageDetection\Event\DetectUserLanguagesEvent; |
||
| 13 | use Lochmueller\LanguageDetection\Service\LanguageService; |
||
| 14 | use Lochmueller\LanguageDetection\Service\LocaleCollectionSortService; |
||
| 15 | use Lochmueller\LanguageDetection\Service\SiteConfigurationService; |
||
| 16 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
| 17 | |||
| 18 | class MaxMindDetect |
||
| 19 | { |
||
| 20 | public function __construct( |
||
| 21 | protected LanguageService $languageService, |
||
| 22 | protected SiteConfigurationService $siteConfigurationService, |
||
| 23 | protected LocaleCollectionSortService $localeCollectionSortService |
||
| 24 | ) {} |
||
| 25 | |||
| 26 | public function __invoke(DetectUserLanguagesEvent $event): void |
||
| 27 | { |
||
| 28 | $configuration = $this->siteConfigurationService->getConfiguration($event->getSite()); |
||
| 29 | $provider = $this->getProvider($configuration); |
||
| 30 | |||
| 31 | if (!$provider instanceof ProviderInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 32 | return; |
||
| 33 | } |
||
| 34 | |||
| 35 | try { |
||
| 36 | $result = $provider->country($event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? ''); |
||
| 37 | } catch (\Exception $exception) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | $mode = $configuration->getMaxMindMode(); |
||
| 41 | $locale = $this->languageService->getLanguageByCountry((string)$result->country->isoCode) . '_' . $result->country->isoCode; |
||
| 42 | $event->setUserLanguages($this->localeCollectionSortService->addLocaleByMode($event->getUserLanguages(), new LocaleValueObject($locale), $mode)); |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function getProvider(SiteConfiguration $siteConfiguration): ?ProviderInterface |
||
| 46 | { |
||
| 47 | if (!interface_exists(ProviderInterface::class)) { |
||
| 48 | return null; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($siteConfiguration->getMaxMindAccountId() !== 0 && $siteConfiguration->getMaxMindLicenseKey() !== '') { |
||
| 52 | return new Client($siteConfiguration->getMaxMindAccountId(), $siteConfiguration->getMaxMindLicenseKey()); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($siteConfiguration->getMaxMindDatabasePath() !== '') { |
||
| 56 | $dbPath = GeneralUtility::getFileAbsFileName($siteConfiguration->getMaxMindDatabasePath()); |
||
| 57 | if (is_file($dbPath)) { |
||
| 58 | return new Reader($dbPath); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | return null; |
||
| 63 | } |
||
| 64 | } |
||
| 65 |