Passed
Push — master ( 76abd2...ab5d59 )
by Julito
10:01
created

TrackECourseAccessRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\TrackECourseAccess;
7
use Chamilo\UserBundle\Entity\User;
8
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
use Doctrine\Common\Persistence\ManagerRegistry;
10
11
/**
12
 * TrackECourseAccessRepository.
13
 *
14
 * @package Chamilo\CoreBundle\Repository
15
 *
16
 * @author Angel Fernando Quiroz Campos <[email protected]>
17
 */
18
class TrackECourseAccessRepository extends ServiceEntityRepository
19
{
20
    /**
21
     * TrackECourseAccessRepository constructor.
22
     *
23
     * @param ManagerRegistry $registry
24
     */
25
    public function __construct(ManagerRegistry $registry)
26
    {
27
        parent::__construct($registry, TrackECourseAccess::class);
28
    }
29
30
    /**
31
     * Get the last registered access by an user.
32
     *
33
     * @param User $user The user
34
     *
35
     * @return TrackECourseAccess The access if exists.
36
     *                            Otherwise return null
37
     */
38
    public function getLastAccessByUser(User $user)
39
    {
40
        if (empty($user)) {
41
            return null;
42
        }
43
44
        $lastAccess = $this->findBy(
45
            ['userId' => $user->getId()],
46
            ['courseAccessId' => 'DESC'],
47
            1
48
        );
49
50
        if (!empty($lastAccess)) {
51
            return $lastAccess[0];
52
        }
53
54
        return null;
55
    }
56
}
57