Passed
Push — master ( 769850...cbe010 )
by Jan
05:23
created

LabelProfileRepository::getPartLotsLabelProfiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software: you can redistribute it and/or modify
29
 * it under the terms of the GNU Affero General Public License as published
30
 * by the Free Software Foundation, either version 3 of the License, or
31
 * (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU Affero General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU Affero General Public License
39
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
40
 */
41
42
namespace App\Repository;
43
44
use App\Entity\LabelSystem\LabelOptions;
45
use App\Entity\LabelSystem\LabelProfile;
46
use App\Helpers\Trees\TreeViewNode;
47
use InvalidArgumentException;
48
49
class LabelProfileRepository extends NamedDBElementRepository
50
{
51
    /**
52
     * Find the profiles that are shown in the dropdown for the given type.
53
     * You should maybe use the cached version of this in LabelProfileDropdownHelper.
54
     */
55
    public function getDropdownProfiles(string $type): array
56
    {
57
        if (!in_array($type, LabelOptions::SUPPORTED_ELEMENTS, true)) {
58
            throw new InvalidArgumentException('Invalid supported_element type given.');
59
        }
60
61
        return $this->findBy([
62
            'options.supported_element' => $type,
63
            'show_in_dropdown' => true,
64
        ], ['name' => 'ASC']);
65
    }
66
67
    /**
68
     * Gets a tree of TreeViewNode elements. The root elements has $parent as parent.
69
     * The treeview is generic, that means the href are null and ID values are set.
70
     *
71
     * @return TreeViewNode[]
72
     */
73
    public function getGenericNodeTree(): array
74
    {
75
        $result = [];
76
77
        foreach (LabelOptions::SUPPORTED_ELEMENTS as $type) {
78
            $type_children = [];
79
            $entities = $this->findForSupportedElement($type);
80
            foreach ($entities as $entity) {
81
                /** @var LabelProfile $entity */
82
                $node = new TreeViewNode($entity->getName(), null, null);
83
                $node->setId($entity->getID());
84
                $type_children[] = $node;
85
            }
86
87
            if (!empty($type_children)) {
88
                //Use default label e.g. 'part_label'. $$ marks that it will be translated in TreeViewGenerator
89
                $tmp = new TreeViewNode('$$'.$type.'.label', null, $type_children);
90
91
                $result[] = $tmp;
92
            }
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Find all LabelProfiles that can be used with the given type.
100
     *
101
     * @param string $type     see LabelOptions::SUPPORTED_ELEMENTS for valid values
102
     * @param array  $order_by The way the results should be sorted. By default ordered by
103
     */
104
    public function findForSupportedElement(string $type, array $order_by = ['name' => 'ASC']): array
105
    {
106
        if (!in_array($type, LabelOptions::SUPPORTED_ELEMENTS, true)) {
107
            throw new InvalidArgumentException('Invalid supported_element type given.');
108
        }
109
110
        return $this->findBy(['options.supported_element' => $type], $order_by);
111
    }
112
113
    /**
114
     * Returns all LabelProfiles that can be used for parts
115
     * @return array
116
     */
117
    public function getPartLabelProfiles(): array
118
    {
119
        return $this->getDropdownProfiles('part');
120
    }
121
122
    /**
123
     * Returns all LabelProfiles that can be used for part lots
124
     * @return array
125
     */
126
    public function getPartLotsLabelProfiles(): array
127
    {
128
        return $this->getDropdownProfiles('part_lot');
129
    }
130
131
    /**
132
     * Returns all LabelProfiles that can be used for storelocations
133
     * @return array
134
     */
135
    public function getStorelocationsLabelProfiles(): array
136
    {
137
        return $this->getDropdownProfiles('storelocation');
138
    }
139
}
140