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

ExtensionMenu   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 38
c 1
b 0
f 1
dl 0
loc 89
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdmin() 0 14 3
A getUser() 0 22 6
A get() 0 10 3
A getBundleName() 0 3 1
A __construct() 0 10 1
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\SearchModule\Menu;
15
16
use Knp\Menu\FactoryInterface;
17
use Knp\Menu\ItemInterface;
18
use Zikula\MenuModule\ExtensionMenu\ExtensionMenuInterface;
19
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
20
use Zikula\SearchModule\Entity\RepositoryInterface\SearchStatRepositoryInterface;
21
use Zikula\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
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 CurrentUserApiInterface
37
     */
38
    private $currentUserApi;
39
40
    /**
41
     * @var SearchStatRepositoryInterface
42
     */
43
    private $statRepo;
44
45
    public function __construct(
46
        FactoryInterface $factory,
47
        PermissionApiInterface $permissionApi,
48
        CurrentUserApiInterface $currentUserApi,
49
        SearchStatRepositoryInterface $searchStatRepository
50
    ) {
51
        $this->factory = $factory;
52
        $this->permissionApi = $permissionApi;
53
        $this->currentUserApi = $currentUserApi;
54
        $this->statRepo = $searchStatRepository;
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
66
        return null;
67
    }
68
69
    private function getAdmin(): ?ItemInterface
70
    {
71
        $menu = $this->factory->createItem('searchAdminMenu');
72
        $menu->addChild('User page', [
73
            'route' => 'zikulasearchmodule_search_execute',
74
        ])->setAttribute('icon', 'fas fa-search');
75
76
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
77
            $menu->addChild('Settings', [
78
                'route' => 'zikulasearchmodule_config_config',
79
            ])->setAttribute('icon', 'fas fa-wrench');
80
        }
81
82
        return 0 === $menu->count() ? null : $menu;
83
    }
84
85
    private function getUser(): ?ItemInterface
86
    {
87
        $menu = $this->factory->createItem('searchUserMenu');
88
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_ADMIN)) {
89
            $menu->addChild('Admin page', [
90
                'route' => 'zikulasearchmodule_config_config',
91
            ])->setAttribute('icon', 'fas fa-wrench');
92
        }
93
94
        if ($this->permissionApi->hasPermission($this->getBundleName() . '::', '::', ACCESS_READ)) {
95
            $menu->addChild('New search', [
96
                'route' => 'zikulasearchmodule_search_execute',
97
            ])->setAttribute('icon', 'fas fa-search');
98
99
            if ($this->currentUserApi->isLoggedIn() && $this->statRepo->countStats() > 0) {
100
                $menu->addChild('Recent searches list', [
101
                    'route' => 'zikulasearchmodule_search_recent',
102
                ])->setAttribute('icon', 'fas fa-list');
103
            }
104
        }
105
106
        return 0 === $menu->count() ? null : $menu;
107
    }
108
109
    public function getBundleName(): string
110
    {
111
        return 'ZikulaSearchModule';
112
    }
113
}
114