View source
<?php
namespace Drupal\apigee_edge_teams\Entity\Storage;
use Apigee\Edge\Exception\ApiException;
use Drupal\apigee_edge\Entity\Controller\CachedManagementApiEdgeEntityControllerProxy;
use Drupal\apigee_edge\Entity\Controller\EdgeEntityControllerInterface;
use Drupal\apigee_edge\Entity\Controller\EntityCacheAwareControllerInterface;
use Drupal\apigee_edge\Entity\Controller\ManagementApiEdgeEntityControllerProxy;
use Drupal\apigee_edge\Entity\Storage\AttributesAwareFieldableEdgeEntityStorageBase;
use Drupal\apigee_edge_teams\Entity\Controller\TeamControllerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TeamStorage extends AttributesAwareFieldableEdgeEntityStorageBase implements TeamStorageInterface {
private $teamController;
protected $entityTypeManager;
private $logger;
public function __construct(EntityTypeInterface $entity_type, CacheBackendInterface $cache_backend, MemoryCacheInterface $memory_cache, TimeInterface $system_time, TeamControllerInterface $team_controller, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config, LoggerInterface $logger) {
parent::__construct($entity_type, $cache_backend, $memory_cache, $system_time);
$this->teamController = $team_controller;
$this->cacheExpiration = $config
->get('apigee_edge_teams.team_settings')
->get('cache_expiration');
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('cache.apigee_edge_entity'), $container
->get('entity.memory_cache'), $container
->get('datetime.time'), $container
->get('apigee_edge_teams.controller.team'), $container
->get('entity_type.manager'), $container
->get('config.factory'), $container
->get('logger.channel.apigee_edge_teams'));
}
protected function entityController() : EdgeEntityControllerInterface {
if ($this->teamController instanceof EntityCacheAwareControllerInterface) {
return new CachedManagementApiEdgeEntityControllerProxy($this->teamController);
}
return new ManagementApiEdgeEntityControllerProxy($this->teamController);
}
protected function doSave($id, EntityInterface $entity) {
$team_status = $entity
->getStatus();
$result = parent::doSave($id, $entity);
try {
$this->teamController
->setStatus($entity
->id(), $team_status);
} catch (ApiException $exception) {
throw new EntityStorageException($exception
->getMessage(), $exception
->getCode(), $exception);
}
$entity
->setStatus($team_status);
return $result;
}
protected function doPostSave(EntityInterface $entity, $update) {
if (!$update) {
$team_member_role_storage = $this->entityTypeManager
->getStorage('team_member_role');
$team_roles_by_teams = $team_member_role_storage
->loadByTeam($entity);
if ($team_roles_by_teams) {
$context = [
'%team' => "{$entity->label()} ({$entity->id()})",
'link' => $entity
->toLink($this
->t('Members'), 'members')
->toString(),
];
$this->logger
->warning('Integrity check: Remnant team member roles found for new %team team.', $context);
$success = TRUE;
foreach ($team_roles_by_teams as $team_member_role) {
try {
$team_member_role
->delete();
} catch (\Exception $exception) {
$success = FALSE;
$context += Error::decodeException($exception);
$this->logger
->warning('Failed to remove remnant developer role from new %team team. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
}
}
if ($success) {
$this->logger
->info('Integrity check: Successfully removed all remnant team member roles in association with the new %team team.', $context);
}
else {
$this->logger
->critical('Integrity check: Failed to remove all remnant team member roles from the database for the new %team team.', $context);
}
}
}
parent::doPostSave($entity, $update);
}
protected function doDelete($entities) {
parent::doDelete($entities);
$team_member_role_storage = $this->entityTypeManager
->getStorage('team_member_role');
$dev_roles_by_teams = $team_member_role_storage
->loadByProperties([
'team' => array_keys($entities),
]);
foreach ($dev_roles_by_teams as $role) {
try {
$role
->delete();
} catch (\Exception $exception) {
$context = [
'%team' => "{$role->getTeam()->label()} ({$role->getTeam()->id()})",
'%developer' => $role
->getDeveloper()
->getEmail(),
];
$context += Error::decodeException($exception);
$this->logger
->critical("Integrity check: Failed to remove %developer team member's role(s) from %team team when team got deleted. @message %function (line %line of %file). <pre>@backtrace_string</pre>", $context);
}
}
}
}