infinito/application/src/Subscriber/UserMenuSubscriber.php

88 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Subscriber;
2018-09-13 00:24:49 +02:00
use App\Event\Menu\Topbar\UserMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
class UserMenuSubscriber implements EventSubscriberInterface
{
/**
*
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
*
* @var TranslatorInterface
*/
private $translator;
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}
public function onUserMenuConfigure(UserMenuEvent $event): void
{
$menu = $event->getItem();
2018-09-13 00:24:49 +02:00
$menu->addChild('start', [
'route' => 'homepage',
'attributes' => [
'icon' => 'fab fa-font-awesome-flag'
]
2018-09-13 00:24:49 +02:00
]);
$menu->addChild('imprint', [
'route' => 'imprint',
'attributes' => [
'icon' => 'fas fa-address-card'
]
2018-09-13 00:24:49 +02:00
]);
$dropdown = $menu->addChild('user', [
'attributes' => [
'dropdown' => true,
'icon' => 'fas fa-user'
]
2018-09-13 00:24:49 +02:00
]);
/**
* @todo replace the following check trough fos bundle
*/
if ($this->tokenStorage->getToken()->getRoles()) {
$dropdown->addChild('logout', [
2018-09-12 19:59:55 +02:00
'route' => 'logout',
'attributes' => [
2018-09-06 11:26:10 +02:00
'icon' => 'fas fa-sign-out-alt',
2018-09-13 00:24:49 +02:00
'divider_append' => true,
]
]);
} else {
$dropdown->addChild('login', [
2018-09-13 02:24:25 +02:00
'route' => 'fos_user_security_login',
'attributes' => [
2018-09-13 00:24:49 +02:00
'divider_append' => true,
'icon' => 'fas fa-sign-in-alt'
]
]);
}
$dropdown->addChild('register', [
2018-09-13 02:24:25 +02:00
'route' => 'fos_user_registration_register',
2018-09-13 00:24:49 +02:00
'attributes' => [
'icon' => 'fas fa-file-signature'
]
2018-09-13 00:24:49 +02:00
]);
}
public static function getSubscribedEvents(): array
{
return [
UserMenuEvent::EVENT => 'onUserMenuConfigure'
];
}
}