You are here

class CookieMonster in Bakery Single Sign-On System 8.2

C is for cookie, its good enough for me.

Cookie monster monitors the cookie jar for cookies baked by the kitchen and tosses them out to the browser.

Hierarchy

  • class \Drupal\bakery\EventSubscriber\CookieMonster implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CookieMonster

1 file declares its use of CookieMonster
CookieMonsterTest.php in tests/src/Unit/EventSubscriber/CookieMonsterTest.php
1 string reference to 'CookieMonster'
bakery.services.yml in ./bakery.services.yml
bakery.services.yml
1 service uses CookieMonster
cookie_monster in ./bakery.services.yml
Drupal\bakery\EventSubscriber\CookieMonster

File

src/EventSubscriber/CookieMonster.php, line 19

Namespace

Drupal\bakery\EventSubscriber
View source
class CookieMonster implements EventSubscriberInterface {

  /**
   * Time services.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Bakery settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Local storage for collecting cookie changes.
   *
   * @var \Symfony\Component\HttpFoundation\ParameterBag
   */
  protected $cookieJar;

  /**
   * Cookie lifetime.
   *
   * @var int
   */
  private $freshness;

  /**
   * Cookie domain.
   *
   * @var string
   */
  private $domain;

  /**
   * Use secure cookies?
   *
   * @var bool
   */
  private $secure;
  public function __construct(TimeInterface $time, ConfigFactoryInterface $config_factory, ParameterBag $cookie_jar) {
    $this->time = $time;
    $this->config = $config_factory
      ->get('bakery.settings');
    $this->cookieJar = $cookie_jar;
    $this->domain = $this->config
      ->get('bakery_domain');
    $this->freshness = (int) $this->config
      ->get('bakery_freshness');
    $this->secure = !empty(ini_get('session.cookie_secure'));
  }

  /**
   * Adds a query parameter to check successful log in redirect URL.
   *
   * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
   *   The Event to process.
   */
  public function meWantCookie(ResponseEvent $event) {
    $response = $event
      ->getResponse();
    $this->cookieJar
      ->all();
    if ($this->cookieJar
      ->count()) {
      foreach ($this->cookieJar
        ->all() as $name => $cookie) {
        if (empty($cookie)) {
          $response->headers
            ->clearCookie($name, '/', '', $this->secure);
          $response->headers
            ->clearCookie($name, '/', $this->domain, $this->secure);
        }
        else {
          $response->headers
            ->setCookie(Cookie::create($name, $cookie, $this->time
            ->getRequestTime() + $this->freshness, '/', $this->domain, $this->secure));
        }
      }
    }
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'meWantCookie',
      -1000,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CookieMonster::$config protected property Bakery settings.
CookieMonster::$cookieJar protected property Local storage for collecting cookie changes.
CookieMonster::$domain private property Cookie domain.
CookieMonster::$freshness private property Cookie lifetime.
CookieMonster::$secure private property Use secure cookies?
CookieMonster::$time protected property Time services.
CookieMonster::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
CookieMonster::meWantCookie public function Adds a query parameter to check successful log in redirect URL.
CookieMonster::__construct public function