Test Setup Failed
Push — master ( 195648...185a1e )
by Craig
58s queued 15s
created

ExtensionMenu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - 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\GroupsModule\Menu;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\ItemInterface;
18
use Zikula\GroupsModule\Entity\Repository\GroupApplicationRepository;
19
use Zikula\GroupsModule\Entity\RepositoryInterface\GroupRepositoryInterface;
20
use Zikula\MenuModule\ExtensionMenu\ExtensionMenuInterface;
21
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
22
23
class ExtensionMenu implements ExtensionMenuInterface
24
{
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $factory;
29
30
    /**
31
     * @var PermissionApiInterface
32
     */
33
    private $permissionApi;
34
35
    /**
36
     * @var GroupRepositoryInterface
37
     */
38
    private $groupRepository;
39
40
    /**
41
     * @var GroupApplicationRepository
42
     */
43
    private $groupApplicationRepository;
44
45
    public function __construct(
46
        FactoryInterface $factory,
47
        PermissionApiInterface $permissionApi,
48
        GroupRepositoryInterface $groupRepository,
49
        GroupApplicationRepository $groupApplicationRepository
50
    ) {
51
        $this->factory = $factory;
52
        $this->permissionApi = $permissionApi;
53
        $this->groupRepository = $groupRepository;
54
        $this->groupApplicationRepository = $groupApplicationRepository;
55
    }
56
57
    public function get(string $type = self::TYPE_ADMIN): ?ItemInterface
58
    {
59
        if (self::TYPE_ADMIN === $type) {
60
            return $this->getAdmin();
61
        }
62
        if (self::TYPE_USER === $type) {
63
            return $this->getUser();
64
        }
65
        if (self::TYPE_ACCOUNT === $type) {
66
            return $this->getAccount();
67
        }
68
69
        return null;
70
    }
71
72
    private function getAdmin(): ?ItemInterface
73
    {
74
        $menu = $this->factory->createItem('adminAdminMenu');
75
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_EDIT)) {
76
            $menu->addChild('Groups list', [
77
                'route' => 'zikulagroupsmodule_group_adminlist',
78
            ])->setAttribute('icon', 'fas fa-list');
79
        }
80
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADD)) {
81
            $menu->addChild('New group', [
82
                'route' => 'zikulagroupsmodule_group_create',
83
            ])->setAttribute('icon', 'fas fa-plus');
84
        }
85
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
86
            $menu->addChild('Settings', [
87
                'route' => 'zikulagroupsmodule_config_config',
88
            ])->setAttribute('icon', 'fas fa-wrench');
89
        }
90
        $apps = $this->groupApplicationRepository->findAll();
91
        $appCount = count($apps);
92
        if (($appCount > 0) && $this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_EDIT)) {
93
            $menu->addChild('%amount% pending applications', [
94
                'route' => 'zikulagroupsmodule_group_adminlist',
95
                'routeParameters' => ['_fragment' => 'applications']
96
            ])->setExtra('translation_params', ['%amount%' => $appCount])
97
            ->setAttribute('icon', 'fas fa-exclamation-triangle');
98
        }
99
100
        return 0 === $menu->count() ? null : $menu;
101
    }
102
103
    private function getUser(): ?ItemInterface
104
    {
105
        $menu = $this->factory->createItem('groupsAccountMenu');
106
        $menu->addChild('Group list', [
107
            'route' => 'zikulagroupsmodule_group_list',
108
        ])->setAttribute('icon', 'fas fa-users');
109
110
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_EDIT)) {
111
            $menu->addChild('Groups admin', [
112
                'route' => 'zikulagroupsmodule_group_adminlist',
113
            ])->setAttribute('icon', 'fas fa-wrench');
114
        }
115
116
        return 0 === $menu->count() ? null : $menu;
117
    }
118
119
    private function getAccount(): ?ItemInterface
120
    {
121
        // Check if there is at least one group to show
122
        $groups = $this->groupRepository->findAll();
123
        if (count($groups) > 0) {
124
            $menu = $this->factory->createItem('groupsAccountMenu');
125
            $menu->addChild('Groups manager', [
126
                'route' => 'zikulagroupsmodule_group_list',
127
            ])->setAttribute('icon', 'fas fa-users');
128
        }
129
130
        return $menu ?? null;
131
    }
132
133
    public function getBundleName(): string
134
    {
135
        return 'ZikulaGroupsModule';
136
    }
137
}
138