Passed
Push — master ( b525a2...6ed794 )
by Marcel
06:23
created

ExcuseNoteRepository::removeBetween()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\ExcuseNote;
6
use App\Entity\Student;
7
use DateTime;
8
use Doctrine\ORM\Tools\Pagination\Paginator;
9
10
class ExcuseNoteRepository extends AbstractRepository implements ExcuseNoteRepositoryInterface {
11
12
    public function findByStudent(Student $student): array {
13
        return $this->em->getRepository(ExcuseNote::class)
14
            ->findBy([
15
                'student' => $student
16
            ]);
17
    }
18
19
    public function findByStudents(array $students): array {
20
        $studentIds = array_map(fn(Student $student) => $student->getId(), $students);
21
22
        $qb = $this->em->createQueryBuilder();
23
        $qb->select(['n', 's'])
24
            ->from(ExcuseNote::class, 'n')
25
            ->leftJoin('n.student', 's')
26
            ->where(
27
                $qb->expr()->in('s.id', ':ids')
28
            )
29
            ->setParameter('ids', $studentIds);
30
31
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
32
    }
33
34
    public function findByStudentsAndDate(array $students, DateTime $date): array {
35
        $studentIds = array_map(fn(Student $student) => $student->getId(), $students);
36
37
        $qb = $this->em->createQueryBuilder();
38
        $qb->select(['n', 's'])
39
            ->from(ExcuseNote::class, 'n')
40
            ->leftJoin('n.student', 's')
41
            ->where('n.from.date <= :date')
42
            ->andWhere('n.until.date >= :date')
43
            ->andWhere(
44
                $qb->expr()->in('s.id', ':ids')
45
            )
46
            ->setParameter('ids', $studentIds)
47
            ->setParameter('date', $date);
48
49
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
52
    public function persist(ExcuseNote $note): void {
53
        $this->em->persist($note);
54
        $this->em->flush();
55
    }
56
57
    public function remove(ExcuseNote $note): void {
58
        $this->em->remove($note);
59
        $this->em->flush();
60
    }
61
62
    public function removeBetween(DateTime $start, DateTime $end): int {
63
        return $this->em->createQueryBuilder()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->em->create...->getQuery()->execute() could return the type array<mixed,mixed> which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
64
            ->delete(ExcuseNote::class, 'e')
65
            ->where('e.until.date >= :start')
66
            ->andWhere('e.until.date <= :end')
67
            ->setParameter('start', $start)
68
            ->setParameter('end', $end)
69
            ->getQuery()
70
            ->execute();
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function getPaginator(int $itemsPerPage, int &$page, ?Student $student, DateTime $start, DateTime $end): Paginator {
77
        $qb = $this->em->createQueryBuilder()
78
            ->select(['e', 's'])
79
            ->from(ExcuseNote::class, 'e')
80
            ->leftJoin('e.student', 's')
81
            ->where('e.from.date >= :start')
82
            ->andWhere('e.until.date <= :end')
83
            ->setParameter('start', $start)
84
            ->setParameter('end', $end)
85
            ->orderBy('e.from.date', 'desc');
86
87
        if($student !== null) {
88
            $qb->andWhere('s.id = :student')
89
                ->setParameter('student', $student->getId());
90
        }
91
92
        if($page < 1) {
93
            $page = 1;
94
        }
95
96
        $offset = ($page - 1) * $itemsPerPage;
97
98
        $paginator = new Paginator($qb);
99
        $paginator->getQuery()
100
            ->setMaxResults($itemsPerPage)
101
            ->setFirstResult($offset);
102
103
        return $paginator;
104
    }
105
}