View source
<?php
namespace Drupal\commerce_license\Entity;
use Drupal\commerce\EntityOwnerTrait;
use Drupal\commerce_license\Plugin\Commerce\LicenseType\LicenseTypeInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
class License extends ContentEntityBase implements LicenseInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
public function label() {
return $this
->getTypePlugin()
->buildLabel($this);
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (isset($this->original) && $this
->getState()
->getId() != $this->original
->getState()
->getId() || !isset($this->original)) {
if ($this
->getState()
->getId() == 'active') {
$this
->getTypePlugin()
->grantLicense($this);
$activation_time = \Drupal::service('datetime.time')
->getRequestTime();
if (empty($this
->getGrantedTime())) {
$this
->setGrantedTime($activation_time);
}
else {
$this
->setRenewedTime($activation_time);
}
if (!$this
->getExpiresTime()) {
$this
->setExpiresTime($this
->calculateExpirationTime($activation_time));
}
}
if (isset($this->original) && $this->original
->getState()
->getId() == 'active') {
$this
->getTypePlugin()
->revokeLicense($this);
}
}
}
public function delete() {
if ($this
->getState()
->getId() == 'active') {
$this
->getTypePlugin()
->revokeLicense($this);
}
parent::delete();
}
public function getTypePlugin() {
$license_type_manager = \Drupal::service('plugin.manager.commerce_license_type');
return $license_type_manager
->createInstance($this
->bundle());
}
public function getExpirationPluginType() {
return $this
->get('expiration_type')->target_plugin_id;
}
public function getExpirationPlugin() {
return $this
->get('expiration_type')
->first()
->getTargetInstance();
}
public function setValuesFromPlugin(LicenseTypeInterface $license_plugin) {
$license_plugin
->setConfigurationValuesOnLicense($this);
}
public function getExpiresTime() {
return $this
->get('expires')->value;
}
public function setExpiresTime($timestamp) {
$this
->set('expires', $timestamp);
return $this;
}
public function getGrantedTime() {
return $this
->get('granted')->value;
}
public function setGrantedTime($timestamp) {
$this
->set('granted', $timestamp);
return $this;
}
public function getRenewedTime() {
return $this
->get('renewed')->value;
}
public function setRenewedTime($timestamp) {
$this
->set('renewed', $timestamp);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
protected function calculateExpirationTime($start) {
$expiration_type_plugin = $this
->getExpirationPlugin();
$start_date = (new \DateTimeImmutable('@' . $start))
->setTimezone(new \DateTimeZone(commerce_license_get_user_timezone($this
->getOwner())));
$expiration_date = $expiration_type_plugin
->calculateDate($start_date);
if (is_object($expiration_date)) {
return $expiration_date
->format('U');
}
else {
return $expiration_date;
}
}
public function getState() {
return $this
->get('state')
->first();
}
public function getPurchasedEntity() {
return $this
->get('product_variation')->entity;
}
public static function getWorkflowId(LicenseInterface $license) {
return $license
->getTypePlugin()
->getWorkflowId();
}
public function getOriginatingOrder() {
return $this
->get('originating_order')->entity;
}
public function setOriginatingOrder(OrderInterface $originating_order) {
$this
->set('originating_order', $originating_order);
return $this;
}
public function getOriginatingOrderId() {
return $this
->get('originating_order')->target_id;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['type']
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 0,
]);
$fields['uid']
->setLabel(t('Owner'))
->setDescription(t('The user ID of the license owner.'))
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => 2,
'settings' => [
'link' => TRUE,
],
])
->setDisplayConfigurable('view', TRUE);
$fields['state'] = BaseFieldDefinition::create('state')
->setLabel(t('State'))
->setDescription(t('The license state.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'state_transition_form',
'weight' => 50,
])
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', [
'\\Drupal\\commerce_license\\Entity\\License',
'getWorkflowId',
]);
$fields['product_variation'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Licensed product variation'))
->setDescription(t('The licensed product variation.'))
->setRequired(TRUE)
->setSetting('target_type', 'commerce_product_variation')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => 1,
'settings' => [
'link' => TRUE,
],
])
->setDisplayConfigurable('view', TRUE);
$fields['expiration_type'] = BaseFieldDefinition::create('commerce_plugin_item:recurring_period')
->setLabel(t('Expiration type'))
->setDescription(t("The configuration for calculating the license's expiry."))
->setCardinality(1)
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_plugin_select',
'weight' => 21,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'commerce_plugin_item_default',
'weight' => 25,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the license was created.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 20,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['granted'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Granted'))
->setDescription(t('The time that the license was first granted or activated.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 21,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['renewed'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Renewed'))
->setDescription(t('The time that the license was most recently renewed.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => 22,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the license was last modified.'));
$fields['expires'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expires'))
->setDescription(t('The time that the license will expire, if any.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'commerce_license_expiration',
'weight' => 26,
'settings' => [
'date_format' => 'medium',
],
])
->setDisplayConfigurable('view', TRUE)
->setDefaultValue(0);
$fields['originating_order'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Originating order'))
->setDescription(t('The order that originated the license creation.'))
->setSetting('target_type', 'commerce_order')
->setSetting('handler', 'default')
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}