Total Complexity | 41 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResourceLink often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResourceLink, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | #[ApiResource] |
||
24 | #[ORM\Table(name: 'resource_link')] |
||
25 | #[ORM\Entity(repositoryClass: ResourceLinkRepository::class)] |
||
26 | #[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)] |
||
27 | class ResourceLink implements Stringable |
||
28 | { |
||
29 | use SoftDeleteableEntity; |
||
30 | use TimestampableTypedEntity; |
||
31 | |||
32 | public const VISIBILITY_DRAFT = 0; |
||
33 | public const VISIBILITY_PENDING = 1; |
||
34 | public const VISIBILITY_PUBLISHED = 2; |
||
35 | |||
36 | #[ORM\Id] |
||
37 | #[ORM\Column(type: 'integer')] |
||
38 | #[ORM\GeneratedValue] |
||
39 | protected ?int $id = null; |
||
40 | |||
41 | #[ORM\ManyToOne(targetEntity: ResourceNode::class, inversedBy: 'resourceLinks')] |
||
42 | #[ORM\JoinColumn(name: 'resource_node_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
43 | protected ResourceNode $resourceNode; |
||
44 | |||
45 | #[Gedmo\SortableGroup] |
||
46 | #[ORM\ManyToOne(targetEntity: Course::class)] |
||
47 | #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] |
||
48 | protected ?Course $course = null; |
||
49 | |||
50 | #[Gedmo\SortableGroup] |
||
51 | #[ORM\ManyToOne(targetEntity: Session::class, inversedBy: 'resourceLinks')] |
||
52 | #[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] |
||
53 | protected ?Session $session = null; |
||
54 | |||
55 | #[Gedmo\SortableGroup] |
||
56 | #[ORM\ManyToOne(targetEntity: Usergroup::class)] |
||
57 | #[ORM\JoinColumn(name: 'usergroup_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')] |
||
58 | protected ?Usergroup $userGroup = null; |
||
59 | |||
60 | #[Gedmo\SortableGroup] |
||
61 | #[ORM\ManyToOne(targetEntity: CGroup::class)] |
||
62 | #[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')] |
||
63 | protected ?CGroup $group = null; |
||
64 | |||
65 | #[Gedmo\SortableGroup] |
||
66 | #[ORM\ManyToOne(targetEntity: User::class)] |
||
67 | #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')] |
||
68 | protected ?User $user = null; |
||
69 | |||
70 | /** |
||
71 | * @var Collection<int, ResourceRight> |
||
72 | */ |
||
73 | #[ORM\OneToMany( |
||
74 | mappedBy: 'resourceLink', |
||
75 | targetEntity: ResourceRight::class, |
||
76 | cascade: ['persist', 'remove'], |
||
77 | orphanRemoval: true |
||
78 | )] |
||
79 | protected Collection $resourceRights; |
||
80 | |||
81 | #[Groups([ |
||
82 | 'ctool:read', |
||
83 | 'c_tool_intro:read', |
||
84 | 'student_publication:read', |
||
85 | ])] |
||
86 | #[ORM\Column(name: 'visibility', type: 'integer', nullable: false)] |
||
87 | protected int $visibility; |
||
88 | |||
89 | #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])] |
||
90 | #[ORM\Column(name: 'start_visibility_at', type: 'datetime', nullable: true)] |
||
91 | protected ?DateTimeInterface $startVisibilityAt = null; |
||
92 | |||
93 | #[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])] |
||
94 | #[ORM\Column(name: 'end_visibility_at', type: 'datetime', nullable: true)] |
||
95 | protected ?DateTimeInterface $endVisibilityAt = null; |
||
96 | |||
97 | #[Gedmo\SortablePosition] |
||
98 | #[ORM\Column] |
||
99 | private int $displayOrder; |
||
100 | |||
101 | #[Gedmo\SortableGroup] |
||
102 | #[ORM\Column] |
||
103 | private ?int $resourceTypeGroup; |
||
104 | |||
105 | public function __construct() |
||
106 | { |
||
107 | $this->resourceRights = new ArrayCollection(); |
||
108 | $this->visibility = self::VISIBILITY_DRAFT; |
||
109 | } |
||
110 | |||
111 | public function __toString(): string |
||
112 | { |
||
113 | return (string) $this->getId(); |
||
114 | } |
||
115 | |||
116 | public function getId(): ?int |
||
117 | { |
||
118 | return $this->id; |
||
119 | } |
||
120 | |||
121 | public function getStartVisibilityAt(): ?DateTimeInterface |
||
122 | { |
||
123 | return $this->startVisibilityAt; |
||
124 | } |
||
125 | |||
126 | public function setStartVisibilityAt(?DateTimeInterface $startVisibilityAt): self |
||
127 | { |
||
128 | $this->startVisibilityAt = $startVisibilityAt; |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | public function getEndVisibilityAt(): ?DateTimeInterface |
||
134 | { |
||
135 | return $this->endVisibilityAt; |
||
136 | } |
||
137 | |||
138 | public function setEndVisibilityAt(?DateTimeInterface $endVisibilityAt): self |
||
139 | { |
||
140 | $this->endVisibilityAt = $endVisibilityAt; |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | public function addResourceRight(ResourceRight $right): self |
||
146 | { |
||
147 | if (!$this->resourceRights->contains($right)) { |
||
148 | $right->setResourceLink($this); |
||
149 | $this->resourceRights->add($right); |
||
150 | } |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return Collection<int, ResourceRight> |
||
157 | */ |
||
158 | public function getResourceRights(): Collection |
||
159 | { |
||
160 | return $this->resourceRights; |
||
161 | } |
||
162 | |||
163 | public function setResourceRights(Collection $rights): self |
||
164 | { |
||
165 | $this->resourceRights = $rights; |
||
166 | |||
167 | /*foreach ($rights as $right) { |
||
168 | $this->addResourceRight($right); |
||
169 | }*/ |
||
170 | |||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | public function getCourse(): ?Course |
||
175 | { |
||
176 | return $this->course; |
||
177 | } |
||
178 | |||
179 | public function setCourse(?Course $course = null): self |
||
180 | { |
||
181 | $this->course = $course; |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | public function getSession(): ?Session |
||
187 | { |
||
188 | return $this->session; |
||
189 | } |
||
190 | |||
191 | public function setSession(?Session $session = null): self |
||
192 | { |
||
193 | $this->session = $session; |
||
194 | |||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | public function hasSession(): bool |
||
199 | { |
||
200 | return null !== $this->session; |
||
201 | } |
||
202 | |||
203 | public function hasCourse(): bool |
||
204 | { |
||
205 | return null !== $this->course; |
||
206 | } |
||
207 | |||
208 | public function hasGroup(): bool |
||
211 | } |
||
212 | |||
213 | public function getGroup(): ?CGroup |
||
214 | { |
||
215 | return $this->group; |
||
216 | } |
||
217 | |||
218 | public function setGroup(?CGroup $group = null): self |
||
223 | } |
||
224 | |||
225 | public function getUserGroup(): ?Usergroup |
||
226 | { |
||
227 | return $this->userGroup; |
||
228 | } |
||
229 | |||
230 | public function setUserGroup(?Usergroup $group = null): self |
||
235 | } |
||
236 | |||
237 | public function getUser(): ?User |
||
238 | { |
||
239 | return $this->user; |
||
240 | } |
||
241 | |||
242 | public function setUser(?User $user = null): self |
||
243 | { |
||
244 | $this->user = $user; |
||
245 | |||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | public function hasUser(): bool |
||
250 | { |
||
251 | return null !== $this->user; |
||
252 | } |
||
253 | |||
254 | public function getResourceNode(): ResourceNode |
||
257 | } |
||
258 | |||
259 | public function setResourceNode(ResourceNode $resourceNode): self |
||
264 | } |
||
265 | |||
266 | public function isPublished(): bool |
||
267 | { |
||
268 | return self::VISIBILITY_PUBLISHED === $this->getVisibility(); |
||
269 | } |
||
270 | |||
271 | public function getVisibility(): int |
||
274 | } |
||
275 | |||
276 | public function setVisibility(int $visibility): self |
||
277 | { |
||
278 | if (!\in_array($visibility, self::getVisibilityList(), true)) { |
||
279 | $message = sprintf( |
||
280 | 'The visibility is not valid. Valid options: %s', |
||
281 | print_r(self::getVisibilityList(), true) |
||
282 | ); |
||
283 | |||
284 | throw new LogicException($message); |
||
285 | } |
||
286 | $this->visibility = $visibility; |
||
287 | |||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | public static function getVisibilityList(): array |
||
292 | { |
||
293 | return [ |
||
294 | 'Draft' => self::VISIBILITY_DRAFT, |
||
295 | 'Pending' => self::VISIBILITY_PENDING, |
||
296 | 'Published' => self::VISIBILITY_PUBLISHED, |
||
297 | ]; |
||
298 | } |
||
299 | |||
300 | public function isPending(): bool |
||
301 | { |
||
302 | return self::VISIBILITY_PENDING === $this->getVisibility(); |
||
303 | } |
||
304 | |||
305 | public function isDraft(): bool |
||
306 | { |
||
307 | return self::VISIBILITY_DRAFT === $this->getVisibility(); |
||
308 | } |
||
309 | |||
310 | public function getVisibilityName(): string |
||
311 | { |
||
312 | return array_flip(static::getVisibilityList())[$this->getVisibility()]; |
||
313 | } |
||
314 | |||
315 | public function getDisplayOrder(): int |
||
318 | } |
||
319 | |||
320 | public function setDisplayOrder(int $displayOrder): static |
||
325 | } |
||
326 | |||
327 | public function getResourceTypeGroup(): ?int |
||
328 | { |
||
329 | return $this->resourceTypeGroup; |
||
330 | } |
||
331 | |||
332 | public function setResourceTypeGroup(int $resourceTypeGroup): static |
||
337 | } |
||
338 | |||
339 | public function moveUpPosition(): static |
||
340 | { |
||
341 | $this->displayOrder--; |
||
342 | |||
343 | return $this; |
||
344 | } |
||
345 | |||
346 | public function moveDownPosition(): static |
||
347 | { |
||
348 | $this->displayOrder++; |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | } |
||
353 |