Passed
Push — master ( 7822f9...996de2 )
by Paweł
03:41 queued 35s
created

LessonRepository::getByModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Lesson;
6
use App\Model\CourseInterface;
7
use App\Model\LessonInterface;
8
use App\Model\ModuleInterface;
9
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
12
/**
13
 * @method Lesson|null find($id, $lockMode = null, $lockVersion = null)
14
 * @method Lesson|null findOneBy(array $criteria, array $orderBy = null)
15
 * @method Lesson[]    findAll()
16
 * @method Lesson[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17
 */
18
class LessonRepository extends ServiceEntityRepository implements LessonRepositoryInterface
19
{
20
    public function __construct(RegistryInterface $registry)
21
    {
22
        parent::__construct($registry, Lesson::class);
23
    }
24
25
    public function getOneById(string $id): ?LessonInterface
26
    {
27
        return $this->createQueryBuilder('l')
28
            ->where('l.id = :id')
29
            ->leftJoin('l.module', 'm')
30
            ->leftJoin('m.course', 'c')
31
            ->andWhere('c.visible != false')
32
            ->setParameter('id', $id)
33
            ->getQuery()
34
            ->getOneOrNullResult();
35
    }
36
37
    public function getByModule(ModuleInterface $module): array
38
    {
39
        return $this->createQueryBuilder('l')
40
            ->where('l.module = :module')
41
            ->leftJoin('l.module', 'm')
42
            ->leftJoin('m.course', 'c')
43
            ->andWhere('c.visible != false')
44
            ->setParameter('module', $module)
45
            ->getQuery()
46
            ->getResult();
47
    }
48
49
    public function getByCourse(CourseInterface $course): array
50
    {
51
        return $this->createQueryBuilder('l')
52
            ->where('c.id = :courseId')
53
            ->leftJoin('l.module', 'm')
54
            ->leftJoin('m.course', 'c')
55
            ->andWhere('c.visible != false')
56
            ->setParameter('courseId', $course->getId())
57
            ->getQuery()
58
            ->getResult();
59
    }
60
61
    public function getNextInModule(LessonInterface $lesson): ?LessonInterface
62
    {
63
        return $this->createQueryBuilder('l')
64
            ->where('l.module = :module')
65
            ->leftJoin('l.module', 'm')
66
            ->leftJoin('m.course', 'c')
67
            ->andWhere('c.visible != false')
68
            ->andWhere('l.position = :position')
69
            ->setParameter('module', $lesson->getModule())
70
            ->setParameter('position', $lesson->getPosition() + 1)
71
            ->getQuery()
72
            ->getOneOrNullResult();
73
    }
74
75
    public function getPreviousInModule(LessonInterface $lesson): ?LessonInterface
76
    {
77
        return $this->createQueryBuilder('l')
78
            ->where('l.module = :module')
79
            ->leftJoin('l.module', 'm')
80
            ->leftJoin('m.course', 'c')
81
            ->andWhere('c.visible != false')
82
            ->andWhere('l.position = :position')
83
            ->setParameter('module', $lesson->getModule())
84
            ->setParameter('position', $lesson->getPosition() - 1)
85
            ->getQuery()
86
            ->getOneOrNullResult();
87
    }
88
}
89