Lines: 91.67%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\visitors\Location\LocationPlace; | |
| 9 | use Drupal\visitors\VisitorsLocationPluginInterface; | |
| 10 | use Drupal\visitors\VisitorsLocationPluginManager; | |
| 11 | use Drupal\visitors\VisitorsLocationResolverInterface; | |
| 12 | ||
| 13 | /** | |
| 14 | * Runs the location plugin selected in the Visitors settings. | |
| 15 | */ | |
| 16 | final class LocationResolver implements VisitorsLocationResolverInterface { | |
| 17 | ||
| 18 | /** | |
| 19 | * Constructs a location resolver. | |
| 20 | * | |
| 21 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 22 | * The config factory. | |
| 23 | * @param \Drupal\visitors\VisitorsLocationPluginManager $pluginManager | |
| 24 | * The location plugin manager. | |
| 25 | */ | |
| 26 | public function __construct( | |
| 27 | protected ConfigFactoryInterface $configFactory, | |
| 28 | protected VisitorsLocationPluginManager $pluginManager, | |
| 29 | 11 | ) {} |
| 30 | ||
| 31 | /** | |
| 32 | * {@inheritdoc} | |
| 33 | */ | |
| 34 | public function resolve(string $ip, array $languages): ?LocationPlace { | |
| 35 | 11 | $settings = $this->configFactory->get('visitors.settings'); |
| 36 | 11 | $id = $settings->get('location_plugin'); |
| 37 | 11 | $known = is_string($id) |
| 38 | 11 | && $id !== '' |
| 39 | 11 | && $this->pluginManager->hasDefinition($id); |
| 40 | 11 | if (!$known) { |
| 41 | 2 | return NULL; |
| 42 | } | |
| 43 | 11 | $plugin = $this->pluginManager->createInstance($id); |
| 44 | 11 | if (!$plugin instanceof VisitorsLocationPluginInterface) { |
| 45 | 0 | return NULL; |
| 46 | } | |
| 47 | 11 | return $plugin->getLocation($ip, $languages); |
| 48 | } | |
| 49 | ||
| 50 | } |