infinito/application/src/Subscriber/UserMenuSubscriber.php

95 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2018-09-12 22:25:22 +02:00
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',
],
]
);
$menu->addChild(
2018-09-06 11:26:10 +02:00
'imprint',
[
2018-09-12 22:25:22 +02:00
'route' => 'imprint',
2018-09-06 11:26:10 +02:00
'attributes' => [
'icon' => 'fas fa-address-card',
],
]
);
2018-09-13 02:47:57 +02:00
$dropdown = $menu->addChild($this->tokenStorage->getToken()
->getUsername() ?? 'user', [
2018-09-13 00:24:49 +02:00
'attributes' => [
'dropdown' => true,
'icon' => 'fas fa-user',
],
2018-09-13 00:24:49 +02:00
]);
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',
'divider_append' => true,
],
2018-09-13 02:47:57 +02:00
]);
$dropdown->addChild('edit profile', [
'route' => 'fos_user_profile_edit',
'attributes' => [
'icon' => 'fas fa-user-edit',
'divider_append' => true,
],
2018-09-13 00:24:49 +02:00
]);
} 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',
],
2018-09-13 00:24:49 +02:00
]);
}
$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 [
2018-09-12 22:25:22 +02:00
UserMenuEvent::EVENT => 'onUserMenuConfigure',
];
}
}