View source
<?php
namespace Drupal\apigee_edge\Entity;
use Apigee\Edge\Entity\EntityInterface as EdgeEntityInterface;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Structure\AttributesProperty;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
abstract class App extends AttributesAwareFieldableEdgeEntityBase implements AppInterface {
protected $decorated;
public function __construct(array $values, string $entity_type, ?EdgeEntityInterface $decorated = NULL) {
unset($values['credentials']);
if ($decorated) {
$decorated = clone $decorated;
$decorated
->setCredentials();
}
parent::__construct($values, $entity_type, $decorated);
}
public function deleteAttribute(string $name) : void {
$this->decorated
->deleteAttribute($name);
}
public function getAppFamily() : string {
return $this->decorated
->getAppFamily();
}
public function getAppId() : ?string {
return $this->decorated
->getAppId();
}
public function getAttributeValue(string $attribute) : ?string {
return $this->decorated
->getAttributeValue($attribute);
}
public function getAttributes() : AttributesProperty {
return $this->decorated
->getAttributes();
}
public function getCallbackUrl() : ?string {
return $this->decorated
->getCallbackUrl();
}
public function getCreatedAt() : ?\DateTimeImmutable {
return $this->decorated
->getCreatedAt();
}
public function getCreatedBy() : ?string {
return method_exists($this->decorated, 'getCreatedBy') ? $this->decorated
->getCreatedBy() : '';
}
public function getCredentials() : array {
if (empty($this
->getAppId())) {
return [];
}
$app_cache = \Drupal::service('apigee_edge.controller.cache.apps');
$app = $app_cache
->getEntity($this
->getAppId());
if ($app === NULL) {
$app_controller = \Drupal::service('apigee_edge.controller.app');
try {
$app = $app_controller
->loadApp($this
->getAppId());
} catch (ApiException $e) {
}
}
return $app ? $app
->getCredentials() : [];
}
public function setCredentials() : void {
}
public function getDescription() : ?string {
return $this->decorated
->getDescription();
}
public function getDisplayName() : ?string {
return $this->decorated
->getDisplayName();
}
public function getLastModifiedAt() : ?\DateTimeImmutable {
return $this->decorated
->getLastModifiedAt();
}
public function getLastModifiedBy() : ?string {
return method_exists($this->decorated, 'getLastModifiedBy') ? $this->decorated
->getLastModifiedBy() : '';
}
public function getName() : ?string {
return $this->decorated
->getName();
}
public function getScopes() : array {
return $this->decorated
->getScopes();
}
public function getStatus() : ?string {
return $this->decorated
->getStatus();
}
public function hasAttribute(string $name) : bool {
return $this->decorated
->hasAttribute($name);
}
public function setAppFamily(string $app_family) : void {
$this->decorated
->setAppFamily($app_family);
}
public function setAttribute(string $name, string $value) : void {
$this->decorated
->setAttribute($name, $value);
}
public function setAttributes(AttributesProperty $attributes) : void {
$this->decorated
->setAttributes($attributes);
}
public function setCallbackUrl(string $callback_url) : void {
$this->decorated
->setCallbackUrl($callback_url);
}
public function setDescription(string $description) : void {
$this->decorated
->setDescription($description);
}
public function setDisplayName(string $display_name) : void {
$this->decorated
->setDisplayName($display_name);
}
public function setScopes(string ...$scopes) : void {
$this->decorated
->setScopes(...$scopes);
}
protected function drupalEntityId() : ?string {
return $this->decorated
->getAppId();
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$definitions = parent::baseFieldDefinitions($entity_type);
$definitions['name']
->setRequired(TRUE);
$definitions['displayName']
->setDisplayOptions('view', [
'label' => 'inline',
'weight' => 0,
])
->setDisplayOptions('form', [
'weight' => 0,
]);
$definitions['callbackUrl'] = BaseFieldDefinition::create('app_callback_url')
->setDisplayOptions('form', [
'weight' => 1,
])
->setDisplayOptions('view', [
'label' => 'inline',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setLabel(t('Callback URL'));
$definitions['description'] = static::getBaseFieldDefinition('description', 'string_long')
->setSetting('case_sensitive', TRUE)
->setDisplayOptions('form', [
'weight' => 2,
])
->setDisplayOptions('view', [
'label' => 'inline',
'weight' => 4,
]);
$definitions['status']
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'status_property',
'weight' => 1,
]);
$definitions['createdAt']
->setDisplayOptions('view', [
'type' => 'timestamp_ago',
'label' => 'inline',
'weight' => 3,
])
->setLabel(t('Created'));
$definitions['lastModifiedAt']
->setDisplayOptions('view', [
'type' => 'timestamp_ago',
'label' => 'inline',
'weight' => 5,
])
->setLabel(t('Last updated'));
$read_only_fields = [
'appId',
'appFamily',
'createdAt',
'lastModifiedAt',
'name',
'scopes',
'status',
];
foreach ($read_only_fields as $field) {
$definitions[$field]
->setDisplayConfigurable('form', FALSE);
}
return $definitions;
}
protected static function propertyToBaseFieldTypeMap() : array {
return parent::propertyToBaseFieldBlackList() + [
'createdAt' => 'timestamp',
'lastModifiedAt' => 'timestamp',
'scopes' => 'list_string',
'status' => 'string',
];
}
protected static function propertyToBaseFieldBlackList() : array {
return array_merge(parent::propertyToBaseFieldBlackList(), [
'credentials',
]);
}
public function get($field_name) {
$value = parent::get($field_name);
if ($field_name === 'callbackUrl') {
if (!$value
->isEmpty()) {
foreach ($value
->getValue() as $id => $item) {
try {
Url::fromUri($item['value']);
} catch (\Exception $exception) {
$value
->removeItem($id);
}
}
}
}
return $value;
}
public function set($field_name, $value, $notify = TRUE) {
if ($field_name === 'callbackUrl' && !is_array($value)) {
try {
Url::fromUri($value);
} catch (\Exception $exception) {
$app = parent::set($field_name, '', $notify);
$app
->setCallbackUrl($value);
return $app;
}
}
return parent::set($field_name, $value, $notify);
}
public function label() {
$label = parent::label();
if ($label === $this
->id()) {
$label = $this
->getName();
}
return $label;
}
public function uuid() {
return $this->decorated
->getAppId();
}
public static function uniqueIdProperties() : array {
return array_merge(parent::uniqueIdProperties(), [
'appId',
]);
}
}