UserMenuExtensionHelper::configureUserMenuItems()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 4
nc 5
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeBundle\Helper;
15
16
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuCollector;
19
use Zikula\ThemeBundle\ExtensionMenu\ExtensionMenuInterface;
20
21
class UserMenuExtensionHelper
22
{
23
    public function __construct(private readonly ExtensionMenuCollector $extensionMenuCollector)
24
    {
25
    }
26
27
    public function configureUserMenu(UserMenu $menu, UserInterface $user): UserMenu
28
    {
29
        return $menu
30
            // TODO full name (with fallback to username)
31
            // ->setName($user->getFullName())
32
33
            // TODO avatar
34
            // the default user avatar is a generic avatar icon
35
            // you can return an URL with the avatar image
36
            // ->setAvatarUrl($user->getProfileImageUrl())
37
            // use this method if you don't want to display the user image
38
            // ->displayUserAvatar(false)
39
            // you can also pass an email address to use gravatar's service
40
            ->setGravatarEmail($user->getEmail())
0 ignored issues
show
Bug introduced by
The method getEmail() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Security\Core\User\InMemoryUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            ->setGravatarEmail($user->/** @scrutinizer ignore-call */ getEmail())
Loading history...
41
42
            // additional account menu items contributed by extensions
43
            ->addMenuItems(iterator_to_array($this->configureUserMenuItems()))
44
        ;
45
    }
46
47
    private function configureUserMenuItems(): iterable
48
    {
49
        $menuItemsByBundle = $this->extensionMenuCollector->getAllByContext(ExtensionMenuInterface::CONTEXT_ACCOUNT);
50
        foreach ($menuItemsByBundle as $bundleName => $extensionMenuItems) {
51
            $menuItems = is_array($extensionMenuItems) ? $extensionMenuItems : iterator_to_array($extensionMenuItems);
52
            foreach ($menuItems as $item) {
53
                yield $item;
54
            }
55
        }
56
    }
57
}
58