Passed
Push — master ( 62e1a3...672be2 )
by Daniel
06:09
created

Route::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Entity\Core;
15
16
use ApiPlatform\Core\Annotation\ApiResource;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\Common\Collections\Collection;
19
use Silverback\ApiComponentsBundle\Annotation as Silverback;
20
use Silverback\ApiComponentsBundle\Entity\Utility\IdTrait;
21
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
22
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
23
use Symfony\Component\Validator\Constraints as Assert;
24
25
/**
26
 * Although a user will be able to get the routes and the tree of data down to getting the ID for a component
27
 * fetching a component will be restricted based on the route it is within.
28
 *
29
 * @author Daniel West <[email protected]>
30
 *
31
 * @Silverback\Timestamped
32
 * @ApiResource(
33
 *     mercure=true,
34
 *     collectionOperations={
35
 *         "get",
36
 *         "post",
37
 *         "generate"={ "method"="POST", "path"="/routes/generate" }
38
 *     },
39
 *     itemOperations={
40
 *         "get"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" },
41
 *         "delete"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" },
42
 *         "put"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" },
43
 *         "patch"={ "requirements"={"id"="(.+)"}, "security"="is_granted('read_route', object)" }
44
 *     }
45
 * )
46
 * @Assert\Expression(
47
 *     "!(this.getPage() == null & this.getPageData() == null)",
48
 *     message="Please specify either page or pageData."
49
 * )
50
 * @Assert\Expression(
51
 *     "!(this.getPage() != null & this.getPageData() != null)",
52
 *     message="Please specify either page or pageData, not both."
53
 * )
54
 * @UniqueEntity("name", message="The route name must be unique.")
55
 * @UniqueEntity("path", message="The route path must be unique.")
56
 */
57
class Route
58
{
59
    use IdTrait;
60
    use TimestampedTrait;
61
62
    /**
63
     * @Assert\NotBlank
64
     */
65
    private string $path = '';
66
67
    /**
68
     * @Assert\NotNull
69
     */
70
    private string $name;
71
72
    private ?Route $redirect = null;
73
74
    private Collection $redirectedFrom;
75
76
    private ?Page $page = null;
77
78
    private ?AbstractPageData $pageData = null;
79
80 3
    public function __construct()
81
    {
82 3
        $this->redirectedFrom = new ArrayCollection();
83 3
    }
84
85
    public function getPath(): string
86
    {
87
        return $this->path;
88
    }
89
90 1
    public function setPath(string $path): self
91
    {
92 1
        $this->path = $path;
93
94 1
        return $this;
95
    }
96
97
    public function getName(): string
98
    {
99
        return $this->name;
100
    }
101
102 1
    public function setName(string $name): self
103
    {
104 1
        $this->name = $name;
105
106 1
        return $this;
107
    }
108
109
    public function getRedirect(): ?self
110
    {
111
        return $this->redirect;
112
    }
113
114
    public function setRedirect(?self $redirect): self
115
    {
116
        $this->redirect = $redirect;
117
        if ($redirect) {
118
            $redirect->addRedirectedFrom($this);
119
        }
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return Collection|Route[]
126
     */
127
    public function getRedirectedFrom()
128
    {
129
        return $this->redirectedFrom;
130
    }
131
132
    /**
133
     * @param array|Collection $redirectedFrom
134
     */
135
    public function setRedirectedFrom($redirectedFrom): self
136
    {
137
        $isArray = \is_array($redirectedFrom);
138
        if (!$isArray && !$redirectedFrom instanceof Collection) {
0 ignored issues
show
introduced by
$redirectedFrom is always a sub-type of Doctrine\Common\Collections\Collection.
Loading history...
139
            throw new \InvalidArgumentException('setRedirectedFrom requires an array or Collection');
140
        }
141
        $this->redirectedFrom = $isArray ? new ArrayCollection($redirectedFrom) : $redirectedFrom;
0 ignored issues
show
Bug introduced by
It seems like $redirectedFrom can also be of type Doctrine\Common\Collections\Collection; however, parameter $elements of Doctrine\Common\Collecti...llection::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        $this->redirectedFrom = $isArray ? new ArrayCollection(/** @scrutinizer ignore-type */ $redirectedFrom) : $redirectedFrom;
Loading history...
142
143
        return $this;
144
    }
145
146
    public function addRedirectedFrom(self $redirectedFrom): self
147
    {
148
        if (!$this->redirectedFrom->contains($redirectedFrom)) {
149
            $this->redirectedFrom->add($redirectedFrom);
150
        }
151
152
        return $this;
153
    }
154
155
    public function getPage(): ?Page
156
    {
157
        return $this->page;
158
    }
159
160
    public function setPage(?Page $page): self
161
    {
162
        $this->page = $page;
163
        if ($this->page) {
164
            $this->page->setRoute($this);
165
        }
166
167
        return $this;
168
    }
169
170
    public function getPageData(): ?AbstractPageData
171
    {
172
        return $this->pageData;
173
    }
174
175
    public function setPageData(?AbstractPageData $pageData): self
176
    {
177
        $this->pageData = $pageData;
178
        if ($this->pageData) {
179
            $this->pageData->setRoute($this);
180
        }
181
182
        return $this;
183
    }
184
}
185