Total Complexity | 43 |
Total Lines | 394 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 |
||
40 | #[ApiResource( |
||
41 | operations: [ |
||
42 | new Get(security: "is_granted('VIEW', object)"), |
||
43 | new Put( |
||
44 | controller: UpdateCCalendarEventAction::class, |
||
45 | security: "is_granted('EDIT', object)", |
||
46 | deserialize: false |
||
47 | ), |
||
48 | new Delete(security: "is_granted('DELETE', object)"), |
||
49 | new GetCollection( |
||
50 | paginationEnabled: false, |
||
51 | security: "is_granted('ROLE_USER')", |
||
52 | output: CalendarEvent::class, |
||
53 | provider: CalendarEventProvider::class, |
||
54 | ), |
||
55 | new Post( |
||
56 | securityPostDenormalize: "is_granted('CREATE', object)", |
||
57 | processor: CCalendarEventProcessor::class |
||
58 | ), |
||
59 | ], |
||
60 | normalizationContext: ['groups' => ['calendar_event:read', 'resource_node:read']], |
||
61 | denormalizationContext: ['groups' => ['calendar_event:write']], |
||
62 | security: "is_granted('ROLE_USER')" |
||
63 | )] |
||
64 | #[ORM\Table(name: 'c_calendar_event')] |
||
65 | #[ORM\Entity(repositoryClass: CCalendarEventRepository::class)] |
||
66 | #[ApiFilter(filterClass: SearchFilter::class, properties: ['allDay' => 'boolean'])] |
||
67 | #[ApiFilter(filterClass: DateFilter::class, strategy: 'exclude_null')] |
||
68 | #[ApiFilter(filterClass: CidFilter::class)] |
||
69 | #[ApiFilter(filterClass: SidFilter::class)] |
||
70 | class CCalendarEvent extends AbstractResource implements ResourceInterface, Stringable |
||
71 | { |
||
72 | public const COLOR_STUDENT_PUBLICATION = '#FF8C00'; |
||
73 | |||
74 | public const TYPE_INVITATION = 'invitation'; |
||
75 | public const TYPE_SUBSCRIPTION = 'subscription'; |
||
76 | |||
77 | public const SUBSCRIPTION_VISIBILITY_NO = 0; |
||
78 | public const SUBSCRIPTION_VISIBILITY_ALL = 1; |
||
79 | public const SUBSCRIPTION_VISIBILITY_CLASS = 2; |
||
80 | |||
81 | #[Groups(['calendar_event:read'])] |
||
82 | #[ORM\Column(name: 'iid', type: 'integer')] |
||
83 | #[ORM\Id] |
||
84 | #[ORM\GeneratedValue] |
||
85 | protected ?int $iid = null; |
||
86 | |||
87 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
88 | #[Assert\NotBlank] |
||
89 | #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
||
90 | protected string $title; |
||
91 | |||
92 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
93 | #[Assert\NotBlank] |
||
94 | #[ORM\Column(name: 'content', type: 'text', nullable: true)] |
||
95 | protected ?string $content = null; |
||
96 | |||
97 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
98 | #[ORM\Column(name: 'start_date', type: 'datetime', nullable: true)] |
||
99 | protected ?DateTime $startDate = null; |
||
100 | |||
101 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
102 | #[ORM\Column(name: 'end_date', type: 'datetime', nullable: true)] |
||
103 | protected ?DateTime $endDate = null; |
||
104 | |||
105 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
||
106 | #[ORM\JoinColumn(name: 'parent_event_id', referencedColumnName: 'iid')] |
||
107 | protected ?CCalendarEvent $parentEvent = null; |
||
108 | |||
109 | /** |
||
110 | * @var Collection<int, CCalendarEvent> |
||
111 | */ |
||
112 | #[ORM\OneToMany(mappedBy: 'parentEvent', targetEntity: self::class)] |
||
113 | protected Collection $children; |
||
114 | |||
115 | /** |
||
116 | * @var Collection<int, CCalendarEventRepeat> |
||
117 | */ |
||
118 | #[ORM\OneToMany( |
||
119 | mappedBy: 'event', |
||
120 | targetEntity: CCalendarEventRepeat::class, |
||
121 | cascade: ['persist'], |
||
122 | orphanRemoval: true |
||
123 | )] |
||
124 | protected Collection $repeatEvents; |
||
125 | |||
126 | #[Assert\NotNull] |
||
127 | #[ORM\Column(name: 'all_day', type: 'boolean', nullable: false)] |
||
128 | protected bool $allDay; |
||
129 | |||
130 | #[ORM\Column(name: 'comment', type: 'text', nullable: true)] |
||
131 | protected ?string $comment = null; |
||
132 | |||
133 | #[ORM\Column(name: 'color', type: 'string', length: 20, nullable: true)] |
||
134 | protected ?string $color = null; |
||
135 | |||
136 | #[ORM\ManyToOne(targetEntity: Room::class)] |
||
137 | #[ORM\JoinColumn(name: 'room_id', referencedColumnName: 'id')] |
||
138 | protected ?Room $room = null; |
||
139 | |||
140 | /** |
||
141 | * @var Collection<int, CCalendarEventAttachment> |
||
142 | */ |
||
143 | #[ORM\OneToMany(mappedBy: 'event', targetEntity: CCalendarEventAttachment::class, cascade: ['persist', 'remove'])] |
||
144 | protected Collection $attachments; |
||
145 | |||
146 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
147 | #[ORM\Column(name: 'invitation_type', type: 'string', nullable: true)] |
||
148 | protected ?string $invitationType = null; |
||
149 | |||
150 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
151 | #[Assert\NotNull] |
||
152 | #[ORM\Column(name: 'collective', type: 'boolean')] |
||
153 | protected bool $collective = false; |
||
154 | |||
155 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
156 | #[ORM\Column(name: 'subscription_visibility', type: 'integer')] |
||
157 | protected int $subscriptionVisibility = self::SUBSCRIPTION_VISIBILITY_NO; |
||
158 | |||
159 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
160 | #[ORM\Column(name: 'subscription_item_id', type: 'integer', nullable: true)] |
||
161 | protected ?int $subscriptionItemId = null; |
||
162 | |||
163 | #[Groups(['calendar_event:read', 'calendar_event:write'])] |
||
164 | #[ORM\Column(name: 'max_attendees', type: 'integer')] |
||
165 | protected int $maxAttendees = 0; |
||
166 | |||
167 | public function __construct() |
||
168 | { |
||
169 | $this->children = new ArrayCollection(); |
||
170 | $this->attachments = new ArrayCollection(); |
||
171 | $this->repeatEvents = new ArrayCollection(); |
||
172 | $this->allDay = false; |
||
173 | $this->collective = false; |
||
174 | } |
||
175 | |||
176 | public function __toString(): string |
||
177 | { |
||
178 | return $this->getTitle(); |
||
179 | } |
||
180 | |||
181 | public function getTitle(): string |
||
182 | { |
||
183 | return $this->title; |
||
184 | } |
||
185 | |||
186 | public function setTitle(string $title): self |
||
187 | { |
||
188 | $this->title = $title; |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | public function getContent(): ?string |
||
194 | { |
||
195 | return $this->content; |
||
196 | } |
||
197 | |||
198 | public function setContent(string $content): self |
||
199 | { |
||
200 | $this->content = $content; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | public function getStartDate(): ?DateTime |
||
208 | } |
||
209 | |||
210 | public function setStartDate(?DateTime $startDate): self |
||
211 | { |
||
212 | $this->startDate = $startDate; |
||
213 | |||
214 | return $this; |
||
215 | } |
||
216 | |||
217 | public function getEndDate(): ?DateTime |
||
218 | { |
||
219 | return $this->endDate; |
||
220 | } |
||
221 | |||
222 | public function setEndDate(?DateTime $endDate): self |
||
223 | { |
||
224 | $this->endDate = $endDate; |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | public function getParentEvent(): ?self |
||
230 | { |
||
231 | return $this->parentEvent; |
||
232 | } |
||
233 | |||
234 | public function setParentEvent(self $parent): self |
||
235 | { |
||
236 | $this->parentEvent = $parent; |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | public function addChild(self $event): self |
||
242 | { |
||
243 | if (!$this->getChildren()->contains($event)) { |
||
244 | $this->getChildren()->add($event); |
||
245 | } |
||
246 | |||
247 | return $this; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @return Collection<int, CCalendarEvent> |
||
252 | */ |
||
253 | public function getChildren(): Collection |
||
254 | { |
||
255 | return $this->children; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param Collection<int, CCalendarEvent> $children |
||
260 | */ |
||
261 | public function setChildren(Collection $children): self |
||
262 | { |
||
263 | $this->children = $children; |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | public function isAllDay(): bool |
||
269 | { |
||
270 | return $this->allDay; |
||
271 | } |
||
272 | |||
273 | public function setAllDay(bool $allDay): self |
||
274 | { |
||
275 | $this->allDay = $allDay; |
||
276 | |||
277 | return $this; |
||
278 | } |
||
279 | |||
280 | public function getComment(): ?string |
||
281 | { |
||
282 | return $this->comment; |
||
283 | } |
||
284 | |||
285 | public function setComment(string $comment): self |
||
286 | { |
||
287 | $this->comment = $comment; |
||
288 | |||
289 | return $this; |
||
290 | } |
||
291 | |||
292 | public function getRoom(): ?Room |
||
293 | { |
||
294 | return $this->room; |
||
295 | } |
||
296 | |||
297 | public function setRoom(Room $room): self |
||
298 | { |
||
299 | $this->room = $room; |
||
300 | |||
301 | return $this; |
||
302 | } |
||
303 | |||
304 | public function getColor(): ?string |
||
305 | { |
||
306 | return $this->color; |
||
307 | } |
||
308 | |||
309 | public function setColor(string $color): self |
||
310 | { |
||
311 | $this->color = $color; |
||
312 | |||
313 | return $this; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @return Collection<int, CCalendarEventAttachment> |
||
318 | */ |
||
319 | public function getAttachments(): Collection |
||
320 | { |
||
321 | return $this->attachments; |
||
322 | } |
||
323 | |||
324 | public function setAttachments(Collection $attachments): self |
||
325 | { |
||
326 | $this->attachments = $attachments; |
||
327 | |||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | public function addAttachment(CCalendarEventAttachment $attachment): self |
||
332 | { |
||
333 | $this->attachments->add($attachment); |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @return Collection<int, CCalendarEventRepeat> |
||
340 | */ |
||
341 | public function getRepeatEvents(): Collection |
||
342 | { |
||
343 | return $this->repeatEvents; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param Collection<int, CCalendarEventRepeat> $repeatEvents |
||
348 | */ |
||
349 | public function setRepeatEvents(Collection $repeatEvents): self |
||
350 | { |
||
351 | $this->repeatEvents = $repeatEvents; |
||
352 | |||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | public function getResourceIdentifier(): int|Uuid |
||
357 | { |
||
358 | return $this->getIid(); |
||
|
|||
359 | } |
||
360 | |||
361 | public function getIid(): ?int |
||
362 | { |
||
363 | return $this->iid; |
||
364 | } |
||
365 | |||
366 | public function getResourceName(): string |
||
369 | } |
||
370 | |||
371 | public function setResourceName(string $name): self |
||
374 | } |
||
375 | |||
376 | public function isCollective(): bool |
||
377 | { |
||
378 | return $this->collective; |
||
379 | } |
||
380 | |||
381 | public function setCollective(bool $collective): self |
||
382 | { |
||
383 | $this->collective = $collective; |
||
384 | |||
385 | return $this; |
||
386 | } |
||
387 | |||
388 | public function getInvitationType(): string |
||
389 | { |
||
390 | return $this->invitationType; |
||
391 | } |
||
392 | |||
393 | public function setInvitationType(string $invitationType): self |
||
394 | { |
||
395 | $this->invitationType = $invitationType; |
||
396 | |||
397 | return $this; |
||
398 | } |
||
399 | |||
400 | public function getSubscriptionVisibility(): int |
||
401 | { |
||
402 | return $this->subscriptionVisibility; |
||
403 | } |
||
404 | |||
405 | public function setSubscriptionVisibility(int $subscriptionVisibility): self |
||
406 | { |
||
407 | $this->subscriptionVisibility = $subscriptionVisibility; |
||
408 | |||
409 | return $this; |
||
410 | } |
||
411 | |||
412 | public function getSubscriptionItemId(): ?int |
||
413 | { |
||
414 | return $this->subscriptionItemId; |
||
415 | } |
||
416 | |||
417 | public function setSubscriptionItemId(?int $subscriptionItemId): self |
||
418 | { |
||
419 | $this->subscriptionItemId = $subscriptionItemId; |
||
420 | |||
421 | return $this; |
||
422 | } |
||
423 | |||
424 | public function getMaxAttendees(): int |
||
427 | } |
||
428 | |||
429 | public function setMaxAttendees(int $maxAttendees): self |
||
430 | { |
||
434 | } |
||
435 | } |
||
436 |