Passed
Push — main ( 674e66...68245a )
by Axel
04:12
created

UserMenuExtensionHelper::configureUserMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 1
nc 1
nop 2
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->getEmailCanonical())
0 ignored issues
show
Bug introduced by
The method getEmailCanonical() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Nucleos\UserBundle\Model\UserInterface. ( 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 */ getEmailCanonical())
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