ToolsTreeBuilder::getEditNodes()   F
last analyzed

Complexity

Conditions 12
Paths 2048

Size

Total Lines 72
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 57
c 0
b 0
f 0
nc 2048
nop 0
dl 0
loc 72
rs 3.1781

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Services\Trees;
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\Attachments\PartAttachment;
27
use App\Entity\ProjectSystem\Project;
28
use App\Entity\LabelSystem\LabelProfile;
29
use App\Entity\Parts\Category;
30
use App\Entity\Parts\Footprint;
31
use App\Entity\Parts\Manufacturer;
32
use App\Entity\Parts\MeasurementUnit;
33
use App\Entity\Parts\Part;
34
use App\Entity\Parts\Storelocation;
35
use App\Entity\Parts\Supplier;
36
use App\Entity\PriceInformations\Currency;
37
use App\Entity\UserSystem\Group;
38
use App\Entity\UserSystem\User;
39
use App\Helpers\Trees\TreeViewNode;
40
use App\Services\UserSystem\UserCacheKeyGenerator;
41
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
42
use Symfony\Component\Security\Core\Security;
43
use Symfony\Contracts\Cache\ItemInterface;
44
use Symfony\Contracts\Cache\TagAwareCacheInterface;
45
use Symfony\Contracts\Translation\TranslatorInterface;
46
47
/**
48
 * This Service generates the tree structure for the tools.
49
 * Whenever you change something here, you has to clear the cache, because the results are cached for performance reasons.
50
 */
51
class ToolsTreeBuilder
52
{
53
    protected TranslatorInterface $translator;
54
    protected UrlGeneratorInterface $urlGenerator;
55
    protected UserCacheKeyGenerator $keyGenerator;
56
    protected TagAwareCacheInterface $cache;
57
    protected Security $security;
58
59
    public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator,
60
        TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator,
61
        Security $security)
62
    {
63
        $this->translator = $translator;
64
        $this->urlGenerator = $urlGenerator;
65
66
        $this->cache = $treeCache;
67
68
        $this->keyGenerator = $keyGenerator;
69
70
        $this->security = $security;
71
    }
72
73
    /**
74
     * Generates the tree for the tools menu.
75
     * The result is cached.
76
     *
77
     * @return TreeViewNode[] the array containing all Nodes for the tools menu
78
     */
79
    public function getTree(): array
80
    {
81
        $key = 'tree_tools_'.$this->keyGenerator->generateKey();
82
83
        return $this->cache->get($key, function (ItemInterface $item) {
84
            //Invalidate tree, whenever group or the user changes
85
            $item->tag(['tree_tools', 'groups', $this->keyGenerator->generateKey()]);
86
87
            $tree = [];
88
            if (!empty($this->getToolsNode())) {
89
                $tree[] = (new TreeViewNode($this->translator->trans('tree.tools.tools'), null, $this->getToolsNode()))
90
                    ->setIcon('fa-fw fa-treeview fa-solid fa-toolbox');
91
            }
92
93
            if (!empty($this->getEditNodes())) {
94
                $tree[] = (new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes()))
95
                    ->setIcon('fa-fw fa-treeview fa-solid fa-pen-to-square');
96
            }
97
            if (!empty($this->getShowNodes())) {
98
                $tree[] = (new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes()))
99
                    ->setIcon('fa-fw fa-treeview fa-solid fa-eye');
100
            }
101
            if (!empty($this->getSystemNodes())) {
102
                $tree[] = (new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes()))
103
                ->setIcon('fa-fw fa-treeview fa-solid fa-server');
104
            }
105
106
            return $tree;
107
        });
108
    }
109
110
    protected function getToolsNode(): array
111
    {
112
        $nodes = [];
113
114
        if ($this->security->isGranted('@labels.create_labels')) {
115
            $nodes[] = (new TreeViewNode(
116
                $this->translator->trans('tree.tools.tools.label_dialog'),
117
                $this->urlGenerator->generate('label_dialog')
118
            ))->setIcon("fa-treeview fa-fw fa-solid fa-qrcode");
119
        }
120
121
        if ($this->security->isGranted('@tools.label_scanner')) {
122
            $nodes[] = (new TreeViewNode(
123
                $this->translator->trans('tree.tools.tools.label_scanner'),
124
                $this->urlGenerator->generate('scan_dialog')
125
            ))->setIcon('fa-treeview fa-fw fa-solid fa-camera-retro');
126
        }
127
128
        if ($this->security->isGranted('@tools.reel_calculator')) {
129
            $nodes[] = (new TreeViewNode(
130
                $this->translator->trans('tree.tools.tools.reel_calculator'),
131
                $this->urlGenerator->generate('tools_reel_calculator')
132
            ))->setIcon('fa-treeview fa-fw fa-solid fa-ruler');
133
        }
134
        if ($this->security->isGranted('@tools.builtin_footprints_viewer')) {
135
            $nodes[] = (new TreeViewNode(
136
                $this->translator->trans('tools.builtin_footprints_viewer.title'),
137
                $this->urlGenerator->generate('tools_builtin_footprints_viewer')
138
            ))->setIcon('fa-treeview fa-fw fa-solid fa-images');
139
        }
140
        if ($this->security->isGranted('@tools.ic_logos')) {
141
            $nodes[] = (new TreeViewNode(
142
                $this->translator->trans('perm.tools.ic_logos'),
143
                $this->urlGenerator->generate('tools_ic_logos')
144
            ))->setIcon('fa-treeview fa-fw fa-solid fa-flag');
145
        }
146
147
        return $nodes;
148
    }
149
150
    /**
151
     * This functions creates a tree entries for the "edit" node of the tool's tree.
152
     *
153
     * @return TreeViewNode[]
154
     */
155
    protected function getEditNodes(): array
156
    {
157
        $nodes = [];
158
159
        if ($this->security->isGranted('read', new AttachmentType())) {
160
            $nodes[] = (new TreeViewNode(
161
                $this->translator->trans('tree.tools.edit.attachment_types'),
162
                $this->urlGenerator->generate('attachment_type_new')
163
            ))->setIcon('fa-fw fa-treeview fa-solid fa-file-alt');
164
        }
165
        if ($this->security->isGranted('read', new Category())) {
166
            $nodes[] = (new TreeViewNode(
167
                $this->translator->trans('tree.tools.edit.categories'),
168
                $this->urlGenerator->generate('category_new')
169
            ))->setIcon('fa-fw fa-treeview fa-solid fa-tags');
170
        }
171
        if ($this->security->isGranted('read', new Project())) {
172
            $nodes[] = (new TreeViewNode(
173
                $this->translator->trans('tree.tools.edit.projects'),
174
                $this->urlGenerator->generate('project_new')
175
            ))->setIcon('fa-fw fa-treeview fa-solid fa-archive');
176
        }
177
        if ($this->security->isGranted('read', new Supplier())) {
178
            $nodes[] = (new TreeViewNode(
179
                $this->translator->trans('tree.tools.edit.suppliers'),
180
                $this->urlGenerator->generate('supplier_new')
181
            ))->setIcon('fa-fw fa-treeview fa-solid fa-truck');
182
        }
183
        if ($this->security->isGranted('read', new Manufacturer())) {
184
            $nodes[] = (new TreeViewNode(
185
                $this->translator->trans('tree.tools.edit.manufacturer'),
186
                $this->urlGenerator->generate('manufacturer_new')
187
            ))->setIcon('fa-fw fa-treeview fa-solid fa-industry');
188
        }
189
        if ($this->security->isGranted('read', new Storelocation())) {
190
            $nodes[] = (new TreeViewNode(
191
                $this->translator->trans('tree.tools.edit.storelocation'),
192
                $this->urlGenerator->generate('store_location_new')
193
            ))->setIcon('fa-fw fa-treeview fa-solid fa-cube');
194
        }
195
        if ($this->security->isGranted('read', new Footprint())) {
196
            $nodes[] = (new TreeViewNode(
197
                $this->translator->trans('tree.tools.edit.footprint'),
198
                $this->urlGenerator->generate('footprint_new')
199
            ))->setIcon('fa-fw fa-treeview fa-solid fa-microchip');
200
        }
201
        if ($this->security->isGranted('read', new Currency())) {
202
            $nodes[] = (new TreeViewNode(
203
                $this->translator->trans('tree.tools.edit.currency'),
204
                $this->urlGenerator->generate('currency_new')
205
            ))->setIcon('fa-fw fa-treeview fa-solid fa-coins');
206
        }
207
        if ($this->security->isGranted('read', new MeasurementUnit())) {
208
            $nodes[] = (new TreeViewNode(
209
                $this->translator->trans('tree.tools.edit.measurement_unit'),
210
                $this->urlGenerator->generate('measurement_unit_new')
211
            ))->setIcon('fa-fw fa-treeview fa-solid fa-balance-scale');
212
        }
213
        if ($this->security->isGranted('read', new LabelProfile())) {
214
            $nodes[] = (new TreeViewNode(
215
                $this->translator->trans('tree.tools.edit.label_profile'),
216
                $this->urlGenerator->generate('label_profile_new')
217
            ))->setIcon('fa-fw fa-treeview fa-solid fa-qrcode');
218
        }
219
        if ($this->security->isGranted('create', new Part())) {
220
            $nodes[] = (new TreeViewNode(
221
                $this->translator->trans('tree.tools.edit.part'),
222
                $this->urlGenerator->generate('part_new')
223
            ))->setIcon('fa-fw fa-treeview fa-solid fa-plus-square');
224
        }
225
226
        return $nodes;
227
    }
228
229
    /**
230
     * This function creates the tree entries for the "show" node of the tools tree.
231
     *
232
     * @return TreeViewNode[]
233
     */
234
    protected function getShowNodes(): array
235
    {
236
        $show_nodes = [];
237
238
        if ($this->security->isGranted('@parts.read')) {
239
            $show_nodes[] = (new TreeViewNode(
240
                $this->translator->trans('tree.tools.show.all_parts'),
241
                $this->urlGenerator->generate('parts_show_all')
242
            ))->setIcon('fa-fw fa-treeview fa-solid fa-globe');
243
        }
244
245
        if ($this->security->isGranted('@attachments.list_attachments')) {
246
            $show_nodes[] = (new TreeViewNode(
247
                $this->translator->trans('tree.tools.show.all_attachments'),
248
                $this->urlGenerator->generate('attachment_list')
249
            ))->setIcon('fa-fw fa-treeview fa-solid fa-paperclip');
250
        }
251
252
        if ($this->security->isGranted('@tools.statistics')) {
253
            $show_nodes[] = (new TreeViewNode(
254
                $this->translator->trans('tree.tools.show.statistics'),
255
                $this->urlGenerator->generate('statistics_view')
256
            ))->setIcon('fa-fw fa-treeview fa-solid fa-chart-bar');
257
        }
258
259
        return $show_nodes;
260
    }
261
262
    /**
263
     * This function creates the tree entries for the "system" node of the tools tree.
264
     */
265
    protected function getSystemNodes(): array
266
    {
267
        $nodes = [];
268
269
        if ($this->security->isGranted('read', new User())) {
270
            $nodes[] = (new TreeViewNode(
271
                $this->translator->trans('tree.tools.system.users'),
272
                $this->urlGenerator->generate('user_new')
273
            ))->setIcon('fa-fw fa-treeview fa-solid fa-user');
274
        }
275
        if ($this->security->isGranted('read', new Group())) {
276
            $nodes[] = (new TreeViewNode(
277
                $this->translator->trans('tree.tools.system.groups'),
278
                $this->urlGenerator->generate('group_new')
279
            ))->setIcon('fa-fw fa-treeview fa-solid fa-users');
280
        }
281
282
        if ($this->security->isGranted('@system.show_logs')) {
283
            $nodes[] = (new TreeViewNode(
284
                $this->translator->trans('tree.tools.system.event_log'),
285
                $this->urlGenerator->generate('log_view')
286
            ))->setIcon('fa-fw fa-treeview fa-solid fa-binoculars');
287
        }
288
289
        if ($this->security->isGranted('@system.server_infos')) {
290
            $nodes[] = (new TreeViewNode(
291
                $this->translator->trans('tools.server_infos.title'),
292
                $this->urlGenerator->generate('tools_server_infos')
293
            ))->setIcon('fa-fw fa-treeview fa-solid fa-database');
294
        }
295
296
        return $nodes;
297
    }
298
}
299