You are here

class CookieHelper in Persistent Login 8

Cookie helper service.

Hierarchy

Expanded class hierarchy of CookieHelper

1 string reference to 'CookieHelper'
persistent_login.services.yml in ./persistent_login.services.yml
persistent_login.services.yml
1 service uses CookieHelper
persistent_login.cookie_helper in ./persistent_login.services.yml
Drupal\persistent_login\CookieHelper

File

src/CookieHelper.php, line 12

Namespace

Drupal\persistent_login
View source
class CookieHelper implements CookieHelperInterface {

  /**
   * The session configuration.
   *
   * @var \Drupal\Core\Session\SessionConfigurationInterface
   */
  private $sessionConfiguration;

  /**
   * The Config Factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * Instantiates a new CookieHelper instance.
   *
   * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
   *   The session configuration.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The Config Factory service.
   */
  public function __construct(SessionConfigurationInterface $session_configuration, ConfigFactoryInterface $configFactory) {
    $this->sessionConfiguration = $session_configuration;
    $this->configFactory = $configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public function getCookieName(Request $request) {
    $prefix = $this->configFactory
      ->get('persistent_login.settings')
      ->get('cookie_prefix');

    // Use different session identifiers for HTTPS and HTTP to prevent a cookie
    // collision.
    // @see \Drupal\Core\Session\SessionConfiguration::getName()
    if ($request
      ->isSecure()) {
      $prefix = 'S' . $prefix;
    }
    $sessionConfigurationSettings = $this->sessionConfiguration
      ->getOptions($request);

    // Replace the session cookie prefix.
    $session_name = preg_replace('/^S?SESS/', '', $sessionConfigurationSettings['name']);
    return $prefix . $session_name;
  }

  /**
   * {@inheritdoc}
   */
  public function getCookieValue(Request $request) {
    return $request->cookies
      ->get($this
      ->getCookieName($request));
  }

  /**
   * {@inheritdoc}
   */
  public function hasCookie(Request $request) {
    return $request->cookies
      ->has($this
      ->getCookieName($request));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CookieHelper::$configFactory private property The Config Factory service.
CookieHelper::$sessionConfiguration private property The session configuration.
CookieHelper::getCookieName public function Returns the name of the persistent login cookie. Overrides CookieHelperInterface::getCookieName
CookieHelper::getCookieValue public function Returns the value of the persistent login cookie. Overrides CookieHelperInterface::getCookieValue
CookieHelper::hasCookie public function Checks if a request contains a persistent login cookie. Overrides CookieHelperInterface::hasCookie
CookieHelper::__construct public function Instantiates a new CookieHelper instance.