| Total Complexity | 61 |
| Total Lines | 491 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 0 |
Complex classes like CCalendarEvent 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 CCalendarEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | #[ApiResource( |
||
| 46 | operations: [ |
||
| 47 | new Get(security: "is_granted('VIEW', object)"), |
||
| 48 | new Put( |
||
| 49 | controller: UpdateCCalendarEventAction::class, |
||
| 50 | security: "is_granted('EDIT', object)", |
||
| 51 | deserialize: false |
||
| 52 | ), |
||
| 53 | new Delete(security: "is_granted('DELETE', object)"), |
||
| 54 | new GetCollection( |
||
| 55 | paginationEnabled: false, |
||
| 56 | security: "is_granted('ROLE_USER')", |
||
| 57 | output: CalendarEvent::class, |
||
| 58 | provider: CalendarEventStateProvider::class, |
||
| 59 | ), |
||
| 60 | new Post( |
||
| 61 | securityPostDenormalize: "is_granted('CREATE', object)", |
||
| 62 | processor: CCalendarEventStateProcessor::class |
||
| 63 | ), |
||
| 64 | ], |
||
| 65 | normalizationContext: ['groups' => ['calendar_event:read', 'resource_node:read']], |
||
| 66 | denormalizationContext: ['groups' => ['calendar_event:write']], |
||
| 67 | security: "is_granted('ROLE_USER')" |
||
| 68 | )] |
||
| 69 | #[ORM\Table(name: 'c_calendar_event')] |
||
| 70 | #[ORM\Entity(repositoryClass: CCalendarEventRepository::class)] |
||
| 71 | #[ApiFilter(filterClass: SearchFilter::class, properties: ['allDay' => 'boolean'])] |
||
| 72 | #[ApiFilter(filterClass: DateFilter::class, strategy: 'exclude_null')] |
||
| 73 | #[ApiFilter(filterClass: CidFilter::class)] |
||
| 74 | #[ApiFilter(filterClass: SidFilter::class)] |
||
| 75 | #[ApiFilter(GlobalEventFilter::class, properties: ['type'])] |
||
| 76 | class CCalendarEvent extends AbstractResource implements ResourceInterface, Stringable |
||
| 77 | { |
||
| 78 | public const COLOR_STUDENT_PUBLICATION = '#FF8C00'; |
||
| 79 | |||
| 80 | public const TYPE_INVITATION = 'invitation'; |
||
| 81 | public const TYPE_SUBSCRIPTION = 'subscription'; |
||
| 82 | |||
| 83 | public const SUBSCRIPTION_VISIBILITY_NO = 0; |
||
| 84 | public const SUBSCRIPTION_VISIBILITY_ALL = 1; |
||
| 85 | public const SUBSCRIPTION_VISIBILITY_CLASS = 2; |
||
| 86 | |||
| 87 | #[Groups(['calendar_event:read'])] |
||
| 88 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
| 89 | #[ORM\Id] |
||
| 90 | #[ORM\GeneratedValue] |
||
| 91 | protected ?int $iid = null; |
||
| 92 | |||
| 93 | #[Groups(['calendar_event:write', 'calendar_event:read'])] |
||
| 94 | #[Assert\NotBlank] |
||
| 95 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
| 96 | protected string $title; |
||
| 97 | |||
| 98 | #[Groups(['calendar_event:write', 'calendar_event:read'])] |
||
| 99 | #[Assert\NotBlank] |
||
| 100 | #[ORM\Column(name: 'content', type: 'text', nullable: true)] |
||
| 101 | protected ?string $content = null; |
||
| 102 | |||
| 103 | #[Groups(['calendar_event:write', 'calendar_event:read'])] |
||
| 104 | #[ORM\Column(name: 'start_date', type: 'datetime', nullable: true)] |
||
| 105 | protected ?DateTime $startDate = null; |
||
| 106 | |||
| 107 | #[Groups(['calendar_event:write', 'calendar_event:read'])] |
||
| 108 | #[ORM\Column(name: 'end_date', type: 'datetime', nullable: true)] |
||
| 109 | protected ?DateTime $endDate = null; |
||
| 110 | |||
| 111 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
| 112 | #[ORM\JoinColumn(name: 'parent_event_id', referencedColumnName: 'iid')] |
||
| 113 | protected ?CCalendarEvent $parentEvent = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Collection<int, CCalendarEvent> |
||
| 117 | */ |
||
| 118 | #[ORM\OneToMany(mappedBy: 'parentEvent', targetEntity: self::class)] |
||
| 119 | protected Collection $children; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var Collection<int, CCalendarEventRepeat> |
||
| 123 | */ |
||
| 124 | #[ORM\OneToMany( |
||
| 125 | mappedBy: 'event', |
||
| 126 | targetEntity: CCalendarEventRepeat::class, |
||
| 127 | cascade: ['persist'], |
||
| 128 | orphanRemoval: true |
||
| 129 | )] |
||
| 130 | protected Collection $repeatEvents; |
||
| 131 | |||
| 132 | #[Assert\NotNull] |
||
| 133 | #[ORM\Column(name: 'all_day', type: 'boolean', nullable: false)] |
||
| 134 | protected bool $allDay; |
||
| 135 | |||
| 136 | #[ORM\Column(name: 'comment', type: 'text', nullable: true)] |
||
| 137 | protected ?string $comment = null; |
||
| 138 | |||
| 139 | #[Groups(['calendar_event:write', 'calendar_event:read'])] |
||
| 140 | #[ORM\Column(name: 'color', type: 'string', length: 20, nullable: true)] |
||
| 141 | protected ?string $color = null; |
||
| 142 | |||
| 143 | #[ORM\ManyToOne(targetEntity: Room::class)] |
||
| 144 | #[ORM\JoinColumn(name: 'room_id', referencedColumnName: 'id')] |
||
| 145 | protected ?Room $room = null; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var Collection<int, CCalendarEventAttachment> |
||
| 149 | */ |
||
| 150 | #[ORM\OneToMany(mappedBy: 'event', targetEntity: CCalendarEventAttachment::class, cascade: ['persist', 'remove'])] |
||
| 151 | protected Collection $attachments; |
||
| 152 | |||
| 153 | #[Groups(['calendar_event:write'])] |
||
| 154 | #[ORM\Column(name: 'invitation_type', type: 'string', nullable: true)] |
||
| 155 | protected ?string $invitationType = null; |
||
| 156 | |||
| 157 | #[Groups(['calendar_event:write'])] |
||
| 158 | #[Assert\NotNull] |
||
| 159 | #[ORM\Column(name: 'collective', type: 'boolean')] |
||
| 160 | protected bool $collective = false; |
||
| 161 | |||
| 162 | #[Groups(['calendar_event:write'])] |
||
| 163 | #[ORM\Column(name: 'subscription_visibility', type: 'integer')] |
||
| 164 | protected int $subscriptionVisibility = self::SUBSCRIPTION_VISIBILITY_NO; |
||
| 165 | |||
| 166 | #[Groups(['calendar_event:write'])] |
||
| 167 | #[ORM\Column(name: 'subscription_item_id', type: 'integer', nullable: true)] |
||
| 168 | protected ?int $subscriptionItemId = null; |
||
| 169 | |||
| 170 | #[Groups(['calendar_event:write'])] |
||
| 171 | #[ORM\Column(name: 'max_attendees', type: 'integer')] |
||
| 172 | protected int $maxAttendees = 0; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var Collection<int, AgendaReminder> |
||
| 176 | */ |
||
| 177 | #[Groups(['calendar_event:write'])] |
||
| 178 | #[ORM\OneToMany(mappedBy: 'event', targetEntity: AgendaReminder::class, cascade: ['persist'], orphanRemoval: true)] |
||
| 179 | private Collection $reminders; |
||
| 180 | |||
| 181 | #[ORM\ManyToOne(targetEntity: Career::class)] |
||
| 182 | #[ORM\JoinColumn(name: 'career_id', referencedColumnName: 'id', nullable: true)] |
||
| 183 | protected ?Career $career = null; |
||
| 184 | |||
| 185 | #[ORM\ManyToOne(targetEntity: Promotion::class)] |
||
| 186 | #[ORM\JoinColumn(name: 'promotion_id', referencedColumnName: 'id', nullable: true)] |
||
| 187 | protected ?Promotion $promotion = null; |
||
| 188 | |||
| 189 | public function __construct() |
||
| 190 | { |
||
| 191 | $this->children = new ArrayCollection(); |
||
| 192 | $this->attachments = new ArrayCollection(); |
||
| 193 | $this->repeatEvents = new ArrayCollection(); |
||
| 194 | $this->allDay = false; |
||
| 195 | $this->collective = false; |
||
| 196 | $this->reminders = new ArrayCollection(); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function __toString(): string |
||
| 200 | { |
||
| 201 | return $this->getTitle(); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getTitle(): string |
||
| 205 | { |
||
| 206 | return $this->title; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function setTitle(string $title): self |
||
| 210 | { |
||
| 211 | $this->title = $title; |
||
| 212 | |||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getContent(): ?string |
||
| 217 | { |
||
| 218 | return $this->content; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function setContent(string $content): self |
||
| 222 | { |
||
| 223 | $this->content = $content; |
||
| 224 | |||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getStartDate(): ?DateTime |
||
| 229 | { |
||
| 230 | return $this->startDate; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function setStartDate(?DateTime $startDate): self |
||
| 234 | { |
||
| 235 | $this->startDate = $startDate; |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function getEndDate(): ?DateTime |
||
| 241 | { |
||
| 242 | return $this->endDate; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function setEndDate(?DateTime $endDate): self |
||
| 246 | { |
||
| 247 | $this->endDate = $endDate; |
||
| 248 | |||
| 249 | return $this; |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getParentEvent(): ?self |
||
| 253 | { |
||
| 254 | return $this->parentEvent; |
||
| 255 | } |
||
| 256 | |||
| 257 | public function setParentEvent(self $parent): self |
||
| 258 | { |
||
| 259 | $this->parentEvent = $parent; |
||
| 260 | |||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function addChild(self $event): self |
||
| 265 | { |
||
| 266 | if (!$this->getChildren()->contains($event)) { |
||
| 267 | $this->getChildren()->add($event); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $this; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return Collection<int, CCalendarEvent> |
||
| 275 | */ |
||
| 276 | public function getChildren(): Collection |
||
| 277 | { |
||
| 278 | return $this->children; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param Collection<int, CCalendarEvent> $children |
||
| 283 | */ |
||
| 284 | public function setChildren(Collection $children): self |
||
| 285 | { |
||
| 286 | $this->children = $children; |
||
| 287 | |||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | public function isAllDay(): bool |
||
| 292 | { |
||
| 293 | return $this->allDay; |
||
| 294 | } |
||
| 295 | |||
| 296 | public function setAllDay(bool $allDay): self |
||
| 297 | { |
||
| 298 | $this->allDay = $allDay; |
||
| 299 | |||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | |||
| 303 | public function getComment(): ?string |
||
| 304 | { |
||
| 305 | return $this->comment; |
||
| 306 | } |
||
| 307 | |||
| 308 | public function setComment(string $comment): self |
||
| 309 | { |
||
| 310 | $this->comment = $comment; |
||
| 311 | |||
| 312 | return $this; |
||
| 313 | } |
||
| 314 | |||
| 315 | public function getRoom(): ?Room |
||
| 316 | { |
||
| 317 | return $this->room; |
||
| 318 | } |
||
| 319 | |||
| 320 | public function setRoom(Room $room): self |
||
| 321 | { |
||
| 322 | $this->room = $room; |
||
| 323 | |||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getColor(): ?string |
||
| 328 | { |
||
| 329 | return $this->color; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function setColor(string $color): self |
||
| 333 | { |
||
| 334 | $this->color = $color; |
||
| 335 | |||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @return Collection<int, CCalendarEventAttachment> |
||
| 341 | */ |
||
| 342 | public function getAttachments(): Collection |
||
| 343 | { |
||
| 344 | return $this->attachments; |
||
| 345 | } |
||
| 346 | |||
| 347 | public function setAttachments(Collection $attachments): self |
||
| 348 | { |
||
| 349 | $this->attachments = $attachments; |
||
| 350 | |||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | public function addAttachment(CCalendarEventAttachment $attachment): self |
||
| 355 | { |
||
| 356 | $this->attachments->add($attachment); |
||
| 357 | |||
| 358 | return $this; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return Collection<int, CCalendarEventRepeat> |
||
| 363 | */ |
||
| 364 | public function getRepeatEvents(): Collection |
||
| 365 | { |
||
| 366 | return $this->repeatEvents; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param Collection<int, CCalendarEventRepeat> $repeatEvents |
||
| 371 | */ |
||
| 372 | public function setRepeatEvents(Collection $repeatEvents): self |
||
| 377 | } |
||
| 378 | |||
| 379 | public function getResourceIdentifier(): int|Uuid |
||
| 382 | } |
||
| 383 | |||
| 384 | public function getIid(): ?int |
||
| 385 | { |
||
| 386 | return $this->iid; |
||
| 387 | } |
||
| 388 | |||
| 389 | public function getResourceName(): string |
||
| 390 | { |
||
| 391 | return $this->getTitle(); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function setResourceName(string $name): self |
||
| 395 | { |
||
| 396 | return $this->setTitle($name); |
||
| 397 | } |
||
| 398 | |||
| 399 | public function isCollective(): bool |
||
| 400 | { |
||
| 401 | return $this->collective; |
||
| 402 | } |
||
| 403 | |||
| 404 | public function setCollective(bool $collective): self |
||
| 405 | { |
||
| 406 | $this->collective = $collective; |
||
| 407 | |||
| 408 | return $this; |
||
| 409 | } |
||
| 410 | |||
| 411 | public function getInvitationType(): ?string |
||
| 412 | { |
||
| 413 | return $this->invitationType; |
||
| 414 | } |
||
| 415 | |||
| 416 | public function setInvitationType(string $invitationType): self |
||
| 417 | { |
||
| 418 | $this->invitationType = $invitationType; |
||
| 419 | |||
| 420 | return $this; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getSubscriptionVisibility(): int |
||
| 424 | { |
||
| 425 | return $this->subscriptionVisibility; |
||
| 426 | } |
||
| 427 | |||
| 428 | public function setSubscriptionVisibility(int $subscriptionVisibility): self |
||
| 429 | { |
||
| 430 | $this->subscriptionVisibility = $subscriptionVisibility; |
||
| 431 | |||
| 432 | return $this; |
||
| 433 | } |
||
| 434 | |||
| 435 | public function getSubscriptionItemId(): ?int |
||
| 436 | { |
||
| 437 | return $this->subscriptionItemId; |
||
| 438 | } |
||
| 439 | |||
| 440 | public function setSubscriptionItemId(?int $subscriptionItemId): self |
||
| 441 | { |
||
| 442 | $this->subscriptionItemId = $subscriptionItemId; |
||
| 443 | |||
| 444 | return $this; |
||
| 445 | } |
||
| 446 | |||
| 447 | public function getMaxAttendees(): int |
||
| 448 | { |
||
| 449 | return $this->maxAttendees; |
||
| 450 | } |
||
| 451 | |||
| 452 | public function setMaxAttendees(int $maxAttendees): self |
||
| 453 | { |
||
| 454 | $this->maxAttendees = $maxAttendees; |
||
| 455 | |||
| 456 | return $this; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return Collection<int, AgendaReminder> |
||
| 461 | */ |
||
| 462 | public function getReminders(): Collection |
||
| 463 | { |
||
| 464 | return $this->reminders; |
||
| 465 | } |
||
| 466 | |||
| 467 | public function addReminder(AgendaReminder $reminder): static |
||
| 475 | } |
||
| 476 | |||
| 477 | public function removeReminder(AgendaReminder $reminder): static |
||
| 478 | { |
||
| 479 | if ($this->reminders->removeElement($reminder)) { |
||
| 480 | // set the owning side to null (unless already changed) |
||
| 481 | if ($reminder->getEvent() === $this) { |
||
| 482 | $reminder->setEvent(null); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | return $this; |
||
| 487 | } |
||
| 488 | |||
| 489 | public function getCareer(): ?Career |
||
| 492 | } |
||
| 493 | |||
| 494 | public function setCareer(?Career $career): self |
||
| 495 | { |
||
| 496 | $this->career = $career; |
||
| 497 | |||
| 498 | return $this; |
||
| 499 | } |
||
| 500 | |||
| 501 | public function getPromotion(): ?Promotion |
||
| 502 | { |
||
| 503 | return $this->promotion; |
||
| 504 | } |
||
| 505 | |||
| 506 | public function setPromotion(?Promotion $promotion): self |
||
| 511 | } |
||
| 512 | |||
| 513 | public function determineType(): string |
||
| 514 | { |
||
| 515 | $resourceLinks = $this->resourceNode->getResourceLinks(); |
||
| 516 | |||
| 517 | foreach ($resourceLinks as $link) { |
||
| 518 | if (null === $link->getCourse() |
||
| 519 | && null === $link->getSession() |
||
| 520 | && null === $link->getGroup() |
||
| 521 | && null === $link->getUser() |
||
| 522 | ) { |
||
| 523 | return 'global'; |
||
| 524 | } |
||
| 525 | |||
| 536 | } |
||
| 537 | } |
||
| 538 |