You are here

class CommerceAutoSkuManager in Commerce AutoSKU 8.2

Hierarchy

Expanded class hierarchy of CommerceAutoSkuManager

1 file declares its use of CommerceAutoSkuManager
CommerceAutoSkuForm.php in src/Form/CommerceAutoSkuForm.php
Contains \Drupal\commerce_autosku\Controller\CommerceAutoSkuForm.
1 string reference to 'CommerceAutoSkuManager'
commerce_autosku.services.yml in ./commerce_autosku.services.yml
commerce_autosku.services.yml
1 service uses CommerceAutoSkuManager
commerce_autosku.manager in ./commerce_autosku.services.yml
Drupal\commerce_autosku\CommerceAutoSkuManager

File

src/CommerceAutoSkuManager.php, line 17
Contains \Drupal\commerce_autosku\AutoEntityLabelManager.

Namespace

Drupal\commerce_autosku
View source
class CommerceAutoSkuManager implements CommerceAutoSkuManagerInterface {
  use StringTranslationTrait;

  /**
   * Automatic label is disabled.
   */
  const DISABLED = 'disabled';

  /**
   * Automatic label is enabled. Will always be generated.
   */
  const ENABLED = 'enabled';

  /**
   * Automatic label is optional. Will only be generated if no label was given.
   */
  const OPTIONAL = 'optional';

  /**
   * The content entity.
   *
   * @var ProductVariationInterface
   */
  protected $entity;

  /**
   * The type of the entity.
   *
   * @var string
   */
  protected $entity_type;

  /**
   * The bundle of the entity.
   *
   * @var string
   */
  protected $entity_bundle;

  /**
   * The bundle of the entity.
   *
   * @var ProductVariationTypeInterface
   */
  protected $bundle_entity_type;

  /**
   * Indicates if the automatic label has been applied.
   *
   * @var bool
   */
  protected $auto_sku_applied = FALSE;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The generator manager.
   *
   * @var \Drupal\commerce_autosku\CommerceAutoSkuGeneratorManagerInterface
   */
  protected $generatorManager;

  /**
   * Constructs an CommerceAutoSkuManager object.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to add the automatic label to.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager
   * @param \Drupal\commerce_autosku\CommerceAutoSkuGeneratorManagerInterface $generator_manager
   *   The generator manager.
   */
  public function __construct(ContentEntityInterface $entity, EntityTypeManagerInterface $entity_type_manager, CommerceAutoSkuGeneratorManagerInterface $generator_manager) {
    $this->entity = $entity;
    $this->entityTypeManager = $entity_type_manager;
    $this->generatorManager = $generator_manager;
    $entity_type_id = $entity
      ->getEntityTypeId();
    $bundle_id = $entity
      ->bundle();
    $bundle_entity_type_id = $entity_type_manager
      ->getDefinition($entity_type_id)
      ->getBundleEntityType();
    $this->bundle_entity_type = $this->entityTypeManager
      ->getStorage($bundle_entity_type_id)
      ->load($bundle_id);
  }

  /**
   * Checks if the entity has a label.
   *
   * @return bool
   *   True if the entity has a label property.
   */
  public function hasSku() {

    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    $definition = $this->entityTypeManager
      ->getDefinition($this->entity
      ->getEntityTypeId());

    // @todo cleanup.
    $hasKey = $definition
      ->hasKey('sku');
    if ($hasKey) {
      return $hasKey;
    }
    $entityManager = \Drupal::service('entity_field.manager');
    $fields = $entityManager
      ->getFieldDefinitions($this->entity
      ->getEntityTypeId(), $this->entity
      ->bundle());
    if (isset($fields['sku'])) {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setSku() {
    if (!$this
      ->hasSku()) {
      throw new \Exception('This entity has no SKU.');
    }
    $configuration = $this
      ->getConfig('configuration');
    $instance_id = $this
      ->getConfig('plugin');

    /** @var CommerceAutoSkuGeneratorInterface $generator */
    $generator = $this->generatorManager
      ->createInstance($instance_id, $configuration);
    $sku = $generator
      ->generate($this->entity);
    $sku_name = $this
      ->getSkuName();
    $this->entity->{$sku_name}
      ->setValue($sku);
    $this->auto_sku_applied = TRUE;
    return $sku;
  }

  /**
   * {@inheritdoc}
   */
  public function hasAutoSku() {
    return $this
      ->getConfig('mode') == self::ENABLED;
  }

  /**
   * {@inheritdoc}
   */
  public function hasOptionalAutoSku() {
    return $this
      ->getConfig('mode') == self::OPTIONAL;
  }

  /**
   * {@inheritdoc}
   */
  public function autoSkuNeeded() {
    $not_applied = empty($this->auto_sku_applied);
    $required = $this
      ->hasAutoSku() && $this->entity
      ->getSku() == '%AutoSku%';
    $optional = $this
      ->hasOptionalAutoSku() && empty($this->entity
      ->getSku());
    return $not_applied && ($required || $optional);
  }

  /**
   * Gets the field name of the entity label.
   *
   * @return string
   *   The entity label field name. Empty if the entity has no label.
   */
  public function getSkuName() {
    $sku_field = '';
    if ($this
      ->hasSku()) {
      $entityManager = \Drupal::service('entity_field.manager');

      /** @var BaseFieldDefinition[] $fields */
      $fields = $entityManager
        ->getFieldDefinitions($this->entity
        ->getEntityTypeId(), $this->entity
        ->bundle());
      $sku_field = $fields['sku']
        ->getFieldStorageDefinition()
        ->getName();
    }
    return $sku_field;
  }

  /**
   * Returns automatic SKU configuration of the product variation type.
   *
   * @param string $key
   *   The configuration key to get.
   *
   * @return bool|mixed
   */
  protected function getConfig($key) {
    $config = $this->bundle_entity_type
      ->getThirdPartySettings('commerce_autosku');
    return isset($config[$key]) ? $config[$key] : FALSE;
  }

  /**
   * Constructs the list of options for the given bundle.
   */
  public static function commerce_autosku_options() {
    return [
      CommerceAutoSkuManager::DISABLED => t('Disabled'),
      CommerceAutoSkuManager::ENABLED => t('Automatically generate the SKU and hide the label field'),
      CommerceAutoSkuManager::OPTIONAL => t('Automatically generate the SKU if the SKU field is left empty'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceAutoSkuManager::$auto_sku_applied protected property Indicates if the automatic label has been applied.
CommerceAutoSkuManager::$bundle_entity_type protected property The bundle of the entity.
CommerceAutoSkuManager::$entity protected property The content entity.
CommerceAutoSkuManager::$entityTypeManager protected property The entity type manager.
CommerceAutoSkuManager::$entity_bundle protected property The bundle of the entity.
CommerceAutoSkuManager::$entity_type protected property The type of the entity.
CommerceAutoSkuManager::$generatorManager protected property The generator manager.
CommerceAutoSkuManager::autoSkuNeeded public function Returns whether the automatic label has to be set. Overrides CommerceAutoSkuManagerInterface::autoSkuNeeded
CommerceAutoSkuManager::commerce_autosku_options public static function Constructs the list of options for the given bundle.
CommerceAutoSkuManager::DISABLED constant Automatic label is disabled.
CommerceAutoSkuManager::ENABLED constant Automatic label is enabled. Will always be generated.
CommerceAutoSkuManager::getConfig protected function Returns automatic SKU configuration of the product variation type.
CommerceAutoSkuManager::getSkuName public function Gets the field name of the entity label.
CommerceAutoSkuManager::hasAutoSku public function Determines if the entity bundle has auto entity label enabled. Overrides CommerceAutoSkuManagerInterface::hasAutoSku
CommerceAutoSkuManager::hasOptionalAutoSku public function Determines if the entity bundle has an optional automatic label. Overrides CommerceAutoSkuManagerInterface::hasOptionalAutoSku
CommerceAutoSkuManager::hasSku public function Checks if the entity has a label.
CommerceAutoSkuManager::OPTIONAL constant Automatic label is optional. Will only be generated if no label was given.
CommerceAutoSkuManager::setSku public function Sets the automatically generated entity label. Overrides CommerceAutoSkuManagerInterface::setSku
CommerceAutoSkuManager::__construct public function Constructs an CommerceAutoSkuManager object.
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.