View source
<?php
namespace Drupal\apigee_edge_teams\Entity\Storage;
use Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\apigee_edge_teams\Exception\InvalidArgumentException;
use Drupal\apigee_edge_teams\TeamMembershipManagerInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TeamMemberRoleStorage extends SqlContentEntityStorage implements TeamMemberRoleStorageInterface {
protected $teamMembershipManager;
protected $logger;
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, TeamMembershipManagerInterface $team_membership_manager, LoggerInterface $logger, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, EntityTypeManagerInterface $entity_type_manager = NULL) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->teamMembershipManager = $team_membership_manager;
$this->logger = $logger;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('database'), $container
->get('entity_field.manager'), $container
->get('cache.entity'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('apigee_edge_teams.team_membership_manager'), $container
->get('logger.channel.apigee_edge_teams'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'));
}
public function loadByDeveloperAndTeam(AccountInterface $account, TeamInterface $team) : ?TeamMemberRoleInterface {
$result = $this
->loadByProperties([
'uid' => $account
->id(),
'team' => $team
->id(),
]);
$result = reset($result);
return $result ? $result : NULL;
}
public function loadByDeveloper(AccountInterface $account) : array {
return $this
->loadByProperties([
'uid' => $account
->id(),
]);
}
public function loadByTeam(TeamInterface $team) : array {
return $this
->loadByProperties([
'team' => $team
->id(),
]);
}
public function addTeamRoles(AccountInterface $account, TeamInterface $team, array $roles) : TeamMemberRoleInterface {
if ($account
->isAnonymous()) {
throw new InvalidArgumentException('Anonymous user can not be member of a team.');
}
try {
$developer_team_ids = $this->teamMembershipManager
->getTeams($account
->getEmail());
} catch (\Exception $e) {
$developer_team_ids = [];
}
if (!in_array($team
->id(), $developer_team_ids)) {
throw new InvalidArgumentException("{$account->getEmail()} is not member of {$team->id()} team.");
}
$team_member_roles = $this
->loadByDeveloperAndTeam($account, $team);
if ($team_member_roles === NULL) {
$team_member_roles = $this
->create([
'uid' => [
'target_id' => $account
->id(),
],
'team' => [
'target_id' => $team
->id(),
],
]);
}
$existing_roles = array_map(function ($item) {
return $item['target_id'];
}, $team_member_roles->roles
->getValue());
$unique_roles = array_diff(array_unique($roles), $existing_roles);
foreach ($unique_roles as $role) {
$team_member_roles->roles[] = [
'target_id' => $role,
];
}
try {
$team_member_roles
->save();
} catch (EntityStorageException $exception) {
$context = [
'%developer' => $account
->getEmail(),
'%team_id' => $team
->id(),
'%roles' => implode(',', $roles),
'link' => $team
->toLink($this
->t('Members'), 'members')
->toString(),
];
$context += Error::decodeException($exception);
$this->logger
->warning('%developer team member roles in %team_id team could not be saved. Roles: %roles. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
throw $exception;
}
return $team_member_roles;
}
public function removeTeamRoles(AccountInterface $account, TeamInterface $team, array $roles) : TeamMemberRoleInterface {
if ($account
->isAnonymous()) {
throw new InvalidArgumentException('Anonymous user can not be member of a team.');
}
try {
$developer_team_ids = $this->teamMembershipManager
->getTeams($account
->getEmail());
} catch (\Exception $e) {
$developer_team_ids = [];
}
if (!in_array($team
->id(), $developer_team_ids)) {
throw new InvalidArgumentException("{$account->getEmail()} is not member of {$team->id()} team.");
}
$team_member_roles = $this
->loadByDeveloperAndTeam($account, $team);
if ($team_member_roles === NULL) {
throw new InvalidArgumentException("{$account->getEmail()} does not have team roles in {$team->id()} team.");
}
$team_member_roles->roles = array_filter($team_member_roles->roles
->getValue(), function (array $item) use ($roles) {
return !in_array($item['target_id'], $roles);
});
try {
if (empty($team_member_roles->roles
->getValue())) {
$team_member_roles
->delete();
}
else {
$team_member_roles
->save();
}
} catch (EntityStorageException $exception) {
$context = [
'%developer' => $account
->getEmail(),
'%team_id' => $team
->id(),
'%roles' => implode(',', $roles),
'link' => $team
->toLink($this
->t('Members'), 'members')
->toString(),
];
$context += Error::decodeException($exception);
$this->logger
->warning('%developer team member roles in %team_id team could not be removed. Roles: %roles. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
throw $exception;
}
return $team_member_roles;
}
protected function doSave($id, EntityInterface $entity) {
$return = parent::doSave($id, $entity);
if ($return === SAVED_NEW) {
Cache::invalidateTags($entity
->getTeam()
->getCacheTags());
}
return $return;
}
protected function doDelete($entities) {
foreach ($entities as $entity) {
if (($team = $entity
->getTeam()) !== NULL) {
Cache::invalidateTags($team
->getCacheTags());
}
}
parent::doDelete($entities);
}
}