Completed
Branch master (c8bc5d)
by Jan
11:58 queued 07:17
created

EntityURLGenerator::editURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.8333
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\Attachment;
33
use App\Entity\Attachments\AttachmentType;
34
use App\Entity\Base\DBElement;
35
use App\Entity\Parts\Category;
36
use App\Entity\Devices\Device;
37
use App\Entity\Parts\Footprint;
38
use App\Entity\Parts\Manufacturer;
39
use App\Entity\Base\NamedDBElement;
40
use App\Entity\Parts\MeasurementUnit;
41
use App\Entity\Parts\Part;
42
use App\Entity\Parts\Storelocation;
43
use App\Entity\Parts\Supplier;
44
use App\Entity\PriceInformations\Currency;
45
use App\Entity\UserSystem\User;
46
use App\Exceptions\EntityNotSupported;
47
use Symfony\Component\HttpKernel\HttpCache\Store;
48
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
49
50
/**
51
 * This service can be used to generate links to controllers for different aspects of an entity
52
 * (like info, edit, delete, etc.)
53
 * Useful for Twig, where you can generate a link to an entity using a filter.
54
 * @package App\Services
55
 */
56
class EntityURLGenerator
57
{
58
    /**
59
     * @var UrlGeneratorInterface
60
     */
61
    protected $urlGenerator;
62
63
    public function __construct(UrlGeneratorInterface $urlGenerator)
64
    {
65
        $this->urlGenerator = $urlGenerator;
66
    }
67
68
    /**
69
     * Finds the controller name for the class of the entity using the given map.
70
     * Throws an exception if the entity class is not known to the map.
71
     * @param array $map The map that should be used for determing the controller
72
     * @param $entity mixed The entity for which the controller name should be determined.
73
     * @return string The name of the controller fitting the entity class
74
     * @throws EntityNotSupported
75
     */
76
    protected function mapToController(array $map, $entity): string
77
    {
78
        $class = get_class($entity);
79
80
        //Check if we have an mapping for the given class
81
        if (!array_key_exists($class, $map)) {
82
            throw new EntityNotSupported('The given entity is not supported yet!');
83
        }
84
85
        return $map[$class];
86
    }
87
88
    /**
89
     * Generates an URL to the page using the given page type and element.
90
     * For the given types, the [type]URL() functions are called (e.g. infoURL()).
91
     * Not all entity class and $type combinations are supported.
92
     *
93
     * @param $entity mixed The element for which the page should be generated.
94
     * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts'
95
     * @return string The link to the desired page.
96
     * @throws EntityNotSupported Thrown if the entity is not supported for the given type.
97
     * @throws \InvalidArgumentException Thrown if the givent type is not existing.
98
     */
99
    public function getURL($entity, string $type)
100
    {
101
        switch ($type) {
102
            case 'info':
103
                return $this->infoURL($entity);
104
            case 'edit':
105
                return $this->editURL($entity);
106
            case 'create':
107
                return $this->createURL($entity);
108
            case 'clone':
109
                return $this->cloneURL($entity);
110
            case 'list':
111
            case 'list_parts':
112
                return $this->listPartsURL($entity);
113
            case 'delete':
114
                return $this->deleteURL($entity);
115
            case 'file_download':
116
                return $this->downloadURL($entity);
117
            case 'file_view':
118
                return $this->viewURL($entity);
119
        }
120
121
        throw new \InvalidArgumentException('Method is not supported!');
122
    }
123
124
    public function viewURL($entity): string
125
    {
126
        if ($entity instanceof Attachment) {
127
            if ($entity->isExternal()) { //For external attachments, return the link to external path
128
                return $entity->getURL();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $entity->getURL() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
129
            }
130
            return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]);
131
        }
132
133
        //Otherwise throw an error
134
        throw new EntityNotSupported('The given entity is not supported yet!');
135
    }
136
137
    public function downloadURL($entity): string
138
    {
139
        if ($entity instanceof Attachment) {
140
            if ($entity->isExternal()) { //For external attachments, return the link to external path
141
                return $entity->getURL();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $entity->getURL() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
142
            }
143
            return $this->urlGenerator->generate('attachment_download', ['id' => $entity->getID()]);
144
        }
145
146
        //Otherwise throw an error
147
        throw new EntityNotSupported('The given entity is not supported yet!');
148
    }
149
150
    /**
151
     * Generates an URL to a page, where info about this entity can be viewed.
152
     *
153
     * @param $entity mixed The entity for which the info should be generated.
154
     * @return string The URL to the info page
155
     * @throws EntityNotSupported If the method is not supported for the given Entity
156
     */
157
    public function infoURL(DBElement $entity): string
158
    {
159
        $map = [
160
            Part::class => 'part_info'
161
        ];
162
163
        return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
164
    }
165
166
    /**
167
     * Generates an URL to a page, where this entity can be edited.
168
     *
169
     * @param $entity mixed The entity for which the edit link should be generated.
170
     * @return string The URL to the edit page.
171
     * @throws EntityNotSupported If the method is not supported for the given Entity
172
     */
173
    public function editURL($entity): string
174
    {
175
        $map = [
176
            Part::class => 'part_edit',
177
            AttachmentType::class => 'attachment_type_edit',
178
            Category::class => 'category_edit',
179
            Device::class => 'device_edit',
180
            Supplier::class => 'supplier_edit',
181
            Manufacturer::class => 'manufacturer_edit',
182
            Storelocation::class => 'store_location_edit',
183
            Footprint::class => 'footprint_edit',
184
            User::class => 'user_edit',
185
            Currency::class => 'currency_edit',
186
            MeasurementUnit::class => 'measurement_unit_edit'
187
        ];
188
189
        return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
190
    }
191
192
    /**
193
     * Generates an URL to a page, where a entity of this type can be created.
194
     *
195
     * @param $entity mixed The entity for which the link should be generated.
196
     * @return string The URL to the page.
197
     * @throws EntityNotSupported If the method is not supported for the given Entity
198
     */
199
    public function createURL($entity): string
200
    {
201
        $map = [
202
            Part::class => 'part_new',
203
            AttachmentType::class => 'attachment_type_new',
204
            Category::class => 'category_new',
205
            Device::class => 'device_new',
206
            Supplier::class => 'supplier_new',
207
            Manufacturer::class => 'manufacturer_new',
208
            Storelocation::class => 'store_location_new',
209
            Footprint::class => 'footprint_new',
210
            User::class => 'user_new',
211
            Currency::class => 'currency_new',
212
            MeasurementUnit::class => 'measurement_unit_new'
213
        ];
214
215
        return $this->urlGenerator->generate($this->mapToController($map, $entity));
216
    }
217
218
    /**
219
     * Generates an URL to a page, where a new entity can be created, that has the same informations as the
220
     * given entity (element cloning)
221
     *
222
     * @param $entity mixed The entity for which the link should be generated.
223
     * @return string The URL to the page.
224
     * @throws EntityNotSupported If the method is not supported for the given Entity
225
     */
226
    public function cloneURL(DBElement $entity): string
227
    {
228
        $map = [
229
            Part::class => 'part_clone'
230
        ];
231
232
        return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
233
    }
234
235
    /**
236
     * Generates an URL to a page, where all parts are listed, which are contained in the given element.
237
     *
238
     * @param $entity mixed The entity for which the link should be generated.
239
     * @return string The URL to the page.
240
     * @throws EntityNotSupported If the method is not supported for the given Entity
241
     */
242
    public function listPartsURL(DBElement $entity): string
243
    {
244
        $map = [
245
            Category::class => 'part_list_category',
246
            Footprint::class => 'part_list_footprint',
247
            Manufacturer::class => 'part_list_manufacturer'
248
        ];
249
250
        return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
251
    }
252
253
    public function deleteURL(DBElement $entity): string
254
    {
255
        $map = [
256
            Part::class => 'part_delete',
257
            AttachmentType::class => 'attachment_type_delete',
258
            Category::class => 'category_delete',
259
            Device::class => 'device_delete',
260
            Supplier::class => 'supplier_delete',
261
            Manufacturer::class => 'manufacturer_delete',
262
            Storelocation::class => 'store_location_delete',
263
            Footprint::class => 'footprint_delete',
264
            User::class => 'user_delete',
265
            Currency::class => 'currency_delete',
266
            MeasurementUnit::class => 'measurement_unit_delete'
267
        ];
268
269
        return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]);
270
    }
271
}
272