Completed
Push — master ( 487c78...defd16 )
by Jan
03:27
created

EntityURLGenerator::deleteURL()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 7
nop 1
dl 0
loc 27
rs 8.8333
c 0
b 0
f 0
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\AttachmentType;
33
use App\Entity\Category;
34
use App\Entity\Device;
35
use App\Entity\Manufacturer;
36
use App\Entity\NamedDBElement;
37
use App\Entity\Part;
38
use App\Entity\Storelocation;
39
use App\Entity\Supplier;
40
use App\Exceptions\EntityNotSupported;
41
use Symfony\Component\HttpKernel\HttpCache\Store;
42
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
43
44
class EntityURLGenerator
45
{
46
    /**
47
     * @var UrlGeneratorInterface
48
     */
49
    protected $urlGenerator;
50
51
    public function __construct(UrlGeneratorInterface $urlGenerator)
52
    {
53
        $this->urlGenerator = $urlGenerator;
54
    }
55
56
    /**
57
     * Generates an URL to the page using the given page type and element.
58
     * For the given types, the [type]URL() functions are called (e.g. infoURL()).
59
     * Not all entity class and $type combinations are supported.
60
     *
61
     * @param $entity mixed The element for which the page should be generated.
62
     * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts'
63
     * @return string The link to the desired page.
64
     * @throws EntityNotSupported Thrown if the entity is not supported for the given type.
65
     * @throws \InvalidArgumentException Thrown if the givent type is not existing.
66
     */
67
    public function getURL($entity, string $type)
68
    {
69
        switch ($type) {
70
            case 'info':
71
                return $this->infoURL($entity);
72
            case 'edit':
73
                return $this->editURL($entity);
74
            case 'create':
75
                return $this->createURL($entity);
76
            case 'clone':
77
                return $this->cloneURL($entity);
78
            case 'list':
79
            case 'list_parts':
80
                return $this->listPartsURL($entity);
81
            case 'delete':
82
                return $this->deleteURL($entity);
83
        }
84
85
        throw new \InvalidArgumentException('Method is not supported!');
86
    }
87
88
    /**
89
     * Generates an URL to a page, where info about this entity can be viewed.
90
     *
91
     * @param $entity mixed The entity for which the info should be generated.
92
     * @return string The URL to the info page
93
     * @throws EntityNotSupported If the method is not supported for the given Entity
94
     */
95
    public function infoURL($entity): string
96
    {
97
        if ($entity instanceof Part) {
98
            return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]);
99
        }
100
101
        //Otherwise throw an error
102
        throw new EntityNotSupported('The given entity is not supported yet!');
103
    }
104
105
    /**
106
     * Generates an URL to a page, where this entity can be edited.
107
     *
108
     * @param $entity mixed The entity for which the edit link should be generated.
109
     * @return string The URL to the edit page.
110
     * @throws EntityNotSupported If the method is not supported for the given Entity
111
     */
112
    public function editURL($entity): string
113
    {
114
        if ($entity instanceof Part) {
115
            return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
116
        }
117
118
        if ($entity instanceof AttachmentType) {
119
            return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]);
120
        }
121
122
        if ($entity instanceof Category) {
123
            return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]);
124
        }
125
126
        if ($entity instanceof Device) {
127
            return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]);
128
        }
129
130
        if ($entity instanceof Supplier) {
131
            return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]);
132
        }
133
134
        if ($entity instanceof Manufacturer) {
135
            return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]);
136
        }
137
138
        if ($entity instanceof Storelocation) {
139
            return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]);
140
        }
141
142
        //Otherwise throw an error
143
        throw new EntityNotSupported('The given entity is not supported yet!');
144
    }
145
146
    /**
147
     * Generates an URL to a page, where a entity of this type can be created.
148
     *
149
     * @param $entity mixed The entity for which the link should be generated.
150
     * @return string The URL to the page.
151
     * @throws EntityNotSupported If the method is not supported for the given Entity
152
     */
153
    public function createURL($entity): string
154
    {
155
        if ($entity instanceof Part) {
156
            return $this->urlGenerator->generate('part_new');
157
        }
158
159
        if ($entity instanceof AttachmentType) {
160
            return $this->urlGenerator->generate('attachment_type_new');
161
        }
162
163
        if ($entity instanceof Category) {
164
            return $this->urlGenerator->generate('category_new');
165
        }
166
167
        if ($entity instanceof Device) {
168
            return $this->urlGenerator->generate('device_new');
169
        }
170
171
        if ($entity instanceof Supplier) {
172
            return $this->urlGenerator->generate('supplier_new');
173
        }
174
175
        if ($entity instanceof Manufacturer) {
176
            return $this->urlGenerator->generate('manufacturer_new');
177
        }
178
179
        if ($entity instanceof Storelocation) {
180
            return $this->urlGenerator->generate('store_location_new');
181
        }
182
183
        throw new EntityNotSupported('The given entity is not supported yet!');
184
    }
185
186
    /**
187
     * Generates an URL to a page, where a new entity can be created, that has the same informations as the
188
     * given entity (element cloning)
189
     *
190
     * @param $entity mixed The entity for which the link should be generated.
191
     * @return string The URL to the page.
192
     * @throws EntityNotSupported If the method is not supported for the given Entity
193
     */
194
    public function cloneURL($entity): string
195
    {
196
        if ($entity instanceof Part) {
197
            return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]);
198
        }
199
200
        throw new EntityNotSupported('The given entity is not supported yet!');
201
    }
202
203
    /**
204
     * Generates an URL to a page, where all parts are listed, which are contained in the given element.
205
     *
206
     * @param $entity mixed The entity for which the link should be generated.
207
     * @return string The URL to the page.
208
     * @throws EntityNotSupported If the method is not supported for the given Entity
209
     */
210
    public function listPartsURL($entity) : string
211
    {
212
        if ($entity instanceof Category) {
213
            return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]);
214
        }
215
        throw new EntityNotSupported('The given entity is not supported yet!');
216
217
    }
218
219
    public function deleteURL($entity) : string
220
    {
221
        if ($entity instanceof AttachmentType) {
222
            return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]);
223
        }
224
225
        if ($entity instanceof Category) {
226
            return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]);
227
        }
228
229
        if ($entity instanceof Device) {
230
            return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]);
231
        }
232
233
        if ($entity instanceof Supplier) {
234
            return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]);
235
        }
236
237
        if ($entity instanceof Manufacturer) {
238
            return $this->urlGenerator->generate('manufacturer_new', ['id' => $entity->getID()]);
239
        }
240
241
        if ($entity instanceof Storelocation) {
242
            return $this->urlGenerator->generate('store_location_new', ['id' => $entity->getID()]);
243
        }
244
245
        throw new EntityNotSupported('The given entity is not supported yet!');
246
    }
247
248
    /**
249
     * Generates an HTML link to the info page about the given entity.
250
     *
251
     * @param $entity mixed The entity for which the info link should be generated.
252
     *
253
     * @return string The HTML of the info page link
254
     *
255
     * @throws EntityNotSupported
256
     */
257
    public function infoHTML($entity): string
258
    {
259
        $href = $this->infoURL($entity);
260
261
        if ($entity instanceof NamedDBElement) {
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\NamedDBElement.
Loading history...
262
            return sprintf('<a href="%s">%s</a>', $href, $entity->getName());
263
        }
264
265
        throw new EntityNotSupported('The given entity is not supported yet!');
266
    }
267
}
268