MailingListManager.php in Mailing List 8
File
src/MailingListManager.php
View source
<?php
namespace Drupal\mailing_list;
use Drupal\mailing_list\SubscriptionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\PrivateTempStoreFactory;
class MailingListManager implements MailingListManagerInterface {
protected $currentUser;
protected $sessionManager;
protected $userPrivateTempstore;
public function __construct(AccountInterface $current_user, SessionManagerInterface $session_manager, PrivateTempStoreFactory $user_private_tempstore) {
$this->currentUser = $current_user;
$this->sessionManager = $session_manager;
$this->userPrivateTempstore = $user_private_tempstore;
}
public function grantSessionAccess(SubscriptionInterface $subscription) {
if ($this->currentUser
->isAnonymous() && !isset($_SESSION['session_started'])) {
$_SESSION['session_started'] = TRUE;
$this->sessionManager
->start();
}
if (!$this->sessionManager
->isStarted()) {
return;
}
$collection = $this->userPrivateTempstore
->get('mailing_list');
if (!($grants = $collection
->get('grants'))) {
$grants = [];
}
$grants[$subscription
->uuid()] = REQUEST_TIME;
$collection
->set('grants', $grants);
}
public function revokeSessionAccess(SubscriptionInterface $subscription) {
if ($this
->hasSessionAccess($subscription)) {
$collection = $this->userPrivateTempstore
->get('mailing_list');
$grants = $collection
->get('grants');
unset($grants[$subscription
->uuid()]);
$collection
->set('grants', $grants);
}
}
public function hasSessionAccess(SubscriptionInterface $subscription) {
return $this->sessionManager
->isStarted() && ($grants = $this->userPrivateTempstore
->get('mailing_list')
->get('grants')) && isset($grants[$subscription
->uuid()]);
}
}