Completed
Push — master ( a078ee...44bad9 )
by Jan
04:05
created

ToolsTreeBuilder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 75
c 6
b 0
f 0
dl 0
loc 152
rs 10
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getShowNodes() 0 8 1
A getSystemNodes() 0 18 3
A getTree() 0 13 1
F getEditNodes() 0 66 11
1
<?php
2
/**
3
 * part-db version 0.1
4
 * Copyright (C) 2005 Christoph Lechner
5
 * http://www.cl-projects.de/.
6
 *
7
 * part-db version 0.2+
8
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
9
 * http://code.google.com/p/part-db/
10
 *
11
 * Part-DB Version 0.4+
12
 * Copyright (C) 2016 - 2019 Jan Böhmer
13
 * https://github.com/jbtronics
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28
 */
29
30
namespace App\Services;
31
32
use App\Entity\Attachments\AttachmentType;
33
use App\Entity\Devices\Device;
34
use App\Entity\Parts\Category;
35
use App\Entity\Parts\Footprint;
36
use App\Entity\Parts\Manufacturer;
37
use App\Entity\Parts\MeasurementUnit;
38
use App\Entity\Parts\Part;
39
use App\Entity\Parts\Storelocation;
40
use App\Entity\Parts\Supplier;
41
use App\Entity\PriceInformations\Currency;
42
use App\Entity\UserSystem\Group;
43
use App\Entity\UserSystem\User;
44
use App\Helpers\TreeViewNode;
45
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
46
use Symfony\Component\Security\Core\Security;
47
use Symfony\Contracts\Cache\ItemInterface;
48
use Symfony\Contracts\Cache\TagAwareCacheInterface;
49
use Symfony\Contracts\Translation\TranslatorInterface;
50
51
/**
52
 * This Service generates the tree structure for the tools.
53
 * @package App\Services
54
 */
55
class ToolsTreeBuilder
56
{
57
58
    protected $translator;
59
    protected $urlGenerator;
60
    protected $keyGenerator;
61
    protected $cache;
62
    protected $security;
63
64
    public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator,
65
                                TagAwareCacheInterface $treeCache, UserCacheKeyGenerator $keyGenerator,
66
                                Security $security)
67
    {
68
        $this->translator = $translator;
69
        $this->urlGenerator = $urlGenerator;
70
71
        $this->cache = $treeCache;
72
73
        $this->keyGenerator = $keyGenerator;
74
75
        $this->security = $security;
76
    }
77
78
    /**
79
     * Generates the tree for the tools menu.
80
     * The result is cached.
81
     * @return TreeViewNode The array containing all Nodes for the tools menu.
82
     */
83
    public function getTree() : array
84
    {
85
        $key = "tree_tools_" .  $this->keyGenerator->generateKey();
86
87
        return $this->cache->get($key, function (ItemInterface $item) {
88
            //Invalidate tree, whenever group or the user changes
89
            $item->tag(["tree_tools", "groups", $this->keyGenerator->generateKey()]);
90
91
            $tree = array();
92
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes());
93
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes());
94
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes());
95
            return $tree;
96
        });
97
    }
98
99
    /**
100
     * This functions creates a tree entries for the "edit" node of the tool's tree
101
     * @return TreeViewNode[]
102
     */
103
    protected function getEditNodes() : array
104
    {
105
        $nodes = array();
106
107
        if ($this->security->isGranted('read', new AttachmentType())) {
108
            $nodes[] = new TreeViewNode(
109
                $this->translator->trans('tree.tools.edit.attachment_types'),
110
                $this->urlGenerator->generate('attachment_type_new')
111
            );
112
        }
113
        if ($this->security->isGranted('read', new Category())) {
114
            $nodes[] = new TreeViewNode(
115
                $this->translator->trans('tree.tools.edit.categories'),
116
                $this->urlGenerator->generate('category_new')
117
            );
118
        }
119
        if ($this->security->isGranted('read', new Device())) {
120
            $nodes[] = new TreeViewNode(
121
                $this->translator->trans('tree.tools.edit.devices'),
122
                $this->urlGenerator->generate('device_new')
123
            );
124
        }
125
        if ($this->security->isGranted('read', new Supplier())) {
126
            $nodes[] = new TreeViewNode(
127
                $this->translator->trans('tree.tools.edit.suppliers'),
128
                $this->urlGenerator->generate('supplier_new')
129
            );
130
        }
131
        if ($this->security->isGranted('read', new Manufacturer())) {
132
            $nodes[] = new TreeViewNode(
133
                $this->translator->trans('tree.tools.edit.manufacturer'),
134
                $this->urlGenerator->generate('manufacturer_new')
135
            );
136
        }
137
        if ($this->security->isGranted('read', new Storelocation())) {
138
            $nodes[] = new TreeViewNode(
139
                $this->translator->trans('tree.tools.edit.storelocation'),
140
                $this->urlGenerator->generate('store_location_new')
141
            );
142
        }
143
        if ($this->security->isGranted('read', new Footprint())) {
144
            $nodes[] = new TreeViewNode(
145
                $this->translator->trans('tree.tools.edit.footprint'),
146
                $this->urlGenerator->generate('footprint_new')
147
            );
148
        }
149
        if ($this->security->isGranted('read', new Currency())) {
150
            $nodes[] = new TreeViewNode(
151
                $this->translator->trans('tree.tools.edit.currency'),
152
                $this->urlGenerator->generate('currency_new')
153
            );
154
        }
155
        if ($this->security->isGranted('read', new MeasurementUnit())) {
156
            $nodes[] = new TreeViewNode(
157
                $this->translator->trans('tree.tools.edit.measurement_unit'),
158
                $this->urlGenerator->generate('measurement_unit_new')
159
            );
160
        }
161
        if ($this->security->isGranted('create', new Part())) {
162
            $nodes[] = new TreeViewNode(
163
                $this->translator->trans('tree.tools.edit.part'),
164
                $this->urlGenerator->generate('part_new')
165
            );
166
        }
167
168
        return $nodes;
169
    }
170
171
    /**
172
     * This function creates the tree entries for the "show" node of the tools tree
173
     * @return TreeViewNode[]
174
     */
175
    protected function getShowNodes() : array
176
    {
177
        $show_nodes = array();
178
        $show_nodes[] = new TreeViewNode($this->translator->trans('tree.tools.show.all_parts'),
179
            $this->urlGenerator->generate('parts_show_all')
180
        );
181
182
        return $show_nodes;
183
    }
184
185
    /**
186
     * This function creates the tree entries for the "system" node of the tools tree.
187
     * @return array
188
     */
189
    protected function getSystemNodes() : array
190
    {
191
        $nodes = array();
192
193
        if ($this->security->isGranted('read', new User())) {
194
            $nodes[] = new TreeViewNode(
195
                $this->translator->trans('tree.tools.system.users'),
196
                $this->urlGenerator->generate("user_new")
197
            );
198
        }
199
        if ($this->security->isGranted('read', new Group())) {
200
            $nodes[] = new TreeViewNode(
201
                $this->translator->trans('tree.tools.system.groups'),
202
                $this->urlGenerator->generate('group_new')
203
            );
204
        }
205
206
        return $nodes;
207
    }
208
}
209