View source
<?php
namespace Drupal\avatars\Plugin\Avatars\Service;
use dpi\ak\Annotation\AvatarService;
use dpi\ak\AvatarConfiguration;
use dpi\ak\AvatarConfigurationInterface;
use dpi\ak\AvatarIdentifierInterface;
use dpi\ak\AvatarKit\AvatarServices\AvatarServiceInterface;
use dpi\ak\AvatarServiceDiscoveryInterface;
use dpi\ak\AvatarServiceFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class AvatarKitCommonService extends AvatarKitServiceBase implements ContainerFactoryPluginInterface {
protected $avatarKitServiceDiscovery;
protected $avatarKitServiceFactory;
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;
}
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;
protected function createService(AvatarConfigurationInterface $configuration) : AvatarServiceInterface {
$id = $this
->getDerivativeId();
return $this->avatarKitServiceFactory
->createService($id, $configuration);
}
protected function newAvatarConfiguration() : AvatarConfigurationInterface {
return new AvatarConfiguration();
}
protected function getMetadata() : AvatarService {
$id = $this
->getDerivativeId();
return $this->avatarKitServiceDiscovery
->getMetadata($id);
}
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);
}
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);
}
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');
}
public function defaultConfiguration() : array {
$configuration = parent::defaultConfiguration();
$configuration['width'] = NULL;
$configuration['height'] = NULL;
$configuration['protocol'] = NULL;
return $configuration;
}
public function getAvatar(AvatarIdentifierInterface $identifier) : ?string {
return $this
->getService()
->getAvatar($identifier);
}
public function createIdentifier() : AvatarIdentifierInterface {
return $this
->getService()
->createIdentifier();
}
}