Passed
Push — master ( 9ac6fa...c6fd81 )
by Julito
10:12
created

Skill   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 401
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 92
c 1
b 0
f 0
dl 0
loc 401
rs 8.8798
wmc 44

33 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessUrlId() 0 3 1
A addItem() 0 4 1
A setDescription() 0 5 1
A hasAsset() 0 3 1
A setAsset() 0 5 1
A getIssuedSkills() 0 3 1
A setStatus() 0 5 1
A getStatus() 0 3 1
A setProfile() 0 5 1
A setUpdatedAt() 0 5 1
A addToCourse() 0 4 1
A __construct() 0 8 1
A getShortCode() 0 3 1
A getItems() 0 3 1
A __toString() 0 3 1
A setShortCode() 0 5 1
A getUpdatedAt() 0 3 1
A getProfile() 0 3 1
A getAsset() 0 3 1
A getDescription() 0 3 1
A setCriteria() 0 5 1
A getCourses() 0 3 1
A getCriteria() 0 3 1
A setAccessUrlId() 0 5 1
A setItems() 0 5 1
A setIcon() 0 5 1
A hasItem() 0 17 5
A getName() 0 3 1
B hasCourseAndSession() 0 27 8
A setName() 0 5 1
A getIcon() 0 3 1
A setCourses() 0 5 1
A getId() 0 3 1

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Core\Annotation\ApiResource;
10
use DateTime;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Collection;
13
use Doctrine\ORM\Mapping as ORM;
14
use Gedmo\Mapping\Annotation as Gedmo;
15
use Symfony\Component\Serializer\Annotation\Groups;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * @ApiResource(
20
 *     attributes={"security"="is_granted('ROLE_ADMIN')"},
21
 *     normalizationContext={"groups"={"skill:read"}}
22
 * )
23
 *
24
 * @ORM\Table(name="skill")
25
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\SkillRepository")
26
 */
27
class Skill
28
{
29
    public const STATUS_DISABLED = 0;
30
    public const STATUS_ENABLED = 1;
31
32
    /**
33
     * @Groups({"skill:read"})
34
     * @ORM\Column(name="id", type="integer")
35
     * @ORM\Id
36
     * @ORM\GeneratedValue
37
     */
38
    protected int $id;
39
40
    /**
41
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Profile", inversedBy="skills")
42
     * @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
43
     */
44
    protected ?Profile $profile = null;
45
46
    /**
47
     * @ORM\OneToMany(targetEntity="SkillRelUser", mappedBy="skill", cascade={"persist"})
48
     *
49
     * @var SkillRelUser[]|Collection
50
     */
51
    protected Collection $issuedSkills;
52
53
    /**
54
     * @ORM\OneToMany(targetEntity="SkillRelItem", mappedBy="skill", cascade={"persist"})
55
     *
56
     * @var Collection|SkillRelItem[]
57
     */
58
    protected Collection $items;
59
60
    /**
61
     * @ORM\OneToMany(targetEntity="SkillRelSkill", mappedBy="skill", cascade={"persist"})
62
     *
63
     * @var Collection|SkillRelSkill[]
64
     */
65
    protected Collection $skills;
66
67
    /**
68
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelCourse", mappedBy="skill", cascade={"persist"})
69
     *
70
     * @var Collection|SkillRelCourse[]
71
     */
72
    protected Collection $courses;
73
74
    /**
75
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SkillRelGradebook", mappedBy="skill", cascade={"persist"})
76
     *
77
     * @var Collection|SkillRelGradebook[]
78
     */
79
    protected Collection $gradeBookCategories;
80
81
    /**
82
     * @Groups({"skill:read", "skill:write"})
83
     * @Assert\NotBlank()
84
     *
85
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
86
     */
87
    protected string $name;
88
89
    /**
90
     * @Assert\NotBlank()
91
     * @Groups({"skill:read", "skill:write"})
92
     *
93
     * @ORM\Column(name="short_code", type="string", length=100, nullable=false)
94
     */
95
    protected string $shortCode;
96
97
    /**
98
     * @Groups({"skill:read", "skill:write"})
99
     *
100
     * @ORM\Column(name="description", type="text", nullable=false)
101
     */
102
    protected string $description;
103
104
    /**
105
     * @ORM\Column(name="access_url_id", type="integer", nullable=false)
106
     */
107
    protected int $accessUrlId;
108
109
    /**
110
     * @ORM\Column(name="icon", type="string", length=255, nullable=false)
111
     */
112
    protected string $icon;
113
114
    /**
115
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Asset", cascade={"persist", "remove"})
116
     * @ORM\JoinColumn(name="asset_id", referencedColumnName="id")
117
     */
118
    protected ?Asset $asset = null;
119
120
    /**
121
     * @ORM\Column(name="criteria", type="text", nullable=true)
122
     */
123
    protected ?string $criteria = null;
124
125
    /**
126
     * @ORM\Column(name="status", type="integer", nullable=false, options={"default":1})
127
     */
128
    protected int $status;
129
130
    /**
131
     * @Gedmo\Timestampable(on="update")
132
     *
133
     * @ORM\Column(name="updated_at", type="datetime", nullable=false)
134
     */
135
    protected DateTime $updatedAt;
136
137
    public function __construct()
138
    {
139
        $this->issuedSkills = new ArrayCollection();
140
        $this->items = new ArrayCollection();
141
        $this->courses = new ArrayCollection();
142
        $this->icon = '';
143
        $this->description = '';
144
        $this->status = self::STATUS_ENABLED;
145
    }
146
147
    public function __toString(): string
148
    {
149
        return $this->getName();
150
    }
151
152
    public function setName(string $name): self
153
    {
154
        $this->name = $name;
155
156
        return $this;
157
    }
158
159
    public function getName(): string
160
    {
161
        return $this->name;
162
    }
163
164
    public function getShortCode(): string
165
    {
166
        return $this->shortCode;
167
    }
168
169
    public function setShortCode(string $shortCode): self
170
    {
171
        $this->shortCode = $shortCode;
172
173
        return $this;
174
    }
175
176
    public function setDescription(string $description): self
177
    {
178
        $this->description = $description;
179
180
        return $this;
181
    }
182
183
    public function getDescription(): string
184
    {
185
        return $this->description;
186
    }
187
188
    /**
189
     * Set accessUrlId.
190
     *
191
     * @return Skill
192
     */
193
    public function setAccessUrlId(int $accessUrlId)
194
    {
195
        $this->accessUrlId = $accessUrlId;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Get accessUrlId.
202
     *
203
     * @return int
204
     */
205
    public function getAccessUrlId()
206
    {
207
        return $this->accessUrlId;
208
    }
209
210
    public function setIcon(string $icon): self
211
    {
212
        $this->icon = $icon;
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get icon.
219
     *
220
     * @return string
221
     */
222
    public function getIcon()
223
    {
224
        return $this->icon;
225
    }
226
227
    public function setCriteria(string $criteria): self
228
    {
229
        $this->criteria = $criteria;
230
231
        return $this;
232
    }
233
234
    /**
235
     * Get criteria.
236
     *
237
     * @return string
238
     */
239
    public function getCriteria()
240
    {
241
        return $this->criteria;
242
    }
243
244
    public function setStatus(int $status): self
245
    {
246
        $this->status = $status;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Get status.
253
     *
254
     * @return int
255
     */
256
    public function getStatus()
257
    {
258
        return $this->status;
259
    }
260
261
    /**
262
     * Set updatedAt.
263
     *
264
     * @param DateTime $updatedAt The update datetime
265
     *
266
     * @return Skill
267
     */
268
    public function setUpdatedAt(DateTime $updatedAt)
269
    {
270
        $this->updatedAt = $updatedAt;
271
272
        return $this;
273
    }
274
275
    /**
276
     * Get updatedAt.
277
     *
278
     * @return DateTime
279
     */
280
    public function getUpdatedAt()
281
    {
282
        return $this->updatedAt;
283
    }
284
285
    /**
286
     * Get id.
287
     *
288
     * @return int
289
     */
290
    public function getId()
291
    {
292
        return $this->id;
293
    }
294
295
    /**
296
     * @return Profile
297
     */
298
    public function getProfile()
299
    {
300
        return $this->profile;
301
    }
302
303
    public function setProfile(Profile $profile): self
304
    {
305
        $this->profile = $profile;
306
307
        return $this;
308
    }
309
310
    /**
311
     * Get issuedSkills.
312
     *
313
     * @return Collection
314
     */
315
    public function getIssuedSkills()
316
    {
317
        return $this->issuedSkills;
318
    }
319
320
    /**
321
     * @return Collection
322
     */
323
    public function getItems()
324
    {
325
        return $this->items;
326
    }
327
328
    public function setItems(ArrayCollection $items): self
329
    {
330
        $this->items = $items;
331
332
        return $this;
333
    }
334
335
    public function hasItem(int $typeId, int $itemId): bool
336
    {
337
        if (0 !== $this->getItems()->count()) {
338
            $found = false;
339
            /** @var SkillRelItem $item */
340
            foreach ($this->getItems() as $item) {
341
                if ($item->getItemId() === $itemId && $item->getItemType() === $typeId) {
342
                    $found = true;
343
344
                    break;
345
                }
346
            }
347
348
            return $found;
349
        }
350
351
        return false;
352
    }
353
354
    public function addItem(SkillRelItem $skillRelItem): void
355
    {
356
        $skillRelItem->setSkill($this);
357
        $this->items[] = $skillRelItem;
358
    }
359
360
    /**
361
     * @return Collection
362
     */
363
    public function getCourses()
364
    {
365
        return $this->courses;
366
    }
367
368
    public function setCourses(ArrayCollection $courses): self
369
    {
370
        $this->courses = $courses;
371
372
        return $this;
373
    }
374
375
    public function hasAsset(): bool
376
    {
377
        return null !== $this->asset;
378
    }
379
380
    public function getAsset(): ?Asset
381
    {
382
        return $this->asset;
383
    }
384
385
    public function setAsset(?Asset $asset): self
386
    {
387
        $this->asset = $asset;
388
389
        return $this;
390
    }
391
392
    /**
393
     * @return bool
394
     */
395
    public function hasCourseAndSession(SkillRelCourse $searchItem)
396
    {
397
        if (0 !== $this->getCourses()->count()) {
398
            $found = false;
399
            /** @var SkillRelCourse $item */
400
            foreach ($this->getCourses() as $item) {
401
                $sessionPassFilter = false;
402
                $session = $item->getSession();
403
                $sessionId = empty($session) ? 0 : $session->getId();
404
                $searchSessionId = empty($searchItem->getSession()) ? 0 : $searchItem->getSession()->getId();
405
406
                if ($sessionId === $searchSessionId) {
407
                    $sessionPassFilter = true;
408
                }
409
                if ($item->getCourse()->getId() === $searchItem->getCourse()->getId() &&
410
                    $sessionPassFilter
411
                ) {
412
                    $found = true;
413
414
                    break;
415
                }
416
            }
417
418
            return $found;
419
        }
420
421
        return false;
422
    }
423
424
    public function addToCourse(SkillRelCourse $item): void
425
    {
426
        $item->setSkill($this);
427
        $this->courses[] = $item;
428
    }
429
}
430