Completed
Push — master ( f75f17...1445d7 )
by Jan
06:30
created

ToolsTreeBuilder::getSystemNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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\Helpers\TreeViewNode;
33
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
34
use Symfony\Component\Security\Core\Security;
35
use Symfony\Contracts\Cache\ItemInterface;
36
use Symfony\Contracts\Cache\TagAwareCacheInterface;
37
use Symfony\Contracts\Translation\TranslatorInterface;
38
39
/**
40
 * This Service generates the tree structure for the tools.
41
 * @package App\Services
42
 */
43
class ToolsTreeBuilder
44
{
45
46
    protected $translator;
47
    protected $urlGenerator;
48
    protected $security;
49
    protected $cache;
50
51
    public function __construct(TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator,
52
                                TagAwareCacheInterface $treeCache, Security $security)
53
    {
54
        $this->translator = $translator;
55
        $this->urlGenerator = $urlGenerator;
56
57
        $this->security = $security;
58
        $this->cache = $treeCache;
59
    }
60
61
    /**
62
     * Generates the tree for the tools menu.
63
     * The result is cached.
64
     * @return TreeViewNode The array containing all Nodes for the tools menu.
65
     */
66
    public function getTree() : array
67
    {
68
        $username = $this->security->getUser()->getUsername();
69
70
        $key = "tree_tools_" .  $username;
71
72
        return $this->cache->get($key, function (ItemInterface $item) use ($username) {
73
            //Invalidate tree, whenever group or the user changes
74
            $item->tag(["tree_tools", "groups", "user_" . $username]);
75
76
            $tree = array();
77
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $this->getEditNodes());
78
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.show'), null, $this->getShowNodes());
79
            $tree[] = new TreeViewNode($this->translator->trans('tree.tools.system'), null, $this->getSystemNodes());
80
            return $tree;
81
        });
82
    }
83
84
    /**
85
     * This functions creates a tree entries for the "edit" node of the tool's tree
86
     * @return TreeViewNode[]
87
     */
88
    protected function getEditNodes() : array
89
    {
90
        $nodes = array();
91
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.attachment_types'),
92
            $this->urlGenerator->generate('attachment_type_new'));
93
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.categories'),
94
            $this->urlGenerator->generate('category_new'));
95
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.devices'),
96
            $this->urlGenerator->generate('device_new'));
97
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.suppliers'),
98
            $this->urlGenerator->generate('supplier_new'));
99
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.manufacturer'),
100
            $this->urlGenerator->generate('manufacturer_new'));
101
102
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.storelocation'),
103
            $this->urlGenerator->generate('store_location_new'));
104
105
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.footprint'),
106
            $this->urlGenerator->generate('footprint_new'));
107
108
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.currency'),
109
            $this->urlGenerator->generate('currency_new'));
110
111
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.measurement_unit'),
112
            $this->urlGenerator->generate('measurement_unit_new'));
113
114
        $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.part'),
115
            $this->urlGenerator->generate('part_new'));
116
117
        return $nodes;
118
    }
119
120
    /**
121
     * This function creates the tree entries for the "show" node of the tools tree
122
     * @return TreeViewNode[]
123
     */
124
    protected function getShowNodes() : array
125
    {
126
        $show_nodes = array();
127
        $show_nodes[] = new TreeViewNode($this->translator->trans('tree.tools.show.all_parts'),
128
            $this->urlGenerator->generate('parts_show_all')
129
        );
130
131
        return $show_nodes;
132
    }
133
134
    /**
135
     * This function creates the tree entries for the "system" node of the tools tree.
136
     * @return array
137
     */
138
    protected function getSystemNodes() : array
139
    {
140
        $edit_nodes = array();
141
142
        $edit_nodes[] = new TreeViewNode($this->translator->trans('tree.tools.system.users'),
143
            $this->urlGenerator->generate("user_new")
144
        );
145
146
        return $edit_nodes;
147
    }
148
}
149