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