Total Complexity | 52 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Portfolio 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 Portfolio, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | #[ORM\Table(name: 'portfolio')] |
||
18 | #[ORM\Index(columns: ['user_id'], name: 'user')] |
||
19 | #[ORM\Index(columns: ['c_id'], name: 'course')] |
||
20 | #[ORM\Index(columns: ['session_id'], name: 'session')] |
||
21 | #[ORM\Index(columns: ['category_id'], name: 'category')] |
||
22 | #[ORM\Entity] |
||
23 | class Portfolio extends AbstractResource implements ResourceInterface, \Stringable |
||
24 | { |
||
25 | use UserTrait; |
||
26 | |||
27 | public const TYPE_ITEM = 1; |
||
28 | public const TYPE_COMMENT = 2; |
||
29 | |||
30 | public const VISIBILITY_HIDDEN = 0; |
||
31 | public const VISIBILITY_VISIBLE = 1; |
||
32 | public const VISIBILITY_HIDDEN_EXCEPT_TEACHER = 2; |
||
33 | public const VISIBILITY_PER_USER = 3; |
||
34 | |||
35 | #[ORM\Column(name: 'id', type: 'integer')] |
||
36 | #[ORM\Id] |
||
37 | #[ORM\GeneratedValue] |
||
38 | protected ?int $id = null; |
||
39 | |||
40 | #[ORM\Column(name: 'title', type: 'text', nullable: false)] |
||
41 | protected string $title; |
||
42 | |||
43 | #[ORM\Column(name: 'content', type: 'text')] |
||
44 | protected string $content; |
||
45 | |||
46 | #[ORM\ManyToOne(targetEntity: User::class)] |
||
47 | #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] |
||
48 | protected User $user; |
||
49 | |||
50 | #[ORM\ManyToOne(targetEntity: Course::class)] |
||
51 | #[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id')] |
||
52 | protected ?Course $course; |
||
53 | |||
54 | #[ORM\ManyToOne(targetEntity: Session::class)] |
||
55 | #[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id')] |
||
56 | protected ?Session $session; |
||
57 | |||
58 | #[ORM\Column(name: 'creation_date', type: 'datetime')] |
||
59 | protected DateTime $creationDate; |
||
60 | |||
61 | #[ORM\Column(name: 'update_date', type: 'datetime')] |
||
62 | protected DateTime $updateDate; |
||
63 | |||
64 | #[ORM\Column(name: 'visibility', type: 'smallint', options: ['default' => self::VISIBILITY_VISIBLE])] |
||
65 | protected int $visibility = self::VISIBILITY_VISIBLE; |
||
66 | |||
67 | #[ORM\ManyToOne(targetEntity: PortfolioCategory::class, inversedBy: 'items')] |
||
68 | #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'SET NULL')] |
||
69 | protected ?PortfolioCategory $category; |
||
70 | |||
71 | #[ORM\OneToMany(mappedBy: 'item', targetEntity: PortfolioComment::class)] |
||
72 | private Collection $comments; |
||
73 | |||
74 | #[ORM\Column(name: 'origin', type: 'integer', nullable: true)] |
||
75 | private ?int $origin; |
||
76 | |||
77 | #[ORM\Column(name: 'origin_type', type: 'integer', nullable: true)] |
||
78 | private ?int $originType; |
||
79 | |||
80 | #[ORM\Column(name: 'score', type: 'float', nullable: true)] |
||
81 | private ?float $score; |
||
82 | |||
83 | #[ORM\Column(name: 'is_highlighted', type: 'boolean', options: ['default' => false])] |
||
84 | private bool $isHighlighted = false; |
||
85 | |||
86 | #[ORM\Column(name: 'is_template', type: 'boolean', options: ['default' => false])] |
||
87 | private bool $isTemplate = false; |
||
88 | |||
89 | #[ORM\ManyToOne(targetEntity: Portfolio::class, inversedBy: 'duplicates')] |
||
90 | #[ORM\JoinColumn(name: 'duplicated_from', onDelete: 'SET NULL')] |
||
91 | private ?Portfolio $duplicatedFrom = null; |
||
92 | |||
93 | /** |
||
94 | * @var Collection<int, Portfolio> |
||
95 | */ |
||
96 | #[ORM\OneToMany(mappedBy: 'duplicatedFrom', targetEntity: Portfolio::class)] |
||
97 | private Collection $duplicates; |
||
98 | |||
99 | public function __construct() |
||
100 | { |
||
101 | $this->category = null; |
||
102 | $this->comments = new ArrayCollection(); |
||
103 | $this->duplicates = new ArrayCollection(); |
||
104 | } |
||
105 | |||
106 | public function setCourse(?Course $course = null): static |
||
107 | { |
||
108 | $this->course = $course; |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | public function getCourse(): ?Course |
||
114 | { |
||
115 | return $this->course; |
||
116 | } |
||
117 | |||
118 | public function getSession(): ?Session |
||
119 | { |
||
120 | return $this->session; |
||
121 | } |
||
122 | |||
123 | public function setSession(?Session $session = null): static |
||
128 | } |
||
129 | |||
130 | public function setTitle(string $title): static |
||
131 | { |
||
132 | $this->title = $title; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | public function getTitle(bool $stripTags = false): string |
||
138 | { |
||
139 | if ($stripTags) { |
||
140 | return strip_tags($this->title); |
||
141 | } |
||
142 | |||
143 | return $this->title; |
||
144 | } |
||
145 | |||
146 | public function setContent(string $content): static |
||
147 | { |
||
148 | $this->content = $content; |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | public function getContent(): string |
||
154 | { |
||
155 | return $this->content; |
||
156 | } |
||
157 | |||
158 | public function setCreationDate(DateTime $creationDate): static |
||
159 | { |
||
160 | $this->creationDate = $creationDate; |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | public function getCreationDate(): DateTime |
||
166 | { |
||
167 | return $this->creationDate; |
||
168 | } |
||
169 | |||
170 | public function setUpdateDate(DateTime $updateDate): static |
||
171 | { |
||
172 | $this->updateDate = $updateDate; |
||
173 | |||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | public function getUpdateDate(): DateTime |
||
178 | { |
||
179 | return $this->updateDate; |
||
180 | } |
||
181 | |||
182 | public function getId(): ?int |
||
183 | { |
||
184 | return $this->id; |
||
185 | } |
||
186 | |||
187 | public function setVisibility(int $visibility): Portfolio |
||
188 | { |
||
189 | $this->visibility = $visibility; |
||
190 | |||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | public function getVisibility(): int |
||
195 | { |
||
196 | return $this->visibility; |
||
197 | } |
||
198 | |||
199 | public function getCategory(): ?PortfolioCategory |
||
200 | { |
||
201 | return $this->category; |
||
202 | } |
||
203 | |||
204 | public function setCategory(?PortfolioCategory $category = null): static |
||
205 | { |
||
206 | $this->category = $category; |
||
207 | |||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | public function getComments(): Collection |
||
212 | { |
||
213 | return $this->comments; |
||
214 | } |
||
215 | |||
216 | public function getLastComments(int $number = 3, bool $avoidPerUserVisibility = false): Collection |
||
217 | { |
||
218 | $criteria = Criteria::create(); |
||
219 | $criteria |
||
220 | ->orderBy(['date' => 'DESC']) |
||
221 | ->setMaxResults($number) |
||
222 | ; |
||
223 | |||
224 | if ($avoidPerUserVisibility) { |
||
225 | $criteria->where( |
||
226 | Criteria::expr()->neq('visibility', PortfolioComment::VISIBILITY_PER_USER) |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | return $this->comments->matching($criteria); |
||
231 | } |
||
232 | |||
233 | public function getOrigin(): ?int |
||
234 | { |
||
235 | return $this->origin; |
||
236 | } |
||
237 | |||
238 | public function setOrigin(?int $origin): static |
||
239 | { |
||
240 | $this->origin = $origin; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | public function getOriginType(): ?int |
||
246 | { |
||
247 | return $this->originType; |
||
248 | } |
||
249 | |||
250 | public function setOriginType(?int $originType): static |
||
251 | { |
||
252 | $this->originType = $originType; |
||
253 | |||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | public function getExcerpt(int $count = 380): string |
||
258 | { |
||
259 | return api_get_short_text_from_html($this->content, $count); |
||
260 | } |
||
261 | |||
262 | public function getScore(): ?float |
||
263 | { |
||
264 | return $this->score; |
||
265 | } |
||
266 | |||
267 | public function setScore(?float $score): void |
||
268 | { |
||
269 | $this->score = $score; |
||
270 | } |
||
271 | |||
272 | public function isHighlighted(): bool |
||
273 | { |
||
274 | return $this->isHighlighted; |
||
275 | } |
||
276 | |||
277 | public function setIsHighlighted(bool $isHighlighted): self |
||
278 | { |
||
279 | $this->isHighlighted = $isHighlighted; |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | public function isTemplate(): bool |
||
285 | { |
||
286 | return $this->isTemplate; |
||
287 | } |
||
288 | |||
289 | public function setIsTemplate(bool $isTemplate): self |
||
290 | { |
||
291 | $this->isTemplate = $isTemplate; |
||
292 | |||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | public function getDuplicatedFrom(): ?Portfolio |
||
297 | { |
||
298 | return $this->duplicatedFrom; |
||
299 | } |
||
300 | |||
301 | public function setDuplicatedFrom(?Portfolio $duplicatedFrom): Portfolio |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @return Collection<int, Portfolio> |
||
310 | */ |
||
311 | public function getDuplicates(): Collection |
||
312 | { |
||
313 | return $this->duplicates; |
||
314 | } |
||
315 | |||
316 | public function addDuplicate(Portfolio $duplicate): Portfolio |
||
317 | { |
||
318 | if (!$this->duplicates->contains($duplicate)) { |
||
319 | $this->duplicates->add($duplicate); |
||
320 | $duplicate->setDuplicatedFrom($this); |
||
321 | } |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | public function removeDuplicate(Portfolio $duplicate): Portfolio |
||
327 | { |
||
328 | if ($this->duplicates->removeElement($duplicate)) { |
||
329 | // set the owning side to null (unless already changed) |
||
330 | if ($duplicate->getDuplicatedFrom() === $this) { |
||
331 | $duplicate->setDuplicatedFrom(null); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | public function hasDuplicates(): bool |
||
339 | { |
||
340 | return $this->duplicates->count() > 0; |
||
341 | } |
||
342 | |||
343 | public function isDuplicated(): bool |
||
344 | { |
||
345 | return null !== $this->duplicatedFrom; |
||
346 | } |
||
347 | |||
348 | public function isDuplicatedInSession(Session $session): bool |
||
351 | } |
||
352 | |||
353 | public function isDuplicatedInSessionId(int $sessionId): bool |
||
354 | { |
||
355 | return $this->duplicates->exists(fn ($key, Portfolio $duplicated): bool => $duplicated->session && $duplicated->session->getId() === $sessionId); |
||
356 | } |
||
357 | |||
358 | public function reset(): void |
||
359 | { |
||
360 | $this->id = null; |
||
361 | $this->duplicates = new ArrayCollection(); |
||
362 | $this->comments = new ArrayCollection(); |
||
363 | } |
||
364 | |||
365 | public function duplicateInSession(Session $session): Portfolio |
||
366 | { |
||
367 | $duplicate = clone $this; |
||
368 | $duplicate->reset(); |
||
369 | |||
370 | $duplicate->setSession($session); |
||
371 | $this->addDuplicate($duplicate); |
||
374 | } |
||
375 | |||
376 | public function getResourceName(): string |
||
377 | { |
||
378 | return $this->getTitle(); |
||
379 | } |
||
380 | |||
381 | public function setResourceName(string $name): static |
||
382 | { |
||
383 | return $this->setTitle($name); |
||
384 | } |
||
385 | |||
386 | public function __toString(): string |
||
387 | { |
||
388 | return $this->getTitle(); |
||
389 | } |
||
390 | |||
391 | public function getResourceIdentifier(): int|Uuid |
||
394 | } |
||
395 | } |
||
396 |