Passed
Push — master ( 4f4814...62e1a3 )
by Daniel
09:55
created

Route::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
    public function setRedirectedFrom(Collection $redirectedFrom): self
133
    {
134
        $this->redirectedFrom = $redirectedFrom;
135
136
        return $this;
137
    }
138
139
    public function addRedirectedFrom(self $redirectedFrom): self
140
    {
141
        if (!$this->redirectedFrom->contains($redirectedFrom)) {
142
            $this->redirectedFrom->add($redirectedFrom);
143
        }
144
145
        return $this;
146
    }
147
148
    public function getPage(): ?Page
149
    {
150
        return $this->page;
151
    }
152
153
    public function setPage(?Page $page): self
154
    {
155
        $this->page = $page;
156
        if ($this->page) {
157
            $this->page->setRoute($this);
158
        }
159
160
        return $this;
161
    }
162
163
    public function getPageData(): ?AbstractPageData
164
    {
165
        return $this->pageData;
166
    }
167
168
    public function setPageData(?AbstractPageData $pageData): self
169
    {
170
        $this->pageData = $pageData;
171
        if ($this->pageData) {
172
            $this->pageData->setRoute($this);
173
        }
174
175
        return $this;
176
    }
177
}
178