Passed
Push — master ( d1c4e0...b88e45 )
by Angel Fernando Quiroz
24:34 queued 16:01
created

CCalendarEvent   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 434
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 1
Metric Value
eloc 171
c 8
b 1
f 1
dl 0
loc 434
rs 8.48
wmc 49

45 Methods

Rating   Name   Duplication   Size   Complexity  
A getEndDate() 0 3 1
A addChild() 0 7 2
A setColor() 0 5 1
A setComment() 0 5 1
A addReminder() 0 8 2
A getMaxAttendees() 0 3 1
A setSubscriptionVisibility() 0 5 1
A getTitle() 0 3 1
A setAllDay() 0 5 1
A setMaxAttendees() 0 5 1
A __toString() 0 3 1
A setAttachments() 0 5 1
A isAllDay() 0 3 1
A getSubscriptionItemId() 0 3 1
A getReminders() 0 3 1
A getStartDate() 0 3 1
A setContent() 0 5 1
A getInvitationType() 0 3 1
A setCollective() 0 5 1
A getContent() 0 3 1
A addAttachment() 0 5 1
A setRoom() 0 5 1
A getRepeatEvents() 0 3 1
A getResourceName() 0 3 1
A setTitle() 0 5 1
A isCollective() 0 3 1
A setChildren() 0 5 1
A getRoom() 0 3 1
A getAttachments() 0 3 1
A setStartDate() 0 5 1
A getColor() 0 3 1
A setInvitationType() 0 5 1
A getResourceIdentifier() 0 3 1
A getSubscriptionVisibility() 0 3 1
A getChildren() 0 3 1
A getComment() 0 3 1
A setParentEvent() 0 5 1
A setResourceName() 0 3 1
A setSubscriptionItemId() 0 5 1
A __construct() 0 8 1
A setRepeatEvents() 0 5 1
A getIid() 0 3 1
A removeReminder() 0 10 3
A setEndDate() 0 5 1
A getParentEvent() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Post;
17
use ApiPlatform\Metadata\Put;
18
use Chamilo\CoreBundle\ApiResource\CalendarEvent;
19
use Chamilo\CoreBundle\Controller\Api\UpdateCCalendarEventAction;
20
use Chamilo\CoreBundle\Entity\AbstractResource;
21
use Chamilo\CoreBundle\Entity\AgendaReminder;
22
use Chamilo\CoreBundle\Entity\ResourceInterface;
23
use Chamilo\CoreBundle\Entity\Room;
24
use Chamilo\CoreBundle\Filter\CidFilter;
25
use Chamilo\CoreBundle\Filter\GlobalEventFilter;
26
use Chamilo\CoreBundle\Filter\SidFilter;
27
use Chamilo\CoreBundle\State\CalendarEventProvider;
28
use Chamilo\CoreBundle\State\CCalendarEventProcessor;
29
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
30
use DateTime;
31
use Doctrine\Common\Collections\ArrayCollection;
32
use Doctrine\Common\Collections\Collection;
33
use Doctrine\ORM\Mapping as ORM;
34
use Stringable;
35
use Symfony\Component\Serializer\Annotation\Groups;
36
use Symfony\Component\Uid\Uuid;
37
use Symfony\Component\Validator\Constraints as Assert;
38
39
/**
40
 * Calendar events.
41
 */
42
#[ApiResource(
43
    operations: [
44
        new Get(security: "is_granted('VIEW', object)"),
45
        new Put(
46
            controller: UpdateCCalendarEventAction::class,
47
            security: "is_granted('EDIT', object)",
48
            deserialize: false
49
        ),
50
        new Delete(security: "is_granted('DELETE', object)"),
51
        new GetCollection(
52
            paginationEnabled: false,
53
            security: "is_granted('ROLE_USER')",
54
            output: CalendarEvent::class,
55
            provider: CalendarEventProvider::class,
56
        ),
57
        new Post(
58
            securityPostDenormalize: "is_granted('CREATE', object)",
59
            processor: CCalendarEventProcessor::class
60
        ),
61
    ],
62
    normalizationContext: ['groups' => ['calendar_event:read', 'resource_node:read']],
63
    denormalizationContext: ['groups' => ['calendar_event:write']],
64
    security: "is_granted('ROLE_USER')"
65
)]
66
#[ORM\Table(name: 'c_calendar_event')]
67
#[ORM\Entity(repositoryClass: CCalendarEventRepository::class)]
68
#[ApiFilter(filterClass: SearchFilter::class, properties: ['allDay' => 'boolean'])]
69
#[ApiFilter(filterClass: DateFilter::class, strategy: 'exclude_null')]
70
#[ApiFilter(filterClass: CidFilter::class)]
71
#[ApiFilter(filterClass: SidFilter::class)]
72
#[ApiFilter(GlobalEventFilter::class, properties: ["type"])]
73
class CCalendarEvent extends AbstractResource implements ResourceInterface, Stringable
74
{
75
    public const COLOR_STUDENT_PUBLICATION = '#FF8C00';
76
77
    public const TYPE_INVITATION = 'invitation';
78
    public const TYPE_SUBSCRIPTION = 'subscription';
79
80
    public const SUBSCRIPTION_VISIBILITY_NO = 0;
81
    public const SUBSCRIPTION_VISIBILITY_ALL = 1;
82
    public const SUBSCRIPTION_VISIBILITY_CLASS = 2;
83
84
    #[Groups(['calendar_event:read'])]
85
    #[ORM\Column(name: 'iid', type: 'integer')]
86
    #[ORM\Id]
87
    #[ORM\GeneratedValue]
88
    protected ?int $iid = null;
89
90
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
91
    #[Assert\NotBlank]
92
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
93
    protected string $title;
94
95
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
96
    #[Assert\NotBlank]
97
    #[ORM\Column(name: 'content', type: 'text', nullable: true)]
98
    protected ?string $content = null;
99
100
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
101
    #[ORM\Column(name: 'start_date', type: 'datetime', nullable: true)]
102
    protected ?DateTime $startDate = null;
103
104
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
105
    #[ORM\Column(name: 'end_date', type: 'datetime', nullable: true)]
106
    protected ?DateTime $endDate = null;
107
108
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
109
    #[ORM\JoinColumn(name: 'parent_event_id', referencedColumnName: 'iid')]
110
    protected ?CCalendarEvent $parentEvent = null;
111
112
    /**
113
     * @var Collection<int, CCalendarEvent>
114
     */
115
    #[ORM\OneToMany(mappedBy: 'parentEvent', targetEntity: self::class)]
116
    protected Collection $children;
117
118
    /**
119
     * @var Collection<int, CCalendarEventRepeat>
120
     */
121
    #[ORM\OneToMany(
122
        mappedBy: 'event',
123
        targetEntity: CCalendarEventRepeat::class,
124
        cascade: ['persist'],
125
        orphanRemoval: true
126
    )]
127
    protected Collection $repeatEvents;
128
129
    #[Assert\NotNull]
130
    #[ORM\Column(name: 'all_day', type: 'boolean', nullable: false)]
131
    protected bool $allDay;
132
133
    #[ORM\Column(name: 'comment', type: 'text', nullable: true)]
134
    protected ?string $comment = null;
135
136
    #[Groups(['calendar_event:write', 'calendar_event:read'])]
137
    #[ORM\Column(name: 'color', type: 'string', length: 20, nullable: true)]
138
    protected ?string $color = null;
139
140
    #[ORM\ManyToOne(targetEntity: Room::class)]
141
    #[ORM\JoinColumn(name: 'room_id', referencedColumnName: 'id')]
142
    protected ?Room $room = null;
143
144
    /**
145
     * @var Collection<int, CCalendarEventAttachment>
146
     */
147
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: CCalendarEventAttachment::class, cascade: ['persist', 'remove'])]
148
    protected Collection $attachments;
149
150
    #[Groups(['calendar_event:write'])]
151
    #[ORM\Column(name: 'invitation_type', type: 'string', nullable: true)]
152
    protected ?string $invitationType = null;
153
154
    #[Groups(['calendar_event:write'])]
155
    #[Assert\NotNull]
156
    #[ORM\Column(name: 'collective', type: 'boolean')]
157
    protected bool $collective = false;
158
159
    #[Groups(['calendar_event:write'])]
160
    #[ORM\Column(name: 'subscription_visibility', type: 'integer')]
161
    protected int $subscriptionVisibility = self::SUBSCRIPTION_VISIBILITY_NO;
162
163
    #[Groups(['calendar_event:write'])]
164
    #[ORM\Column(name: 'subscription_item_id', type: 'integer', nullable: true)]
165
    protected ?int $subscriptionItemId = null;
166
167
    #[Groups(['calendar_event:write'])]
168
    #[ORM\Column(name: 'max_attendees', type: 'integer')]
169
    protected int $maxAttendees = 0;
170
171
    /**
172
     * @var Collection<int, AgendaReminder>
173
     */
174
    #[Groups(['calendar_event:write'])]
175
    #[ORM\OneToMany(mappedBy: 'event', targetEntity: AgendaReminder::class, cascade: ['persist'], orphanRemoval: true)]
176
    private Collection $reminders;
177
178
    public function __construct()
179
    {
180
        $this->children = new ArrayCollection();
181
        $this->attachments = new ArrayCollection();
182
        $this->repeatEvents = new ArrayCollection();
183
        $this->allDay = false;
184
        $this->collective = false;
185
        $this->reminders = new ArrayCollection();
186
    }
187
188
    public function __toString(): string
189
    {
190
        return $this->getTitle();
191
    }
192
193
    public function getTitle(): string
194
    {
195
        return $this->title;
196
    }
197
198
    public function setTitle(string $title): self
199
    {
200
        $this->title = $title;
201
202
        return $this;
203
    }
204
205
    public function getContent(): ?string
206
    {
207
        return $this->content;
208
    }
209
210
    public function setContent(string $content): self
211
    {
212
        $this->content = $content;
213
214
        return $this;
215
    }
216
217
    public function getStartDate(): ?DateTime
218
    {
219
        return $this->startDate;
220
    }
221
222
    public function setStartDate(?DateTime $startDate): self
223
    {
224
        $this->startDate = $startDate;
225
226
        return $this;
227
    }
228
229
    public function getEndDate(): ?DateTime
230
    {
231
        return $this->endDate;
232
    }
233
234
    public function setEndDate(?DateTime $endDate): self
235
    {
236
        $this->endDate = $endDate;
237
238
        return $this;
239
    }
240
241
    public function getParentEvent(): ?self
242
    {
243
        return $this->parentEvent;
244
    }
245
246
    public function setParentEvent(self $parent): self
247
    {
248
        $this->parentEvent = $parent;
249
250
        return $this;
251
    }
252
253
    public function addChild(self $event): self
254
    {
255
        if (!$this->getChildren()->contains($event)) {
256
            $this->getChildren()->add($event);
257
        }
258
259
        return $this;
260
    }
261
262
    /**
263
     * @return Collection<int, CCalendarEvent>
264
     */
265
    public function getChildren(): Collection
266
    {
267
        return $this->children;
268
    }
269
270
    /**
271
     * @param Collection<int, CCalendarEvent> $children
272
     */
273
    public function setChildren(Collection $children): self
274
    {
275
        $this->children = $children;
276
277
        return $this;
278
    }
279
280
    public function isAllDay(): bool
281
    {
282
        return $this->allDay;
283
    }
284
285
    public function setAllDay(bool $allDay): self
286
    {
287
        $this->allDay = $allDay;
288
289
        return $this;
290
    }
291
292
    public function getComment(): ?string
293
    {
294
        return $this->comment;
295
    }
296
297
    public function setComment(string $comment): self
298
    {
299
        $this->comment = $comment;
300
301
        return $this;
302
    }
303
304
    public function getRoom(): ?Room
305
    {
306
        return $this->room;
307
    }
308
309
    public function setRoom(Room $room): self
310
    {
311
        $this->room = $room;
312
313
        return $this;
314
    }
315
316
    public function getColor(): ?string
317
    {
318
        return $this->color;
319
    }
320
321
    public function setColor(string $color): self
322
    {
323
        $this->color = $color;
324
325
        return $this;
326
    }
327
328
    /**
329
     * @return Collection<int, CCalendarEventAttachment>
330
     */
331
    public function getAttachments(): Collection
332
    {
333
        return $this->attachments;
334
    }
335
336
    public function setAttachments(Collection $attachments): self
337
    {
338
        $this->attachments = $attachments;
339
340
        return $this;
341
    }
342
343
    public function addAttachment(CCalendarEventAttachment $attachment): self
344
    {
345
        $this->attachments->add($attachment);
346
347
        return $this;
348
    }
349
350
    /**
351
     * @return Collection<int, CCalendarEventRepeat>
352
     */
353
    public function getRepeatEvents(): Collection
354
    {
355
        return $this->repeatEvents;
356
    }
357
358
    /**
359
     * @param Collection<int, CCalendarEventRepeat> $repeatEvents
360
     */
361
    public function setRepeatEvents(Collection $repeatEvents): self
362
    {
363
        $this->repeatEvents = $repeatEvents;
364
365
        return $this;
366
    }
367
368
    public function getResourceIdentifier(): int|Uuid
369
    {
370
        return $this->getIid();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIid() could return the type null which is incompatible with the type-hinted return Symfony\Component\Uid\Uuid|integer. Consider adding an additional type-check to rule them out.
Loading history...
371
    }
372
373
    public function getIid(): ?int
374
    {
375
        return $this->iid;
376
    }
377
378
    public function getResourceName(): string
379
    {
380
        return $this->getTitle();
381
    }
382
383
    public function setResourceName(string $name): self
384
    {
385
        return $this->setTitle($name);
386
    }
387
388
    public function isCollective(): bool
389
    {
390
        return $this->collective;
391
    }
392
393
    public function setCollective(bool $collective): self
394
    {
395
        $this->collective = $collective;
396
397
        return $this;
398
    }
399
400
    public function getInvitationType(): ?string
401
    {
402
        return $this->invitationType;
403
    }
404
405
    public function setInvitationType(string $invitationType): self
406
    {
407
        $this->invitationType = $invitationType;
408
409
        return $this;
410
    }
411
412
    public function getSubscriptionVisibility(): int
413
    {
414
        return $this->subscriptionVisibility;
415
    }
416
417
    public function setSubscriptionVisibility(int $subscriptionVisibility): self
418
    {
419
        $this->subscriptionVisibility = $subscriptionVisibility;
420
421
        return $this;
422
    }
423
424
    public function getSubscriptionItemId(): ?int
425
    {
426
        return $this->subscriptionItemId;
427
    }
428
429
    public function setSubscriptionItemId(?int $subscriptionItemId): self
430
    {
431
        $this->subscriptionItemId = $subscriptionItemId;
432
433
        return $this;
434
    }
435
436
    public function getMaxAttendees(): int
437
    {
438
        return $this->maxAttendees;
439
    }
440
441
    public function setMaxAttendees(int $maxAttendees): self
442
    {
443
        $this->maxAttendees = $maxAttendees;
444
445
        return $this;
446
    }
447
448
    /**
449
     * @return Collection<int, AgendaReminder>
450
     */
451
    public function getReminders(): Collection
452
    {
453
        return $this->reminders;
454
    }
455
456
    public function addReminder(AgendaReminder $reminder): static
457
    {
458
        if (!$this->reminders->contains($reminder)) {
459
            $this->reminders->add($reminder);
460
            $reminder->setEvent($this);
461
        }
462
463
        return $this;
464
    }
465
466
    public function removeReminder(AgendaReminder $reminder): static
467
    {
468
        if ($this->reminders->removeElement($reminder)) {
469
            // set the owning side to null (unless already changed)
470
            if ($reminder->getEvent() === $this) {
471
                $reminder->setEvent(null);
472
            }
473
        }
474
475
        return $this;
476
    }
477
}
478