| Total Complexity | 51 |
| Total Lines | 410 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Skill 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 Skill, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | #[ApiResource( |
||
| 32 | operations: [ |
||
| 33 | new Post(), |
||
| 34 | new Patch(), |
||
| 35 | new Put(), |
||
| 36 | new Delete(), |
||
| 37 | new GetCollection( |
||
| 38 | uriTemplate: '/skills/tree.{_format}', |
||
| 39 | paginationEnabled: false, |
||
| 40 | normalizationContext: [ |
||
| 41 | 'groups' => ['skill:tree:read'] |
||
| 42 | ], |
||
| 43 | output: SkillTreeNode::class, |
||
| 44 | provider: SkillTreeStateProvider::class |
||
| 45 | ), |
||
| 46 | new GetCollection(), |
||
| 47 | new Get(), |
||
| 48 | ], |
||
| 49 | normalizationContext: [ |
||
| 50 | 'groups' => ['skill:read'], |
||
| 51 | ], |
||
| 52 | security: "is_granted('ROLE_ADMIN')" |
||
| 53 | )] |
||
| 54 | #[ApiFilter(SearchFilter::class, properties: ['issuedSkills.user' => 'exact'])] |
||
| 55 | #[ORM\Table(name: 'skill')] |
||
| 56 | #[ORM\Entity(repositoryClass: SkillRepository::class)] |
||
| 57 | class Skill implements Stringable, Translatable |
||
| 58 | { |
||
| 59 | public const STATUS_DISABLED = 0; |
||
| 60 | public const STATUS_ENABLED = 1; |
||
| 61 | |||
| 62 | #[Groups(['skill:read'])] |
||
| 63 | #[ORM\Column(name: 'id', type: 'integer')] |
||
| 64 | #[ORM\Id] |
||
| 65 | #[ORM\GeneratedValue] |
||
| 66 | protected ?int $id = null; |
||
| 67 | |||
| 68 | #[ORM\ManyToOne(targetEntity: SkillLevelProfile::class, inversedBy: 'skills')] |
||
| 69 | #[ORM\JoinColumn(name: 'profile_id', referencedColumnName: 'id')] |
||
| 70 | protected ?SkillLevelProfile $levelProfile = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Collection<int, SkillRelUser> |
||
| 74 | */ |
||
| 75 | #[Groups(['skill:read'])] |
||
| 76 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelUser::class, cascade: ['persist'])] |
||
| 77 | protected Collection $issuedSkills; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Collection<int, SkillRelItem> |
||
| 81 | */ |
||
| 82 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelItem::class, cascade: ['persist'])] |
||
| 83 | protected Collection $items; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var Collection<int, SkillRelSkill> |
||
| 87 | */ |
||
| 88 | #[ORM\OneToMany(mappedBy: 'parent', targetEntity: SkillRelSkill::class, cascade: ['persist'])] |
||
| 89 | protected Collection $skills; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var Collection<int, SkillRelCourse> |
||
| 93 | */ |
||
| 94 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelCourse::class, cascade: ['persist'])] |
||
| 95 | protected Collection $courses; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var Collection<int, SkillRelGradebook> |
||
| 99 | */ |
||
| 100 | #[ORM\OneToMany(mappedBy: 'skill', targetEntity: SkillRelGradebook::class, cascade: ['persist'])] |
||
| 101 | protected Collection $gradeBookCategories; |
||
| 102 | |||
| 103 | #[Gedmo\Translatable] |
||
| 104 | #[Assert\NotBlank] |
||
| 105 | #[Groups(['skill:read', 'skill:write', 'skill_rel_user:read'])] |
||
| 106 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
| 107 | protected string $title; |
||
| 108 | |||
| 109 | #[Gedmo\Translatable] |
||
| 110 | #[Assert\NotBlank] |
||
| 111 | #[Groups(['skill:read', 'skill:write'])] |
||
| 112 | #[ORM\Column(name: 'short_code', type: 'string', length: 100, nullable: false)] |
||
| 113 | protected string $shortCode; |
||
| 114 | |||
| 115 | #[Groups(['skill:read', 'skill:write'])] |
||
| 116 | #[ORM\Column(name: 'description', type: 'text', nullable: false)] |
||
| 117 | protected string $description; |
||
| 118 | |||
| 119 | #[Assert\NotNull] |
||
| 120 | #[ORM\Column(name: 'access_url_id', type: 'integer', nullable: false)] |
||
| 121 | protected int $accessUrlId; |
||
| 122 | |||
| 123 | #[Groups(['skill:read', 'skill_rel_user:read'])] |
||
| 124 | #[ORM\Column(name: 'icon', type: 'string', length: 255, nullable: false)] |
||
| 125 | protected string $icon; |
||
| 126 | |||
| 127 | #[ORM\ManyToOne(targetEntity: Asset::class, cascade: ['persist', 'remove'])] |
||
| 128 | #[ORM\JoinColumn(name: 'asset_id', referencedColumnName: 'id')] |
||
| 129 | protected ?Asset $asset = null; |
||
| 130 | |||
| 131 | #[ORM\Column(name: 'criteria', type: 'text', nullable: true)] |
||
| 132 | protected ?string $criteria = null; |
||
| 133 | |||
| 134 | #[ORM\Column(name: 'status', type: 'integer', nullable: false, options: ['default' => 1])] |
||
| 135 | protected int $status; |
||
| 136 | |||
| 137 | #[Gedmo\Timestampable(on: 'update')] |
||
| 138 | #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: false)] |
||
| 139 | protected DateTime $updatedAt; |
||
| 140 | |||
| 141 | #[Gedmo\Locale] |
||
| 142 | private ?string $locale = null; |
||
| 143 | |||
| 144 | public function __construct() |
||
| 145 | { |
||
| 146 | $this->issuedSkills = new ArrayCollection(); |
||
| 147 | $this->items = new ArrayCollection(); |
||
| 148 | $this->courses = new ArrayCollection(); |
||
| 149 | $this->gradeBookCategories = new ArrayCollection(); |
||
| 150 | $this->skills = new ArrayCollection(); |
||
| 151 | $this->icon = ''; |
||
| 152 | $this->description = ''; |
||
| 153 | $this->status = self::STATUS_ENABLED; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function __toString(): string |
||
| 157 | { |
||
| 158 | return $this->getTitle(); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setTitle(string $title): self |
||
| 162 | { |
||
| 163 | $this->title = $title; |
||
| 164 | |||
| 165 | return $this; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getTitle(): string |
||
| 169 | { |
||
| 170 | return $this->title; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getShortCode(): string |
||
| 174 | { |
||
| 175 | return $this->shortCode; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function setShortCode(string $shortCode): self |
||
| 179 | { |
||
| 180 | $this->shortCode = $shortCode; |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function setDescription(string $description): self |
||
| 186 | { |
||
| 187 | $this->description = $description; |
||
| 188 | |||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function getDescription(): string |
||
| 193 | { |
||
| 194 | return $this->description; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function setAccessUrlId(int $accessUrlId): static |
||
| 198 | { |
||
| 199 | $this->accessUrlId = $accessUrlId; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getAccessUrlId(): int |
||
| 205 | { |
||
| 206 | return $this->accessUrlId; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function setIcon(string $icon): self |
||
| 210 | { |
||
| 211 | $this->icon = $icon; |
||
| 212 | |||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getIcon(): string |
||
| 217 | { |
||
| 218 | return $this->icon; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function setCriteria(string $criteria): self |
||
| 222 | { |
||
| 223 | $this->criteria = $criteria; |
||
| 224 | |||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getCriteria(): ?string |
||
| 231 | } |
||
| 232 | |||
| 233 | public function setStatus(int $status): self |
||
| 234 | { |
||
| 235 | $this->status = $status; |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function getStatus(): int |
||
| 241 | { |
||
| 242 | return $this->status; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function setUpdatedAt(DateTime $updatedAt): static |
||
| 246 | { |
||
| 247 | $this->updatedAt = $updatedAt; |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getUpdatedAt(): DateTime |
||
| 253 | { |
||
| 254 | return $this->updatedAt; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function getId(): ?int |
||
| 258 | { |
||
| 259 | return $this->id; |
||
| 260 | } |
||
| 261 | |||
| 262 | public function getLevelProfile(): ?SkillLevelProfile |
||
| 263 | { |
||
| 264 | return $this->levelProfile; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function setLevelProfile(SkillLevelProfile $levelProfile): self |
||
| 268 | { |
||
| 269 | $this->levelProfile = $levelProfile; |
||
| 270 | |||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return Collection<int, SkillRelUser> |
||
| 276 | */ |
||
| 277 | public function getIssuedSkills(): Collection |
||
| 278 | { |
||
| 279 | return $this->issuedSkills; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return Collection<int, SkillRelItem> |
||
| 284 | */ |
||
| 285 | public function getItems(): Collection |
||
| 286 | { |
||
| 287 | return $this->items; |
||
| 288 | } |
||
| 289 | |||
| 290 | public function setItems(ArrayCollection $items): self |
||
| 291 | { |
||
| 292 | $this->items = $items; |
||
| 293 | |||
| 294 | return $this; |
||
| 295 | } |
||
| 296 | |||
| 297 | public function hasItem(int $typeId, int $itemId): bool |
||
| 298 | { |
||
| 299 | if (0 !== $this->getItems()->count()) { |
||
| 300 | $found = false; |
||
| 301 | |||
| 302 | /** @var SkillRelItem $item */ |
||
| 303 | foreach ($this->getItems() as $item) { |
||
| 304 | if ($item->getItemId() === $itemId && $item->getItemType() === $typeId) { |
||
| 305 | $found = true; |
||
| 306 | |||
| 307 | break; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | return $found; |
||
| 312 | } |
||
| 313 | |||
| 314 | return false; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function addItem(SkillRelItem $skillRelItem): void |
||
| 321 | } |
||
| 322 | |||
| 323 | public function getCourses(): Collection |
||
| 324 | { |
||
| 325 | return $this->courses; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function setCourses(ArrayCollection $courses): self |
||
| 329 | { |
||
| 330 | $this->courses = $courses; |
||
| 331 | |||
| 332 | return $this; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return Collection<int, SkillRelSkill> |
||
| 337 | */ |
||
| 338 | public function getSkills(): Collection |
||
| 339 | { |
||
| 340 | return $this->skills; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param Collection<int, SkillRelSkill> $skills |
||
| 345 | */ |
||
| 346 | public function setSkills(Collection $skills): self |
||
| 347 | { |
||
| 348 | $this->skills = $skills; |
||
| 349 | |||
| 350 | return $this; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return Collection<int, SkillRelGradebook> |
||
| 355 | */ |
||
| 356 | public function getGradeBookCategories(): Collection |
||
| 357 | { |
||
| 358 | return $this->gradeBookCategories; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param Collection<int, SkillRelGradebook> $gradeBookCategories |
||
| 363 | */ |
||
| 364 | public function setGradeBookCategories(Collection $gradeBookCategories): self |
||
| 365 | { |
||
| 366 | $this->gradeBookCategories = $gradeBookCategories; |
||
| 367 | |||
| 368 | return $this; |
||
| 369 | } |
||
| 370 | |||
| 371 | public function hasAsset(): bool |
||
| 372 | { |
||
| 373 | return null !== $this->asset; |
||
| 374 | } |
||
| 375 | |||
| 376 | public function getAsset(): ?Asset |
||
| 379 | } |
||
| 380 | |||
| 381 | public function setAsset(?Asset $asset): self |
||
| 382 | { |
||
| 383 | $this->asset = $asset; |
||
| 384 | |||
| 385 | return $this; |
||
| 386 | } |
||
| 387 | |||
| 388 | public function hasCourseAndSession(SkillRelCourse $searchItem): bool |
||
| 413 | } |
||
| 414 | |||
| 415 | public function addToCourse(SkillRelCourse $item): void |
||
| 416 | { |
||
| 417 | $item->setSkill($this); |
||
| 418 | $this->courses[] = $item; |
||
| 419 | } |
||
| 420 | |||
| 421 | public function getLocale(): string |
||
| 422 | { |
||
| 423 | return $this->locale; |
||
|
|
|||
| 424 | } |
||
| 425 | |||
| 426 | public function setLocale(string $locale): self |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return Collection<int, Skill> |
||
| 435 | */ |
||
| 436 | public function getChildSkills(): Collection |
||
| 437 | { |
||
| 438 | return $this |
||
| 439 | ->getSkills() |
||
| 440 | ->map(fn(SkillRelSkill $skillRelSkill): Skill => $skillRelSkill->getSkill()) |
||
| 441 | ; |
||
| 442 | } |
||
| 443 | } |
||
| 444 |