Skip to content

Latest commit

 

History

History
97 lines (68 loc) · 3.15 KB

5-events.md

File metadata and controls

97 lines (68 loc) · 3.15 KB

Events

The bundle is dispatching event at some points of the token life time.

When creating a token

Whenever you call Yokai\SecurityTokenBundle\Manager\TokenManagerInterface::create :

When retrieving a token

Whenever you call Yokai\SecurityTokenBundle\Manager\TokenManagerInterface::get :

When consuming a token

Whenever you call Yokai\SecurityTokenBundle\Manager\TokenManagerInterface::consume :

Subscribe to events

Subscribing to these events is as simple as registering an event listener/subscriber to Symfony's event dispatcher.

For example, lets say that you want to log errors during token retrieval, you can register a listener like this one :

<?php

namespace App\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Yokai\SecurityTokenBundle\Event as SecurityTokenEvents;

class LogSecurityTokenErrors implements EventSubscriberInterface
{
    /** @var LoggerInterface */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            SecurityTokenEvents\TokenNotFoundEvent::class => 'onTokenNotFound',
            SecurityTokenEvents\TokenExpiredEvent::class => 'onTokenExpired',
            SecurityTokenEvents\TokenAlreadyConsumedEvent::class => 'onTokenConsumed',
        ];
    }

    public function onTokenNotFound(SecurityTokenEvents\TokenNotFoundEvent $event): void
    {
        $this->logger->warning(
            'Security token was not found',
            ['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
        );
    }

    public function onTokenExpired(SecurityTokenEvents\TokenExpiredEvent $event): void
    {
        $this->logger->warning(
            'Security token was expired',
            ['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
        );
    }

    public function onTokenConsumed(SecurityTokenEvents\TokenAlreadyConsumedEvent $event): void
    {
        $this->logger->warning(
            'Security token was already consumed',
            ['purpose' => $event->getPurpose(), 'value' => $event->getValue()]
        );
    }
}

« Archive commandREADME »