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

SkillRepository::deleteAsset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Session;
11
use Chamilo\CoreBundle\Entity\Skill;
12
use Chamilo\CoreBundle\Entity\User;
13
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
14
use Doctrine\Common\Collections\Criteria;
15
use Doctrine\ORM\Query\Expr\Join;
16
use Doctrine\Persistence\ManagerRegistry;
17
18
/**
19
 * @author Angel Fernando Quiroz Campos <[email protected]>
20
 */
21
class SkillRepository extends ServiceEntityRepository
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, Skill::class);
26
    }
27
28
    public function deleteAsset(Skill $skill): void
29
    {
30
        if ($skill->hasAsset()) {
31
            $asset = $skill->getAsset();
32
            $skill->setAsset(null);
33
34
            $this->getEntityManager()->persist($skill);
35
            $this->getEntityManager()->remove($asset);
36
            $this->getEntityManager()->flush();
37
        }
38
    }
39
40
    public function update(Skill $skill): void
41
    {
42
        $this->getEntityManager()->persist($skill);
43
        $this->getEntityManager()->flush();
44
    }
45
46
    public function delete(Skill $skill): void
47
    {
48
        $this->getEntityManager()->remove($skill);
49
        $this->getEntityManager()->flush();
50
    }
51
52
    /**
53
     * Get the last acquired skill by a user on course and/or session.
54
     *
55
     * @param User    $user    The user
56
     * @param Course  $course  The course
57
     * @param Session $session The session
58
     *
59
     * @return Skill
60
     */
61
    public function getLastByUser(User $user, Course $course = null, Session $session = null)
62
    {
63
        $qb = $this->createQueryBuilder('s');
64
65
        $qb
66
            ->innerJoin(
67
                'ChamiloCoreBundle:SkillRelUser',
68
                'su',
69
                Join::WITH,
70
                's.id = su.skill'
71
            )
72
            ->where(
73
                $qb->expr()->eq('su.user', $user->getId())
74
            )
75
        ;
76
77
        if (null !== $course) {
78
            $qb->andWhere(
79
                $qb->expr()->eq('su.course', $course->getId())
80
            );
81
        }
82
83
        if (null !== $session) {
84
            $qb->andWhere(
85
                $qb->expr()->eq('su.session', $session->getId())
86
            );
87
        }
88
89
        $qb
90
            ->setMaxResults(1)
91
            ->orderBy('su.id', Criteria::DESC)
92
        ;
93
94
        return $qb->getQuery()->getOneOrNullResult();
95
    }
96
}
97