You are here

abstract class AvatarKitCommonService in Avatar Kit 8.2

Same name in this branch
  1. 8.2 src/Plugin/Derivative/AvatarKitCommonService.php \Drupal\avatars\Plugin\Derivative\AvatarKitCommonService
  2. 8.2 src/Plugin/Avatars/Service/AvatarKitCommonService.php \Drupal\avatars\Plugin\Avatars\Service\AvatarKitCommonService

Automatically creates services based on plugins from Avatar Kit library.

Plugin annotation


@AvatarKitService(
  id = "avatars_ak_common",
  deriver = "Drupal\avatars\Plugin\Derivative\AvatarKitCommonService"
)

Hierarchy

Expanded class hierarchy of AvatarKitCommonService

File

src/Plugin/Avatars/Service/AvatarKitCommonService.php, line 25

Namespace

Drupal\avatars\Plugin\Avatars\Service
View source
abstract class AvatarKitCommonService extends AvatarKitServiceBase implements ContainerFactoryPluginInterface {

  /**
   * Avatar Kit service discovery.
   *
   * @var \dpi\ak\AvatarServiceDiscoveryInterface
   */
  protected $avatarKitServiceDiscovery;

  /**
   * Avatar Kit service factory.
   *
   * @var \dpi\ak\AvatarServiceFactoryInterface
   */
  protected $avatarKitServiceFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AvatarServiceDiscoveryInterface $avatarKitServiceDiscovery, AvatarServiceFactoryInterface $avatarKitServiceFactory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->avatarKitServiceDiscovery = $avatarKitServiceDiscovery;
    $this->avatarKitServiceFactory = $avatarKitServiceFactory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('avatars.avatar_kit.discovery.services'), $container
      ->get('avatars.avatar_kit.factory.services'));
  }
  use StringTranslationTrait;

  /**
   * Creates a service from Avatar Kit library.
   *
   * @param \dpi\ak\AvatarConfigurationInterface $configuration
   *   Avatar service configuration.
   *
   * @return \dpi\ak\AvatarKit\AvatarServices\AvatarServiceInterface
   *   A new service instance
   */
  protected function createService(AvatarConfigurationInterface $configuration) : AvatarServiceInterface {
    $id = $this
      ->getDerivativeId();
    return $this->avatarKitServiceFactory
      ->createService($id, $configuration);
  }

  /**
   * Create a new configuration object.
   *
   * @return \dpi\ak\AvatarConfigurationInterface
   *   A new configuration object.
   */
  protected function newAvatarConfiguration() : AvatarConfigurationInterface {
    return new AvatarConfiguration();
  }

  /**
   * Gets metadata for a service from Avatar Kit library.
   *
   * @return \dpi\ak\Annotation\AvatarService
   *   Metadata for a service from Avatar Kit library.
   */
  protected function getMetadata() : AvatarService {
    $id = $this
      ->getDerivativeId();
    return $this->avatarKitServiceDiscovery
      ->getMetadata($id);
  }

  /**
   * Create a service instance.
   *
   * @return \dpi\ak\AvatarKit\AvatarServices\AvatarServiceInterface
   *   A new service instance
   */
  protected function getService() {
    $configuration = $this
      ->newAvatarConfiguration();
    $width = $this->configuration['width'] ?? NULL;
    if (is_int($width)) {
      $configuration
        ->setWidth($width);
    }
    $height = $this->configuration['height'] ?? NULL;
    if (is_int($height)) {
      $configuration
        ->setHeight($height);
    }
    $protocol = $this->configuration['protocol'] ?? NULL;
    if (!empty($protocol)) {
      $configuration
        ->setProtocol($protocol);
    }
    return $this
      ->createService($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
    $metadata = $this
      ->getMetadata();
    $protocols = $metadata->protocols ?? [];
    $protocol_options = array_combine($protocols, $protocols);
    $form['protocol'] = [
      '#title' => $this
        ->t('Protocol'),
      '#options' => $protocol_options,
      '#default_value' => $this->configuration['protocol'] ?? NULL,
      '#required' => TRUE,
      '#type' => 'select',
    ];
    $form['width'] = [
      '#title' => $this
        ->t('Source width'),
      '#type' => 'number',
      '#default_value' => $this->configuration['width'] ?? NULL,
    ];
    $form['height'] = [
      '#title' => $this
        ->t('Source height'),
      '#type' => 'number',
      '#default_value' => $this->configuration['height'] ?? NULL,
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) : void {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['width'] = $form_state
      ->getValue('width');
    $this->configuration['height'] = $form_state
      ->getValue('height');
    $this->configuration['protocol'] = $form_state
      ->getValue('protocol');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() : array {
    $configuration = parent::defaultConfiguration();
    $configuration['width'] = NULL;
    $configuration['height'] = NULL;
    $configuration['protocol'] = NULL;
    return $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvatar(AvatarIdentifierInterface $identifier) : ?string {
    return $this
      ->getService()
      ->getAvatar($identifier);
  }

  /**
   * {@inheritdoc}
   */
  public function createIdentifier() : AvatarIdentifierInterface {
    return $this
      ->getService()
      ->createIdentifier();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvatarKitCommonService::$avatarKitServiceDiscovery protected property Avatar Kit service discovery.
AvatarKitCommonService::$avatarKitServiceFactory protected property Avatar Kit service factory.
AvatarKitCommonService::buildConfigurationForm public function Form constructor. Overrides AvatarKitServiceBase::buildConfigurationForm
AvatarKitCommonService::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AvatarKitCommonService::createIdentifier public function Overrides AvatarKitServiceBase::createIdentifier
AvatarKitCommonService::createService protected function Creates a service from Avatar Kit library.
AvatarKitCommonService::defaultConfiguration public function Gets default configuration for this plugin. Overrides AvatarKitServiceBase::defaultConfiguration
AvatarKitCommonService::getAvatar public function
AvatarKitCommonService::getMetadata protected function Gets metadata for a service from Avatar Kit library.
AvatarKitCommonService::getService protected function Create a service instance.
AvatarKitCommonService::newAvatarConfiguration protected function Create a new configuration object.
AvatarKitCommonService::submitConfigurationForm public function Form submission handler. Overrides AvatarKitServiceBase::submitConfigurationForm
AvatarKitCommonService::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
AvatarKitServiceBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
AvatarKitServiceBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
AvatarKitServiceBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
AvatarKitServiceBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
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.
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.