src/EventSubscriber/User/WidgetSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\User;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Twig\Environment;
  12. use App\Events\Core\HomepageWidgetAddEvent;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. class WidgetSubscriber implements EventSubscriberInterface
  15. {
  16.     private $twig;
  17.     public function __construct(
  18.         Environment $twig,
  19.         AuthenticationUtils $authenticationHelper
  20.     ) {
  21.         $this->authenticationHelper $authenticationHelper;
  22.         $this->twig $twig;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             HomepageWidgetAddEvent::NAME => 'onHomepageWidgetAddEvent',
  28.         ];
  29.     }
  30.     public function onHomepageWidgetAddEvent(HomepageWidgetAddEvent $event): void
  31.     {
  32.         $user $event->getUser();
  33.         if (!$user) {
  34.             $event->addWidget(
  35.                 'login',
  36.                 $this->twig->render('user/widget/login.html.twig', [
  37.                     'last_username' => $this->authenticationHelper->getLastUsername(),
  38.                     'error' => $this->authenticationHelper->getLastAuthenticationError(),
  39.                 ]),
  40.                 'left',
  41.                 2
  42.             );
  43.         }
  44.     }
  45. }