src/EventSubscriber/NavbarUserSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the AdminLTE-Bundle demo.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace App\EventSubscriber;
  9. use App\Entity\User;
  10. use KevinPapst\AdminLTEBundle\Event\NavbarUserEvent;
  11. use KevinPapst\AdminLTEBundle\Event\ShowUserEvent;
  12. use KevinPapst\AdminLTEBundle\Event\SidebarUserEvent;
  13. use KevinPapst\AdminLTEBundle\Model\UserModel;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. class NavbarUserSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var Security
  20.      */
  21.     protected $security;
  22.     /**
  23.      * @param Security $security
  24.      */
  25.     public function __construct(Security $security)
  26.     {
  27.         $this->security $security;
  28.     }
  29.     /**
  30.      * @return array
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             NavbarUserEvent::class => ['onShowUser'100],
  36.             SidebarUserEvent::class => ['onShowUser'100],
  37.         ];
  38.     }
  39.     /**
  40.      * @param ShowUserEvent $event
  41.      */
  42.     public function onShowUser(ShowUserEvent $event)
  43.     {
  44.         if (null === $this->security->getUser()) {
  45.             return;
  46.         }
  47.         /* @var $myUser User */
  48.         $myUser $this->security->getUser();
  49.         $user = new UserModel();
  50.         $user
  51.             ->setId($myUser->getId())
  52.             ->setName($myUser->getUsername())
  53.             ->setUsername($myUser->getUsername())
  54.             ->setIsOnline(true)
  55.             ->setTitle('demo user')
  56.             ->setAvatar('bundles/adminlte/images/default_avatar.png')
  57.             ->setMemberSince(new \DateTime());
  58.         //$event->setShowProfileLink(false);
  59.         //$event->addLink(new NavBarUserLink('Followers', 'home'));
  60.         $event->setUser($user);
  61.     }
  62. }