Completed
Push — master ( 368645...05fd75 )
by Jan
03:49
created

EntityURLGenerator::downloadURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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