Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\VisitorsLocation; | |
| 6 | ||
| 7 | use Drupal\Component\Plugin\PluginBase; | |
| 8 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
| 9 | use Drupal\Core\StringTranslation\TranslatableMarkup; | |
| 10 | use Drupal\visitors\Attribute\VisitorsLocation; | |
| 11 | use Drupal\visitors\Location\LocationPlace; | |
| 12 | use Drupal\visitors\VisitorsLocationInterface; | |
| 13 | use Drupal\visitors\VisitorsLocationPluginInterface; | |
| 14 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 15 | ||
| 16 | /** | |
| 17 | * Sets the country from the browser language region. | |
| 18 | * | |
| 19 | * The first Accept-Language value is split on an underscore. en_US | |
| 20 | * stores the United States. A language with no region stores nothing. | |
| 21 | */ | |
| 22 | #[VisitorsLocation( | |
| 23 | id: 'browser_language', | |
| 24 | label: new TranslatableMarkup('Browser language'), | |
| 25 | weight: 10, | |
| 26 | )] | |
| 27 | final class BrowserLanguage extends PluginBase implements | |
| 28 | VisitorsLocationPluginInterface, | |
| 29 | ContainerFactoryPluginInterface { | |
| 30 | ||
| 31 | /** | |
| 32 | * Constructs a browser language location plugin. | |
| 33 | * | |
| 34 | * @param array $configuration | |
| 35 | * A configuration array containing information about the plugin instance. | |
| 36 | * @param string $plugin_id | |
| 37 | * The plugin ID for the plugin instance. | |
| 38 | * @param mixed $plugin_definition | |
| 39 | * The plugin implementation definition. | |
| 40 | * @param \Drupal\visitors\VisitorsLocationInterface $location | |
| 41 | * The country catalog. | |
| 42 | */ | |
| 43 | public function __construct( | |
| 44 | array $configuration, | |
| 45 | $plugin_id, | |
| 46 | $plugin_definition, | |
| 47 | protected VisitorsLocationInterface $location, | |
| 48 | ) { | |
| 49 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * {@inheritdoc} | |
| 54 | */ | |
| 55 | public static function create( | |
| 56 | ContainerInterface $container, | |
| 57 | array $configuration, | |
| 58 | $plugin_id, | |
| 59 | $plugin_definition, | |
| 60 | ): static { | |
| 61 | 11 | return new static( |
| 62 | 11 | $configuration, |
| 63 | 11 | $plugin_id, |
| 64 | 11 | $plugin_definition, |
| 65 | 11 | $container->get('visitors.location'), |
| 66 | 11 | ); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * {@inheritdoc} | |
| 71 | */ | |
| 72 | public function getLocation(string $ip, array $languages): ?LocationPlace { | |
| 73 | 11 | if ($languages === []) { |
| 74 | 1 | return NULL; |
| 75 | } | |
| 76 | 11 | $language = (string) ($languages[0] ?? ''); |
| 77 | 11 | $parts = explode('_', $language); |
| 78 | 11 | $country_code = strtoupper((string) ($parts[1] ?? '')); |
| 79 | 11 | if (!$this->location->isValidCountryCode($country_code)) { |
| 80 | 5 | return NULL; |
| 81 | } | |
| 82 | 6 | return new LocationPlace( |
| 83 | 6 | continent: $this->location->getContinent($country_code), |
| 84 | 6 | country: $country_code, |
| 85 | 6 | ); |
| 86 | } | |
| 87 | ||
| 88 | } |