AdminCategoryHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentCategory() 0 12 3
A getDefaultCategory() 0 9 3
A getBundleAssignments() 0 6 1
A __construct() 0 2 1
A getCategories() 0 35 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 Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Zikula\ThemeBundle\Entity\AdminCategory;
19
20
/**
21
 * Helper function for non-dynamic admin categories.
22
 */
23
class AdminCategoryHelper
24
{
25
    public function __construct(private readonly RequestStack $requestStack, private readonly TranslatorInterface $translator)
26
    {
27
    }
28
29
    public function getCategories(): array
30
    {
31
        $categoryData = [
32
            [
33
                'name' => $this->translator->trans('System'),
34
                'slug' => 'system',
35
                'description' => $this->translator->trans('Core bundles at the heart of operation of the site.'),
36
                'icon' => 'fas fa-cogs',
37
                'sortOrder' => 0,
38
                'default' => false,
39
            ],
40
            [
41
                'name' => $this->translator->trans('Users'),
42
                'slug' => 'users',
43
                'description' => $this->translator->trans('Bundles for controlling user membership, access rights and profiles.'),
44
                'icon' => 'fas fa-users-cog',
45
                'sortOrder' => 2,
46
                'default' => false,
47
            ],
48
            [
49
                'name' => $this->translator->trans('Content'),
50
                'slug' => 'content',
51
                'description' => $this->translator->trans('Bundles for providing content to your users.'),
52
                'icon' => 'fas fa-file-contract',
53
                'sortOrder' => 3,
54
                'default' => true,
55
            ],
56
        ];
57
58
        $result = [];
59
        foreach ($categoryData as $row) {
60
            $result[] = (new AdminCategory())->setName($row['name'])->setSlug($row['slug'])->setDescription($row['description'])->setIcon($row['icon'])->setSortOrder($row['sortOrder'])->setDefault($row['default']);
61
        }
62
63
        return $result;
64
    }
65
66
    public function getCurrentCategory(): AdminCategory
67
    {
68
        $mainRequest = $this->requestStack->getMainRequest();
69
        $slug = $mainRequest->attributes->get('acslug', null);
70
71
        foreach ($this->getCategories() as $category) {
72
            if ($slug === $category->getSlug()) {
73
                return $category;
74
            }
75
        }
76
77
        return $this->getDefaultCategory();
78
    }
79
80
    public function getDefaultCategory(): AdminCategory
81
    {
82
        foreach ($this->getCategories() as $category) {
83
            if ($category->isDefault()) {
84
                return $category;
85
            }
86
        }
87
88
        throw new \InvalidArgumentException('No default category defined.');
89
    }
90
91
    public function getBundleAssignments(AdminCategory $category): array
92
    {
93
        return match ($category->getSlug()) {
94
            'system' => ['ZikulaThemeBundle'],
95
            'users' => ['ZikulaGroupsBundle', 'ZikulaLegalBundle', 'ZikulaProfileBundle', 'ZikulaUsersBundle'],
96
            'content' => [],
97
        };
98
    }
99
}
100