Passed
Push — master ( 18111a...f46ab8 )
by Julito
12:19
created

Skill   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 372
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 86
c 1
b 0
f 0
dl 0
loc 372
rs 9.1199
wmc 41

30 Methods

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