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