Total Complexity | 77 |
Total Lines | 623 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Meeting 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 Meeting, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Meeting |
||
39 | { |
||
40 | /** @var string meeting type name */ |
||
41 | public $typeName; |
||
42 | |||
43 | /** @var DateTime meeting start time as a DateTime instance */ |
||
44 | public $startDateTime; |
||
45 | |||
46 | /** @var string meeting formatted start time */ |
||
47 | public $formattedStartTime; |
||
48 | |||
49 | /** @var DateInterval meeting duration as a DateInterval instance */ |
||
50 | public $durationInterval; |
||
51 | |||
52 | /** @var string meeting formatted duration */ |
||
53 | public $formattedDuration; |
||
54 | |||
55 | /** @var string */ |
||
56 | public $statusName; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | * @ORM\Column(type="integer", name="id") |
||
61 | * @ORM\Id |
||
62 | * @ORM\GeneratedValue() |
||
63 | */ |
||
64 | protected $id; |
||
65 | |||
66 | /** |
||
67 | * @var int the remote zoom meeting identifier |
||
68 | * @ORM\Column(name="meeting_id", type="string") |
||
69 | */ |
||
70 | protected $meetingId; |
||
71 | |||
72 | /** |
||
73 | * @var User |
||
74 | * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User") |
||
75 | * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true) |
||
76 | */ |
||
77 | protected $user; |
||
78 | |||
79 | /** |
||
80 | * @var Course |
||
81 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course") |
||
82 | * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=true) |
||
83 | */ |
||
84 | protected $course; |
||
85 | |||
86 | /** |
||
87 | * @var CGroupInfo |
||
88 | * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroupInfo") |
||
89 | * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true) |
||
90 | */ |
||
91 | protected $group; |
||
92 | |||
93 | /** |
||
94 | * @var Session |
||
95 | * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session") |
||
96 | * @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=true) |
||
97 | */ |
||
98 | protected $session; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | * @ORM\Column(type="text", name="meeting_list_item_json", nullable=true) |
||
103 | */ |
||
104 | protected $meetingListItemJson; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | * @ORM\Column(type="text", name="meeting_info_get_json", nullable=true) |
||
109 | */ |
||
110 | protected $meetingInfoGetJson; |
||
111 | |||
112 | /** |
||
113 | * @var bool |
||
114 | * |
||
115 | * @ORM\Column(type="boolean", name="sign_attendance") |
||
116 | */ |
||
117 | protected $signAttendance; |
||
118 | |||
119 | /** |
||
120 | * @var string|null |
||
121 | * |
||
122 | * @ORM\Column(type="text", name="reason_to_sign_attendance", nullable=true) |
||
123 | */ |
||
124 | protected $reasonToSignAttendance; |
||
125 | |||
126 | /** @var MeetingListItem */ |
||
127 | protected $meetingListItem; |
||
128 | |||
129 | /** @var MeetingInfoGet */ |
||
130 | protected $meetingInfoGet; |
||
131 | |||
132 | /** |
||
133 | * @var MeetingActivity[]|ArrayCollection |
||
134 | * @ORM\OrderBy({"createdAt" = "DESC"}) |
||
135 | * @ORM\OneToMany(targetEntity="MeetingActivity", mappedBy="meeting", cascade={"persist", "remove"}) |
||
136 | */ |
||
137 | protected $activities; |
||
138 | |||
139 | /** |
||
140 | * @var Registrant[]|ArrayCollection |
||
141 | * |
||
142 | * @ORM\OneToMany(targetEntity="Registrant", mappedBy="meeting", cascade={"persist", "remove"}) |
||
143 | */ |
||
144 | protected $registrants; |
||
145 | |||
146 | /** |
||
147 | * @var Recording[]|ArrayCollection |
||
148 | * |
||
149 | * @ORM\OneToMany(targetEntity="Recording", mappedBy="meeting", cascade={"persist"}, orphanRemoval=true) |
||
150 | */ |
||
151 | protected $recordings; |
||
152 | |||
153 | /** |
||
154 | * @var ArrayCollection<int, Signature> |
||
155 | * |
||
156 | * @ORM\OneToMany(targetEntity="Signature", mappedBy="meeting") |
||
157 | */ |
||
158 | protected $signatures; |
||
159 | |||
160 | public function __construct() |
||
161 | { |
||
162 | $this->registrants = new ArrayCollection(); |
||
163 | $this->recordings = new ArrayCollection(); |
||
164 | $this->activities = new ArrayCollection(); |
||
165 | $this->signAttendance = false; |
||
166 | $this->signatures = new ArrayCollection(); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | public function __toString() |
||
173 | { |
||
174 | return sprintf('Meeting %d', $this->id); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return int |
||
179 | */ |
||
180 | public function getId() |
||
181 | { |
||
182 | return $this->id; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return int |
||
187 | */ |
||
188 | public function getMeetingId() |
||
189 | { |
||
190 | return $this->meetingId; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param int $meetingId |
||
195 | * |
||
196 | * @return Meeting |
||
197 | */ |
||
198 | public function setMeetingId($meetingId) |
||
199 | { |
||
200 | $this->meetingId = $meetingId; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return User |
||
207 | */ |
||
208 | public function getUser() |
||
209 | { |
||
210 | return $this->user; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return Course |
||
215 | */ |
||
216 | public function getCourse() |
||
217 | { |
||
218 | return $this->course; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return Session |
||
223 | */ |
||
224 | public function getSession() |
||
225 | { |
||
226 | return $this->session; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return Registrant[]|ArrayCollection |
||
231 | */ |
||
232 | public function getRegistrants() |
||
233 | { |
||
234 | return $this->registrants; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return Recording[]|ArrayCollection |
||
239 | */ |
||
240 | public function getRecordings() |
||
241 | { |
||
242 | return $this->recordings; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @return MeetingActivity[]|ArrayCollection |
||
247 | */ |
||
248 | public function getActivities() |
||
249 | { |
||
250 | return $this->activities; |
||
251 | } |
||
252 | |||
253 | public function addActivity(MeetingActivity $activity) |
||
254 | { |
||
255 | $activity->setMeeting($this); |
||
256 | $this->activities[] = $activity; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param MeetingActivity[]|ArrayCollection $activities |
||
261 | * |
||
262 | * @return Meeting |
||
263 | */ |
||
264 | public function setActivities($activities) |
||
265 | { |
||
266 | $this->activities = $activities; |
||
267 | |||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @ORM\PostLoad |
||
273 | * |
||
274 | * @throws Exception |
||
275 | */ |
||
276 | public function postLoad() |
||
277 | { |
||
278 | if (null !== $this->meetingListItemJson) { |
||
279 | $this->meetingListItem = MeetingListItem::fromJson($this->meetingListItemJson); |
||
280 | } |
||
281 | if (null !== $this->meetingInfoGetJson) { |
||
282 | $this->meetingInfoGet = MeetingInfoGet::fromJson($this->meetingInfoGetJson); |
||
283 | } |
||
284 | $this->initializeDisplayableProperties(); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @ORM\PostUpdate |
||
289 | * |
||
290 | * @throws Exception |
||
291 | */ |
||
292 | public function postUpdate() |
||
293 | { |
||
294 | $this->initializeDisplayableProperties(); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @ORM\PreFlush |
||
299 | */ |
||
300 | public function preFlush() |
||
301 | { |
||
302 | if (null !== $this->meetingListItem) { |
||
303 | $this->meetingListItemJson = json_encode($this->meetingListItem); |
||
304 | } |
||
305 | if (null !== $this->meetingInfoGet) { |
||
306 | $this->meetingInfoGetJson = json_encode($this->meetingInfoGet); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @return MeetingListItem |
||
312 | */ |
||
313 | public function getMeetingListItem() |
||
314 | { |
||
315 | return $this->meetingListItem; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return MeetingInfoGet |
||
320 | */ |
||
321 | public function getMeetingInfoGet() |
||
322 | { |
||
323 | return $this->meetingInfoGet; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param User $user |
||
328 | * |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function setUser($user) |
||
332 | { |
||
333 | $this->user = $user; |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @param Course $course |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setCourse($course) |
||
344 | { |
||
345 | $this->course = $course; |
||
346 | |||
347 | return $this; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param Session $session |
||
352 | * |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function setSession($session) |
||
356 | { |
||
357 | $this->session = $session; |
||
358 | |||
359 | return $this; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @return CGroupInfo |
||
364 | */ |
||
365 | public function getGroup() |
||
366 | { |
||
367 | return $this->group; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param CGroupInfo $group |
||
372 | * |
||
373 | * @return Meeting |
||
374 | */ |
||
375 | public function setGroup($group) |
||
376 | { |
||
377 | $this->group = $group; |
||
378 | |||
379 | return $this; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @param MeetingListItem $meetingListItem |
||
384 | * |
||
385 | * @throws Exception |
||
386 | * |
||
387 | * @return Meeting |
||
388 | */ |
||
389 | public function setMeetingListItem($meetingListItem) |
||
390 | { |
||
391 | if (null === $this->meetingId) { |
||
392 | $this->meetingId = $meetingListItem->id; |
||
|
|||
393 | } elseif ($this->meetingId != $meetingListItem->id) { |
||
394 | throw new Exception('the Meeting identifier differs from the MeetingListItem identifier'); |
||
395 | } |
||
396 | $this->meetingListItem = $meetingListItem; |
||
397 | |||
398 | return $this; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param MeetingInfoGet $meetingInfoGet |
||
403 | * |
||
404 | * @throws Exception |
||
405 | * |
||
406 | * @return Meeting |
||
407 | */ |
||
408 | public function setMeetingInfoGet($meetingInfoGet) |
||
409 | { |
||
410 | if (null === $this->meetingId) { |
||
411 | $this->meetingId = $meetingInfoGet->id; |
||
412 | } elseif ($this->meetingId != $meetingInfoGet->id) { |
||
413 | throw new Exception('the Meeting identifier differs from the MeetingInfoGet identifier'); |
||
414 | } |
||
415 | $this->meetingInfoGet = $meetingInfoGet; |
||
416 | $this->initializeDisplayableProperties(); |
||
417 | |||
418 | return $this; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @return bool |
||
423 | */ |
||
424 | public function isCourseMeeting() |
||
425 | { |
||
426 | return null !== $this->course; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @return bool |
||
431 | */ |
||
432 | public function isCourseGroupMeeting() |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @return bool |
||
439 | */ |
||
440 | public function isUserMeeting() |
||
441 | { |
||
442 | return null !== $this->user && null === $this->course; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function isGlobalMeeting() |
||
449 | { |
||
450 | return null === $this->user && null === $this->course; |
||
451 | } |
||
452 | |||
453 | public function setStatus($status) |
||
454 | { |
||
455 | $this->meetingInfoGet->status = $status; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Builds the list of users that can register into this meeting. |
||
460 | * Zoom requires an email address, therefore users without an email address are excluded from the list. |
||
461 | * |
||
462 | * @return User[] the list of users |
||
463 | */ |
||
464 | public function getRegistrableUsers() |
||
465 | { |
||
466 | $users = []; |
||
467 | if (!$this->isCourseMeeting()) { |
||
468 | $criteria = ['active' => true]; |
||
469 | $users = Database::getManager()->getRepository('ChamiloUserBundle:User')->findBy($criteria); |
||
470 | } elseif (null === $this->session) { |
||
471 | if (null !== $this->course) { |
||
472 | /** @var CourseRelUser $courseRelUser */ |
||
473 | foreach ($this->course->getUsers() as $courseRelUser) { |
||
474 | $users[] = $courseRelUser->getUser(); |
||
475 | } |
||
476 | } |
||
477 | } else { |
||
478 | if (null !== $this->course) { |
||
479 | $subscriptions = $this->session->getUserCourseSubscriptionsByStatus($this->course, Session::STUDENT); |
||
480 | if ($subscriptions) { |
||
481 | /** @var SessionRelCourseRelUser $sessionCourseUser */ |
||
482 | foreach ($subscriptions as $sessionCourseUser) { |
||
483 | $users[] = $sessionCourseUser->getUser(); |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | } |
||
488 | |||
489 | $activeUsersWithEmail = []; |
||
490 | foreach ($users as $user) { |
||
491 | if ($user->isActive() && !empty($user->getEmail())) { |
||
492 | $activeUsersWithEmail[] = $user; |
||
493 | } |
||
494 | } |
||
495 | |||
496 | return $activeUsersWithEmail; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @return bool |
||
501 | */ |
||
502 | public function requiresDateAndDuration() |
||
503 | { |
||
504 | return MeetingInfoGet::TYPE_SCHEDULED === $this->meetingInfoGet->type |
||
505 | || MeetingInfoGet::TYPE_RECURRING_WITH_FIXED_TIME === $this->meetingInfoGet->type; |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * @return bool |
||
510 | */ |
||
511 | public function requiresRegistration() |
||
512 | { |
||
513 | return MeetingSettings::APPROVAL_TYPE_AUTOMATICALLY_APPROVE === $this->meetingInfoGet->settings->approval_type; |
||
514 | /*return |
||
515 | MeetingSettings::APPROVAL_TYPE_NO_REGISTRATION_REQUIRED != $this->meetingInfoGet->settings->approval_type;*/ |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @return bool |
||
520 | */ |
||
521 | public function hasCloudAutoRecordingEnabled() |
||
522 | { |
||
523 | return \ZoomPlugin::RECORDING_TYPE_NONE !== $this->meetingInfoGet->settings->auto_recording; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param User $user |
||
528 | * |
||
529 | * @return bool |
||
530 | */ |
||
531 | public function hasRegisteredUser($user) |
||
532 | { |
||
533 | return $this->getRegistrants()->exists( |
||
534 | function (Registrant $registrantEntity) use (&$user) { |
||
535 | return $registrantEntity->getUser() === $user; |
||
536 | } |
||
537 | ); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * @param User $user |
||
542 | * |
||
543 | * @return Registrant|null |
||
544 | */ |
||
545 | public function getRegistrant($user) |
||
546 | { |
||
547 | foreach ($this->getRegistrants() as $registrant) { |
||
548 | if ($registrant->getUser() === $user) { |
||
549 | return $registrant; |
||
550 | } |
||
551 | } |
||
552 | |||
553 | return null; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Generates a short presentation of the meeting for the future participant. |
||
558 | * To be displayed above the "Enter meeting" link. |
||
559 | * |
||
560 | * @return string |
||
561 | */ |
||
562 | public function getIntroduction() |
||
563 | { |
||
564 | $introduction = sprintf('<h1>%s</h1>', $this->meetingInfoGet->topic).PHP_EOL; |
||
565 | if (!$this->isGlobalMeeting()) { |
||
566 | if (!empty($this->formattedStartTime)) { |
||
567 | $introduction .= $this->formattedStartTime; |
||
568 | if (!empty($this->formattedDuration)) { |
||
569 | $introduction .= ' ('.$this->formattedDuration.')'; |
||
570 | } |
||
571 | $introduction .= PHP_EOL; |
||
572 | } |
||
573 | } |
||
574 | if ($this->user) { |
||
575 | $introduction .= sprintf('<p>%s</p>', $this->user->getFullname()).PHP_EOL; |
||
576 | } elseif ($this->isCourseMeeting()) { |
||
577 | if (null === $this->session) { |
||
578 | $introduction .= sprintf('<p class="main">%s</p>', $this->course).PHP_EOL; |
||
579 | } else { |
||
580 | $introduction .= sprintf('<p class="main">%s (%s)</p>', $this->course, $this->session).PHP_EOL; |
||
581 | } |
||
582 | } |
||
583 | if (!empty($this->meetingInfoGet->agenda)) { |
||
584 | $introduction .= sprintf('<p>%s</p>', $this->meetingInfoGet->agenda).PHP_EOL; |
||
585 | } |
||
586 | |||
587 | return $introduction; |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * @throws Exception on unexpected start_time or duration |
||
592 | */ |
||
593 | private function initializeDisplayableProperties() |
||
594 | { |
||
595 | $zoomPlugin = new \ZoomPlugin(); |
||
596 | |||
597 | $typeList = [ |
||
598 | API\Meeting::TYPE_INSTANT => $zoomPlugin->get_lang('InstantMeeting'), |
||
599 | API\Meeting::TYPE_SCHEDULED => $zoomPlugin->get_lang('ScheduledMeeting'), |
||
600 | API\Meeting::TYPE_RECURRING_WITH_NO_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithNoFixedTime'), |
||
601 | API\Meeting::TYPE_RECURRING_WITH_FIXED_TIME => $zoomPlugin->get_lang('RecurringWithFixedTime'), |
||
602 | ]; |
||
603 | $this->typeName = $typeList[$this->meetingInfoGet->type]; |
||
604 | |||
605 | if (property_exists($this, 'status')) { |
||
606 | $statusList = [ |
||
607 | 'waiting' => $zoomPlugin->get_lang('Waiting'), |
||
608 | 'started' => $zoomPlugin->get_lang('Started'), |
||
609 | 'finished' => $zoomPlugin->get_lang('Finished'), |
||
610 | ]; |
||
611 | $this->statusName = $statusList[$this->meetingInfoGet->status]; |
||
612 | } |
||
613 | $this->startDateTime = null; |
||
614 | $this->formattedStartTime = ''; |
||
615 | $this->durationInterval = null; |
||
616 | $this->formattedDuration = ''; |
||
617 | if (!empty($this->meetingInfoGet->start_time)) { |
||
618 | $this->startDateTime = new DateTime($this->meetingInfoGet->start_time); |
||
619 | $this->startDateTime->setTimezone(new DateTimeZone(api_get_timezone())); |
||
620 | $this->formattedStartTime = $this->startDateTime->format('Y-m-d H:i'); |
||
621 | } |
||
622 | |||
623 | if (!empty($this->meetingInfoGet->duration)) { |
||
624 | $now = new DateTime(); |
||
625 | $later = new DateTime(); |
||
626 | $later->add(new DateInterval('PT'.$this->meetingInfoGet->duration.'M')); |
||
627 | $this->durationInterval = $later->diff($now); |
||
628 | $this->formattedDuration = $this->durationInterval->format($zoomPlugin->get_lang('DurationFormat')); |
||
629 | } |
||
630 | } |
||
631 | |||
632 | public function isSignAttendance(): bool |
||
633 | { |
||
634 | return $this->signAttendance; |
||
635 | } |
||
636 | |||
637 | public function setSignAttendance(bool $signAttendance): Meeting |
||
638 | { |
||
639 | $this->signAttendance = $signAttendance; |
||
640 | return $this; |
||
641 | } |
||
642 | |||
643 | public function getReasonToSignAttendance(): ?string |
||
644 | { |
||
645 | return $this->reasonToSignAttendance; |
||
646 | } |
||
647 | |||
648 | public function setReasonToSignAttendance(string $reasonToSignAttendance): Meeting |
||
649 | { |
||
650 | $this->reasonToSignAttendance = $reasonToSignAttendance; |
||
651 | |||
652 | return $this; |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * @return ArrayCollection<int, Signature> |
||
657 | */ |
||
658 | public function getSignatures(): ArrayCollection |
||
661 | } |
||
662 | } |
||
663 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.