View source
<?php
namespace Drupal\apigee_edge_apiproduct_rbac_test;
use Apigee\Edge\Api\Management\Entity\ApiProduct;
use Apigee\Edge\Api\Management\Entity\ApiProductInterface;
use Apigee\Edge\Api\Management\Serializer\ApiProductSerializer;
use Apigee\Edge\Entity\EntityInterface;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Structure\AttributesProperty;
use Apigee\Edge\Structure\PagerInterface;
use Drupal\apigee_edge\Entity\Controller\ApiProductControllerInterface;
use Drupal\Core\State\StateInterface;
final class ApiProductController implements ApiProductControllerInterface {
private const STATE_API_PRODUCT_KEY_PREFIX = 'api_product_';
private const STATE_API_PRODUCT_ATTR_KEY_PREFIX = 'api_product_attr_';
private const STATE_API_PRODUCT_LIST_KEY = 'api_products';
private $innerService;
private $state;
private $entitySerializer;
public function __construct(ApiProductControllerInterface $inner_service, StateInterface $state) {
$this->innerService = $inner_service;
$this->state = $state;
$this->entitySerializer = new ApiProductSerializer();
}
public function create(EntityInterface $entity) : void {
$this->innerService
->create($entity);
$this->state
->set($this
->generateApiProductStateKey($entity
->id()), $this->entitySerializer
->normalize($entity));
$this
->updateAttributes($entity
->id(), $entity
->getAttributes());
$list = $this->state
->get(self::STATE_API_PRODUCT_LIST_KEY) ?? [];
$list[] = $entity
->id();
$this->state
->set(self::STATE_API_PRODUCT_LIST_KEY, $list);
}
public function load(string $entity_id) : EntityInterface {
$data = $this->state
->get($this
->generateApiProductStateKey($entity_id));
if (NULL === $data) {
throw new ApiException("API Product with {$entity_id} has not found in the storage.");
}
$entity = $this->entitySerializer
->denormalize($data, ApiProduct::class);
$this
->setAttributesFromStates($entity);
return $entity;
}
public function update(EntityInterface $entity) : void {
$this->state
->set($this
->generateApiProductStateKey($entity
->id()), $this->entitySerializer
->normalize($entity));
}
public function delete(string $entity_id) : EntityInterface {
$this->innerService
->delete($entity_id);
$data = $this->state
->get($this
->generateApiProductStateKey($entity_id));
if (NULL === $data) {
throw new ApiException("API Product with {$entity_id} has not found in the storage.");
}
$entity = $this->entitySerializer
->denormalize($data, ApiProduct::class);
$this->state
->delete($this
->generateApiProductStateKey($entity_id));
$list = $this->state
->get(self::STATE_API_PRODUCT_LIST_KEY) ?? [];
if ($index = array_search($entity_id, $list)) {
unset($list[$index]);
}
$this->state
->set(self::STATE_API_PRODUCT_LIST_KEY, $list);
return $entity;
}
public function getEntities(PagerInterface $pager = NULL, string $key_provider = 'id') : array {
$ids = array_map(function ($id) {
return $this
->generateApiProductStateKey($id);
}, $this->state
->get(self::STATE_API_PRODUCT_LIST_KEY) ?? []);
$entities = [];
foreach ($this->state
->getMultiple($ids) as $data) {
$entity = $this->entitySerializer
->denormalize($data, ApiProduct::class);
$this
->setAttributesFromStates($entity);
$entities[$entity
->id()] = $entity;
}
return $entities;
}
public function getAttributes(string $entity_id) : AttributesProperty {
$data = $this->state
->get($this
->generateApiProductAttributeStateKey($entity_id)) ?? [];
return $this->entitySerializer
->denormalize($data, AttributesProperty::class);
}
public function updateAttributes(string $entity_id, AttributesProperty $attributes) : AttributesProperty {
$this->state
->set($this
->generateApiProductAttributeStateKey($entity_id), $this->entitySerializer
->normalize($attributes));
return $attributes;
}
private function generateApiProductStateKey(string $entity_id) : string {
return self::STATE_API_PRODUCT_KEY_PREFIX . $entity_id;
}
private function generateApiProductAttributeStateKey(string $entity_id) : string {
return self::STATE_API_PRODUCT_ATTR_KEY_PREFIX . $entity_id;
}
private function setAttributesFromStates(ApiProductInterface $entity) {
if ($attributes = $this->state
->get($this
->generateApiProductAttributeStateKey($entity
->id()))) {
$property = $this->entitySerializer
->denormalize($attributes, AttributesProperty::class);
$entity
->setAttributes($property);
}
}
public function searchByAttribute(string $attributeName, string $attributeValue) : array {
return $this->innerService
->searchByAttribute($attributeName, $attributeValue);
}
public function getAttribute(string $entityId, string $name) : string {
return $this->innerService
->getAttribute($entityId, $name);
}
public function updateAttribute(string $entityId, string $name, string $value) : string {
return $this->innerService
->updateAttribute($entityId, $name, $value);
}
public function deleteAttribute(string $entityId, string $name) : void {
$this->innerService
->deleteAttribute($entityId, $name);
}
public function getOrganisationName() : string {
$this->innerService
->getOrganisationName();
}
public function createPager(int $limit = 0, ?string $startKey = NULL) : PagerInterface {
return $this->innerService
->createPager($limit, $startKey);
}
public function getEntityIds(PagerInterface $pager = NULL) : array {
return $this->innerService
->getEntityIds($pager);
}
}