DatabaseDoorScheduleRepository.php 2.98 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
<?php


namespace Source\Gateways\DoorSchedule;

Jacob Priddy's avatar
Jacob Priddy committed
6
use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use Source\Sanitize\CastsTo;
use Source\Entities\Schedule;
use Illuminate\Database\ConnectionInterface;

class DatabaseDoorScheduleRepository implements DoorScheduleRepository
{
    use CastsTo;

    /**
     * @var \Illuminate\Database\ConnectionInterface
     */
    protected ConnectionInterface $db;

    public function __construct(ConnectionInterface $db)
    {
        $this->db = $db;
    }

    /**
     * @inheritDoc
     */
    public function getActiveSchedulesForDoor(string $doorId): array
    {
30
        $type = Schedule::TYPE_OPEN_MODE;
Jacob Priddy's avatar
Jacob Priddy committed
31
        $query = <<<QUERY
32
select S.id, S.group_id, S.type, S.rset, S.start, S.end, S.duration_ms, S.description, S.created_at, S.updated_at
33
FROM schedules as S
34
35
INNER JOIN door_group AS DG ON DG.group_id = S.group_id AND DG.door_id = :DOOR_ID
WHERE S.type = :TYPE AND ((CURRENT_DATE BETWEEN S.start AND S.end) OR (CURRENT_DATE > S.start AND S.end IS NULL))
Jacob Priddy's avatar
Jacob Priddy committed
36
37
38
QUERY;

        $schedules = $this->db->select($query, [
39
40
            ':DOOR_ID' => $this->castToInt($doorId),
            ':TYPE' => $this->castToInt($type),
Jacob Priddy's avatar
Jacob Priddy committed
41
42
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
43
44
45
46
47
48
49
50
        return array_map(function ($schedule) {
            return new Schedule(
                $schedule->id,
                $schedule->group_id,
                $schedule->type,
                $schedule->rset,
                $schedule->duration_ms,
                $schedule->description,
51
52
53
54
                $this->castToCarbon($schedule->start),
                $this->castToCarbon($schedule->end),
                $this->castToCarbon($schedule->created_at),
                $this->castToCarbon($schedule->updated_at)
Jacob Priddy's avatar
Jacob Priddy committed
55
56
57
58
59
60
61
            );
        }, $schedules);
    }

    /**
     * @inheritDoc
     */
62
    public function getSchedulesForDoorBetween(string $doorId, Carbon $begin, Carbon $end, int $type = Schedule::TYPE_OPEN_MODE): array
Jacob Priddy's avatar
Jacob Priddy committed
63
64
65
66
67
    {
        $query = <<<QUERY
select S.id, S.group_id, S.type, S.rset, S.start, S.end, S.duration_ms, S.description, S.created_at, S.updated_at
FROM schedules as S
INNER JOIN door_group AS DG ON DG.group_id = S.group_id AND DG.door_id = :DOOR_ID
68
69
WHERE S.type = :TYPE AND (((:BEGIN, :END) OVERLAPS (s.start, S.end))
        OR (S.start < :END AND S.end IS NULL))
Jacob Priddy's avatar
Jacob Priddy committed
70
71
72
73
QUERY;

        $schedules = $this->db->select($query, [
            ':DOOR_ID' => $this->castToInt($doorId),
74
75
76
            ':TYPE' => $this->castToInt($type),
            ':BEGIN' => $begin,
            ':END' => $end,
Jacob Priddy's avatar
Jacob Priddy committed
77
78
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
79
80
81
82
83
84
85
        return array_map(function ($schedule) {
            return new Schedule(
                $schedule->id,
                $schedule->group_id,
                $schedule->type,
                $schedule->rset,
                $schedule->duration_ms,
86
                $schedule->description,
87
88
89
90
                $this->castToCarbon($schedule->start),
                $this->castToCarbon($schedule->end),
                $this->castToCarbon($schedule->created_at),
                $this->castToCarbon($schedule->updated_at)
Jacob Priddy's avatar
Jacob Priddy committed
91
92
93
94
            );
        }, $schedules);
    }
}