View source
<?php
namespace Drupal\apigee_edge_teams\Entity\Storage;
use Drupal\apigee_edge_teams\Entity\TeamInvitationInterface;
use Drupal\apigee_edge_teams\Event\TeamInvitationEvent;
use Drupal\apigee_edge_teams\Event\TeamInvitationEvents;
use Drupal\Component\Datetime\TimeInterface;
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\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class TeamInvitationStorage extends SqlContentEntityStorage implements TeamInvitationStorageInterface {
protected $eventDispatcher;
protected $time;
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->eventDispatcher = $event_dispatcher;
$this->time = $time;
}
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('entity_type.bundle.info'), $container
->get('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('datetime.time'));
}
protected function invokeHook($hook, EntityInterface $entity) {
parent::invokeHook($hook, $entity);
switch ($hook) {
case 'insert':
$this->eventDispatcher
->dispatch(TeamInvitationEvents::CREATED, new TeamInvitationEvent($entity));
break;
case 'update':
$original = $entity->original;
if (!$original) {
return;
}
unset($entity->original);
if (!$original
->isDeclined() && $entity
->isDeclined()) {
$this->eventDispatcher
->dispatch(TeamInvitationEvents::DECLINED, new TeamInvitationEvent($entity));
}
if (!$original
->isAccepted() && $entity
->isAccepted()) {
$this->eventDispatcher
->dispatch(TeamInvitationEvents::ACCEPTED, new TeamInvitationEvent($entity));
}
break;
}
}
public function loadByRecipient(string $email, ?string $team_id = NULL) : array {
$query = $this
->getQuery()
->condition('recipient', $email);
if ($team_id) {
$query
->condition('team', $team_id);
}
$ids = $query
->execute();
return $this
->loadMultiple(array_values($ids));
}
public function getInvitationsToExpire() : array {
$query = $this
->getQuery()
->condition('expiry', $this->time
->getCurrentTime(), '<')
->condition('status', TeamInvitationInterface::STATUS_PENDING);
$ids = $query
->execute();
return $this
->loadMultiple(array_values($ids));
}
}