Completed
Push — master ( 9dadb1...edd250 )
by Julito
12:09 queued 19s
created

CGroup   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 545
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 91
c 0
b 0
f 0
dl 0
loc 545
rs 8.8
wmc 45

43 Methods

Rating   Name   Duplication   Size   Complexity  
A setResourceName() 0 3 1
A getResourceName() 0 3 1
A getResourceIdentifier() 0 3 1
A setAnnouncementsState() 0 5 1
A setSecretDirectory() 0 5 1
A getWikiState() 0 3 1
A setName() 0 5 1
A getMaxStudent() 0 3 1
A setForumState() 0 5 1
A getCategory() 0 3 1
A setMembers() 0 5 1
A setStatus() 0 5 1
A setCategory() 0 5 1
A getStatus() 0 3 1
A getSelfUnregistrationAllowed() 0 3 1
A getChatState() 0 3 1
A getDescription() 0 3 1
A getDocState() 0 3 1
A getIid() 0 3 1
A setChatState() 0 5 1
A setWorkState() 0 5 1
A __toString() 0 3 1
A setTutors() 0 5 1
A __construct() 0 3 1
A getWorkState() 0 3 1
A getName() 0 3 1
A getMembers() 0 3 1
A getSecretDirectory() 0 3 1
A setMaxStudent() 0 5 1
A setDocumentAccess() 0 5 1
A getAnnouncementsState() 0 3 1
A getCalendarState() 0 3 1
A setWikiState() 0 5 1
A setSelfRegistrationAllowed() 0 5 1
A setSelfUnregistrationAllowed() 0 5 1
A getSelfRegistrationAllowed() 0 3 1
A getForumState() 0 3 1
A userIsTutor() 0 21 3
A setDescription() 0 5 1
A getTutors() 0 3 1
A getDocumentAccess() 0 3 1
A setDocState() 0 5 1
A setCalendarState() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like CGroup 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 CGroup, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiResource;
8
use Chamilo\CoreBundle\Entity\AbstractResource;
9
use Chamilo\CoreBundle\Entity\ResourceInterface;
10
use Chamilo\CoreBundle\Entity\User;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Criteria;
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * @ORM\Table(
18
 *  name="c_group_info",
19
 *  indexes={
20
 *  }
21
 * )
22
 *
23
 * @ApiResource()
24
 * @ORM\Entity
25
 */
26
class CGroup extends AbstractResource implements ResourceInterface
27
{
28
    /**
29
     * @var int
30
     *
31
     * @ORM\Column(name="iid", type="integer")
32
     * @ORM\Id
33
     * @ORM\GeneratedValue
34
     */
35
    protected $iid;
36
37
    /**
38
     * @var string
39
     * @Assert\NotBlank()
40
     * @ORM\Column(name="name", type="string", length=100, nullable=true)
41
     */
42
    protected $name;
43
44
    /**
45
     * @var bool
46
     *
47
     * @ORM\Column(name="status", type="boolean", nullable=true)
48
     */
49
    protected $status;
50
51
    /**
52
     * @var CGroupCategory
53
     *
54
     * @ORM\ManyToOne(targetEntity="CGroupCategory", cascade={"persist"})
55
     * @ORM\JoinColumn(name="category_id", referencedColumnName="iid", onDelete="CASCADE")
56
     */
57
    protected $category;
58
59
    /**
60
     * @var string
61
     *
62
     * @ORM\Column(name="description", type="text", nullable=true)
63
     */
64
    protected $description;
65
66
    /**
67
     * @var int
68
     *
69
     * @ORM\Column(name="max_student", type="integer", nullable=false)
70
     */
71
    protected $maxStudent;
72
73
    /**
74
     * @var bool
75
     *
76
     * @ORM\Column(name="doc_state", type="boolean", nullable=false)
77
     */
78
    protected $docState;
79
80
    /**
81
     * @var bool
82
     *
83
     * @ORM\Column(name="calendar_state", type="boolean", nullable=false)
84
     */
85
    protected $calendarState;
86
87
    /**
88
     * @var bool
89
     *
90
     * @ORM\Column(name="work_state", type="boolean", nullable=false)
91
     */
92
    protected $workState;
93
94
    /**
95
     * @var bool
96
     *
97
     * @ORM\Column(name="announcements_state", type="boolean", nullable=false)
98
     */
99
    protected $announcementsState;
100
101
    /**
102
     * @var bool
103
     *
104
     * @ORM\Column(name="forum_state", type="boolean", nullable=false)
105
     */
106
    protected $forumState;
107
108
    /**
109
     * @var bool
110
     *
111
     * @ORM\Column(name="wiki_state", type="boolean", nullable=false)
112
     */
113
    protected $wikiState;
114
115
    /**
116
     * @var bool
117
     *
118
     * @ORM\Column(name="chat_state", type="boolean", nullable=false)
119
     */
120
    protected $chatState;
121
122
    /**
123
     * @var string
124
     *
125
     * @ORM\Column(name="secret_directory", type="string", length=255, nullable=true)
126
     */
127
    protected $secretDirectory;
128
129
    /**
130
     * @var bool
131
     *
132
     * @ORM\Column(name="self_registration_allowed", type="boolean", nullable=false)
133
     */
134
    protected $selfRegistrationAllowed;
135
136
    /**
137
     * @var bool
138
     *
139
     * @ORM\Column(name="self_unregistration_allowed", type="boolean", nullable=false)
140
     */
141
    protected $selfUnregistrationAllowed;
142
143
    /**
144
     * @var int
145
     *
146
     * @ORM\Column(name="document_access", type="integer", nullable=false, options={"default":0})
147
     */
148
    protected $documentAccess;
149
150
    /**
151
     * @var ArrayCollection|CGroupRelUser[]
152
     *
153
     * @ORM\OneToMany(targetEntity="CGroupRelUser", mappedBy="group")
154
     */
155
    protected $members;
156
157
    /**
158
     * @var ArrayCollection|CGroupRelTutor[]
159
     *
160
     * @ORM\OneToMany(targetEntity="CGroupRelTutor", mappedBy="group")
161
     */
162
    protected $tutors;
163
164
    public function __construct()
165
    {
166
        $this->status = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
167
    }
168
169
    public function __toString(): string
170
    {
171
        return $this->getName();
172
    }
173
174
    /**
175
     * Get iid.
176
     *
177
     * @return int
178
     */
179
    public function getIid()
180
    {
181
        return $this->iid;
182
    }
183
184
    /**
185
     * Set name.
186
     *
187
     * @param string $name
188
     *
189
     * @return CGroup
190
     */
191
    public function setName($name)
192
    {
193
        $this->name = $name;
194
195
        return $this;
196
    }
197
198
    /**
199
     * Get name.
200
     *
201
     * @return string
202
     */
203
    public function getName()
204
    {
205
        return (string) $this->name;
206
    }
207
208
    /**
209
     * Set status.
210
     *
211
     * @param bool $status
212
     *
213
     * @return CGroup
214
     */
215
    public function setStatus($status)
216
    {
217
        $this->status = $status;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Get status.
224
     *
225
     * @return bool
226
     */
227
    public function getStatus()
228
    {
229
        return $this->status;
230
    }
231
232
    /**
233
     * Set description.
234
     *
235
     * @param string $description
236
     */
237
    public function setDescription($description): self
238
    {
239
        $this->description = $description;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Get description.
246
     *
247
     * @return string
248
     */
249
    public function getDescription()
250
    {
251
        return $this->description;
252
    }
253
254
    public function setMaxStudent(int $maxStudent): self
255
    {
256
        $this->maxStudent = $maxStudent;
257
258
        return $this;
259
    }
260
261
    public function getMaxStudent(): int
262
    {
263
        return (int) $this->maxStudent;
264
    }
265
266
    /**
267
     * Set docState.
268
     *
269
     * @param bool $docState
270
     */
271
    public function setDocState($docState): self
272
    {
273
        $this->docState = $docState;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Get docState.
280
     *
281
     * @return bool
282
     */
283
    public function getDocState()
284
    {
285
        return $this->docState;
286
    }
287
288
    /**
289
     * Set calendarState.
290
     *
291
     * @param bool $calendarState
292
     */
293
    public function setCalendarState($calendarState): self
294
    {
295
        $this->calendarState = $calendarState;
296
297
        return $this;
298
    }
299
300
    /**
301
     * Get calendarState.
302
     *
303
     * @return bool
304
     */
305
    public function getCalendarState()
306
    {
307
        return $this->calendarState;
308
    }
309
310
    /**
311
     * Set workState.
312
     *
313
     * @param bool $workState
314
     */
315
    public function setWorkState($workState): self
316
    {
317
        $this->workState = $workState;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Get workState.
324
     *
325
     * @return bool
326
     */
327
    public function getWorkState()
328
    {
329
        return $this->workState;
330
    }
331
332
    /**
333
     * Set announcementsState.
334
     *
335
     * @param bool $announcementsState
336
     */
337
    public function setAnnouncementsState($announcementsState): self
338
    {
339
        $this->announcementsState = $announcementsState;
340
341
        return $this;
342
    }
343
344
    /**
345
     * Get announcementsState.
346
     *
347
     * @return bool
348
     */
349
    public function getAnnouncementsState()
350
    {
351
        return $this->announcementsState;
352
    }
353
354
    /**
355
     * Set forumState.
356
     *
357
     * @param bool $forumState
358
     */
359
    public function setForumState($forumState): self
360
    {
361
        $this->forumState = $forumState;
362
363
        return $this;
364
    }
365
366
    /**
367
     * Get forumState.
368
     *
369
     * @return bool
370
     */
371
    public function getForumState()
372
    {
373
        return $this->forumState;
374
    }
375
376
    /**
377
     * Set wikiState.
378
     *
379
     * @param bool $wikiState
380
     *
381
     * @return CGroup
382
     */
383
    public function setWikiState($wikiState)
384
    {
385
        $this->wikiState = $wikiState;
386
387
        return $this;
388
    }
389
390
    /**
391
     * Get wikiState.
392
     *
393
     * @return bool
394
     */
395
    public function getWikiState()
396
    {
397
        return $this->wikiState;
398
    }
399
400
    /**
401
     * Set chatState.
402
     *
403
     * @param bool $chatState
404
     *
405
     * @return CGroup
406
     */
407
    public function setChatState($chatState)
408
    {
409
        $this->chatState = $chatState;
410
411
        return $this;
412
    }
413
414
    /**
415
     * Get chatState.
416
     *
417
     * @return bool
418
     */
419
    public function getChatState()
420
    {
421
        return $this->chatState;
422
    }
423
424
    /**
425
     * Set secretDirectory.
426
     *
427
     * @param string $secretDirectory
428
     *
429
     * @return CGroup
430
     */
431
    public function setSecretDirectory($secretDirectory)
432
    {
433
        $this->secretDirectory = $secretDirectory;
434
435
        return $this;
436
    }
437
438
    public function getSecretDirectory(): string
439
    {
440
        return $this->secretDirectory;
441
    }
442
443
    /**
444
     * Set selfRegistrationAllowed.
445
     *
446
     * @param bool $selfRegistrationAllowed
447
     */
448
    public function setSelfRegistrationAllowed($selfRegistrationAllowed): self
449
    {
450
        $this->selfRegistrationAllowed = $selfRegistrationAllowed;
451
452
        return $this;
453
    }
454
455
    /**
456
     * Get selfRegistrationAllowed.
457
     *
458
     * @return bool
459
     */
460
    public function getSelfRegistrationAllowed()
461
    {
462
        return $this->selfRegistrationAllowed;
463
    }
464
465
    /**
466
     * Set selfUnregistrationAllowed.
467
     *
468
     * @param bool $selfUnregistrationAllowed
469
     */
470
    public function setSelfUnregistrationAllowed($selfUnregistrationAllowed): self
471
    {
472
        $this->selfUnregistrationAllowed = $selfUnregistrationAllowed;
473
474
        return $this;
475
    }
476
477
    /**
478
     * Get selfUnregistrationAllowed.
479
     *
480
     * @return bool
481
     */
482
    public function getSelfUnregistrationAllowed()
483
    {
484
        return $this->selfUnregistrationAllowed;
485
    }
486
487
    public function getDocumentAccess(): int
488
    {
489
        return $this->documentAccess;
490
    }
491
492
    public function setDocumentAccess(int $documentAccess): self
493
    {
494
        $this->documentAccess = $documentAccess;
495
496
        return $this;
497
    }
498
499
    public function getMembers(): ArrayCollection
500
    {
501
        return $this->members;
502
    }
503
504
    public function setMembers(ArrayCollection $members): self
505
    {
506
        $this->members = $members;
507
508
        return $this;
509
    }
510
511
    public function getTutors(): ArrayCollection
512
    {
513
        return $this->tutors;
514
    }
515
516
    public function setTutors(ArrayCollection $tutors): self
517
    {
518
        $this->tutors = $tutors;
519
520
        return $this;
521
    }
522
523
    public function userIsTutor(User $user = null): bool
524
    {
525
        if (empty($user)) {
526
            return false;
527
        }
528
529
        if (0 === $this->tutors->count()) {
530
            return false;
531
        }
532
533
        $criteria = Criteria::create()
534
            ->where(
535
                Criteria::expr()->eq('cId', $this->course)
0 ignored issues
show
Bug Best Practice introduced by
The property course does not exist on Chamilo\CourseBundle\Entity\CGroup. Did you maybe forget to declare it?
Loading history...
536
            )
537
            ->andWhere(
538
                Criteria::expr()->eq('user', $user)
539
            );
540
541
        $relation = $this->tutors->matching($criteria);
542
543
        return $relation->count() > 0;
544
    }
545
546
    public function getCategory(): CGroupCategory
547
    {
548
        return $this->category;
549
    }
550
551
    public function setCategory(CGroupCategory $category = null): CGroup
552
    {
553
        $this->category = $category;
554
555
        return $this;
556
    }
557
558
    public function getResourceIdentifier(): int
559
    {
560
        return $this->iid;
561
    }
562
563
    public function getResourceName(): string
564
    {
565
        return $this->getName();
566
    }
567
568
    public function setResourceName(string $name): self
569
    {
570
        return $this->setName($name);
571
    }
572
}
573