Passed
Push — master ( 33631f...10f39b )
by Jan
03:09
created

EntityURLGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A infoURL() 0 9 2
A editURL() 0 9 2
A createURL() 0 8 2
A cloneURL() 0 8 2
A infoHTML() 0 7 2
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
35
use App\Entity\DBElement;
36
use App\Entity\NamedDBElement;
37
use App\Entity\Part;
38
use App\Exceptions\EntityNotSupported;
39
use Doctrine\Migrations\Finder\Exception\NameIsReserved;
40
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
41
42
class EntityURLGenerator
43
{
44
    /**
45
     * @var UrlGeneratorInterface
46
     */
47
    protected $urlGenerator;
48
49
    public function __construct(UrlGeneratorInterface $urlGenerator)
50
    {
51
        $this->urlGenerator = $urlGenerator;
52
    }
53
54
    /**
55
     * Generates an URL to a page, where info about this entity can be viewed.
56
     * @param $entity mixed The entity for which the info should be generated.
57
     * @return string The URL to the info page
58
     *
59
     * @throws EntityNotSupported If the method is not supported for the given Entity
60
     */
61
    public function infoURL($entity) : string
62
    {
63
        if($entity instanceof Part)
64
        {
65
            return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]);
66
        }
67
68
        //Otherwise throw an error
69
        throw new EntityNotSupported('The given entity is not supported yet!');
70
    }
71
72
    public function editURL($entity) : string
73
    {
74
        if($entity instanceof Part)
75
        {
76
            return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]);
77
        }
78
79
        //Otherwise throw an error
80
        throw new EntityNotSupported('The given entity is not supported yet!');
81
    }
82
83
    public function createURL($entity) : string
84
    {
85
        if($entity instanceof Part)
86
        {
87
            return $this->urlGenerator->generate('part_new');
88
        }
89
90
        throw new EntityNotSupported('The given entity is not supported yet!');
91
    }
92
93
    public function cloneURL($entity) : string
94
    {
95
        if($entity instanceof Part)
96
        {
97
            return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]);
98
        }
99
100
        throw new EntityNotSupported('The given entity is not supported yet!');
101
    }
102
103
    /**
104
     * Generates an HTML link to the info page about the given entity.
105
     * @param $entity mixed The entity for which the info link should be generated.
106
     * @return string The HTML of the info page link
107
     *
108
     * @throws EntityNotSupported
109
     */
110
    public function infoHTML($entity) : string
111
    {
112
        $href = $this->infoURL($entity);
113
114
        if($entity instanceof NamedDBElement)
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\NamedDBElement.
Loading history...
115
        {
116
            return sprintf('<a href="%s">%s</a>', $href, $entity->getName());
117
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 114 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
118
119
    }
120
121
}