You are here

abstract class ProviderBase in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/ProviderBase.php \Drupal\geocoder\ProviderBase

Provides a base class for providers using handlers.

Hierarchy

Expanded class hierarchy of ProviderBase

2 files declare their use of ProviderBase
Random.php in src/Plugin/Geocoder/Provider/Random.php
TestProvider.php in modules/geocoder_geofield/tests/modules/geocoder_geofield_test/src/Plugin/Geocoder/Provider/TestProvider.php

File

src/ProviderBase.php, line 14

Namespace

Drupal\geocoder
View source
abstract class ProviderBase extends PluginBase implements ProviderInterface, ContainerFactoryPluginInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The cache backend used to cache geocoding data.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * Constructs a geocoder provider plugin object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   The cache backend used to cache geocoding data.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache_backend) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this->cacheBackend = $cache_backend;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('cache.geocoder'));
  }

  /**
   * {@inheritdoc}
   */
  public function geocode($source) {
    return $this
      ->process(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function reverse($latitude, $longitude) {
    return $this
      ->process(__FUNCTION__, func_get_args());
  }

  /**
   * Provides a helper callback for geocode() and reverse().
   *
   * @param string $method
   *   The method: 'geocode' or 'reverse'.
   * @param array $data
   *   An array with data to be processed. When geocoding, it contains only one
   *   item with the string. When reversing, contains 2 items: the latitude and
   *   the longitude.
   *
   * @return \Geocoder\Model\AddressCollection|\Geometry|null
   *   The Address, NULL otherwise.
   */
  protected function process($method, array $data) {
    if ($caching = $this->configFactory
      ->get('geocoder.settings')
      ->get('cache')) {

      // Try to retrieve from cache first.
      $cid = $this
        ->getCacheId($method, $data);
      if ($cache = $this->cacheBackend
        ->get($cid)) {
        return $cache->data;
      }
    }

    // Call the processor.
    $processor = $method == 'geocode' ? 'doGeocode' : 'doReverse';
    $value = call_user_func_array([
      $this,
      $processor,
    ], $data);
    if ($caching) {

      // Cache the result.
      $this->cacheBackend
        ->set($cid, $value);
    }
    return $value;
  }

  /**
   * Performs the geocoding.
   *
   * @param string $source
   *   The data to be geocoded.
   *
   * @return \Geocoder\Model\AddressCollection|\Geometry|null
   *   The Address, NULL otherwise.
   */
  protected abstract function doGeocode($source);

  /**
   * Performs the reverse geocode.
   *
   * @param float $latitude
   *   The latitude.
   * @param float $longitude
   *   The longitude.
   *
   * @return \Geocoder\Model\AddressCollection|null
   *   The AddressCollection, NULL otherwise.
   */
  protected abstract function doReverse($latitude, $longitude);

  /**
   * Builds a cached id.
   *
   * @param string $method
   *   The method: 'geocode' or 'reverse'.
   * @param array $data
   *   An array with data to be processed. When geocoding, it contains only one
   *   item with the string. When reversing, contains 2 items: the latitude and
   *   the longitude.
   *
   * @return string
   *   An unique cache id.
   */
  protected function getCacheId($method, array $data) {
    $cid = [
      $method,
      $this
        ->getPluginId(),
    ];
    $cid[] = sha1(serialize($this->configuration) . serialize($data));
    return implode(':', $cid);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProviderBase::$cacheBackend protected property The cache backend used to cache geocoding data.
ProviderBase::$configFactory protected property The config factory service.
ProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ProviderBase::doGeocode abstract protected function Performs the geocoding. 3
ProviderBase::doReverse abstract protected function Performs the reverse geocode. 3
ProviderBase::geocode public function Geocode a source string. Overrides ProviderInterface::geocode
ProviderBase::getCacheId protected function Builds a cached id.
ProviderBase::process protected function Provides a helper callback for geocode() and reverse().
ProviderBase::reverse public function Reverse geocode latitude and longitude. Overrides ProviderInterface::reverse
ProviderBase::__construct public function Constructs a geocoder provider plugin object. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.