Passed
Push — master ( 22c836...715de5 )
by Jan
03:12
created

EntityURLGenerator::cloneURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
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\Category;
33
use App\Entity\NamedDBElement;
34
use App\Entity\Part;
35
use App\Exceptions\EntityNotSupported;
36
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
37
38
class EntityURLGenerator
39
{
40
    /**
41
     * @var UrlGeneratorInterface
42
     */
43
    protected $urlGenerator;
44
45
    public function __construct(UrlGeneratorInterface $urlGenerator)
46
    {
47
        $this->urlGenerator = $urlGenerator;
48
    }
49
50
    /**
51
     * Generates an URL to the page using the given page type and element.
52
     * For the given types, the [type]URL() functions are called (e.g. infoURL()).
53
     * Not all entity class and $type combinations are supported.
54
     *
55
     * @param $entity mixed The element for which the page should be generated.
56
     * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts'
57
     * @return string The link to the desired page.
58
     * @throws EntityNotSupported Thrown if the entity is not supported for the given type.
59
     * @throws \InvalidArgumentException Thrown if the givent type is not existing.
60
     */
61
    public function getURL($entity, string $type)
62
    {
63
        switch ($type) {
64
            case 'info':
65
                return $this->infoURL($entity);
66
            case 'edit':
67
                return $this->editURL($entity);
68
            case 'create':
69
                return $this->createURL($entity);
70
            case 'clone':
71
                return $this->cloneURL($entity);
72
            case 'list':
73
            case 'list_parts':
74
                return $this->listPartsURL($entity);
75
        }
76
77
        throw new \InvalidArgumentException('Method is not supported!');
78
    }
79
80
    /**
81
     * Generates an URL to a page, where info about this entity can be viewed.
82
     *
83
     * @param $entity mixed The entity for which the info should be generated.
84
     * @return string The URL to the info page
85
     * @throws EntityNotSupported If the method is not supported for the given Entity
86
     */
87
    public function infoURL($entity): string
88
    {
89
        if ($entity instanceof Part) {
90
            return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]);
91
        }
92
93
        //Otherwise throw an error
94
        throw new EntityNotSupported('The given entity is not supported yet!');
95
    }
96
97
    /**
98
     * Generates an URL to a page, where this entity can be edited.
99
     *
100
     * @param $entity mixed The entity for which the edit link should be generated.
101
     * @return string The URL to the edit page.
102
     * @throws EntityNotSupported If the method is not supported for the given Entity
103
     */
104
    public function editURL($entity): string
105
    {
106
        if ($entity instanceof Part) {
107
            return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
108
        }
109
110
        //Otherwise throw an error
111
        throw new EntityNotSupported('The given entity is not supported yet!');
112
    }
113
114
    /**
115
     * Generates an URL to a page, where a entity of this type can be created.
116
     *
117
     * @param $entity mixed The entity for which the link should be generated.
118
     * @return string The URL to the page.
119
     * @throws EntityNotSupported If the method is not supported for the given Entity
120
     */
121
    public function createURL($entity): string
122
    {
123
        if ($entity instanceof Part) {
124
            return $this->urlGenerator->generate('part_new');
125
        }
126
127
        throw new EntityNotSupported('The given entity is not supported yet!');
128
    }
129
130
    /**
131
     * Generates an URL to a page, where a new entity can be created, that has the same informations as the
132
     * given entity (element cloning)
133
     *
134
     * @param $entity mixed The entity for which the link should be generated.
135
     * @return string The URL to the page.
136
     * @throws EntityNotSupported If the method is not supported for the given Entity
137
     */
138
    public function cloneURL($entity): string
139
    {
140
        if ($entity instanceof Part) {
141
            return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]);
142
        }
143
144
        throw new EntityNotSupported('The given entity is not supported yet!');
145
    }
146
147
    /**
148
     * Generates an URL to a page, where all parts are listed, which are contained in the given element.
149
     *
150
     * @param $entity mixed The entity for which the link should be generated.
151
     * @return string The URL to the page.
152
     * @throws EntityNotSupported If the method is not supported for the given Entity
153
     */
154
    public function listPartsURL($entity) : string
155
    {
156
        if ($entity instanceof Category) {
157
            return $this->urlGenerator->generate('app_partlists_showcategory', ['id' => $entity->getID()]);
158
        }
159
        throw new EntityNotSupported('The given entity is not supported yet!');
160
161
    }
162
163
    /**
164
     * Generates an HTML link to the info page about the given entity.
165
     *
166
     * @param $entity mixed The entity for which the info link should be generated.
167
     *
168
     * @return string The HTML of the info page link
169
     *
170
     * @throws EntityNotSupported
171
     */
172
    public function infoHTML($entity): string
173
    {
174
        $href = $this->infoURL($entity);
175
176
        if ($entity instanceof NamedDBElement) {
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\NamedDBElement.
Loading history...
177
            return sprintf('<a href="%s">%s</a>', $href, $entity->getName());
178
        }
179
180
        throw new EntityNotSupported('The given entity is not supported yet!');
181
    }
182
}
183