BuildAccountMenuListener::createAccountMenu()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 21
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\EventListener;
13
14
use FSi\Bundle\AdminBundle\Event\MenuEvent;
15
use FSi\Bundle\AdminBundle\Menu\Item\Item;
16
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem;
17
use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Translation\TranslatorInterface;
20
21
class BuildAccountMenuListener
22
{
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    /**
29
     * @var TokenStorageInterface
30
     */
31
    private $tokenStorage;
32
33
    public function __construct(TranslatorInterface $translator, TokenStorageInterface $tokenStorage)
34
    {
35
        $this->translator = $translator;
36
        $this->tokenStorage = $tokenStorage;
37
    }
38
39
    public function createAccountMenu(MenuEvent $event): Item
40
    {
41
        if (!$this->hasUserLoggedIn()) {
42
            return $event->getMenu();
43
        }
44
45
        $rootItem = $this->createRootItem();
46
47
        if ($this->canChangeUserPassword()) {
48
            $changePasswordItem = new RoutableItem('account.change-password', 'fsi_admin_change_password');
49
            $changePasswordItem->setLabel($this->translator->trans('admin.change_password', [], 'FSiAdminSecurity'));
50
            $rootItem->addChild($changePasswordItem);
51
        }
52
53
        $logoutItem = new RoutableItem('account.logout', 'fsi_admin_security_user_logout');
54
        $logoutItem->setLabel($this->translator->trans('admin.logout', [], 'FSiAdminSecurity'));
55
        $rootItem->addChild($logoutItem);
56
57
        $event->getMenu()->addChild($rootItem);
58
59
        return $event->getMenu();
60
    }
61
62
    private function createRootItem(): Item
63
    {
64
        $rootItem = new Item('account');
65
66
        $rootItem->setLabel(
67
            $this->translator->trans(
68
                'admin.welcome',
69
                ['%username%' => $this->tokenStorage->getToken()->getUsername()],
70
                'FSiAdminSecurity'
71
            )
72
        );
73
74
        $rootItem->setOptions([
75
            'attr' => [
76
                'id' => 'account',
77
            ]
78
        ]);
79
80
        return $rootItem;
81
    }
82
83
    private function hasUserLoggedIn(): bool
84
    {
85
        return $this->tokenStorage->getToken() !== null;
86
    }
87
88
    private function canChangeUserPassword(): bool
89
    {
90
        return $this->tokenStorage->getToken()->getUser() instanceof ChangeablePasswordInterface;
91
    }
92
}
93