DatabaseOverridesRepository.php 5.18 KB
Newer Older
1
2
3
4
5
<?php


namespace Source\Gateways\Overrides;

Jacob Priddy's avatar
Jacob Priddy committed
6
7
8
use Carbon\Carbon;
use Source\Sanitize\CastsTo;
use Source\Entities\Override;
9
10
use Illuminate\Database\ConnectionInterface;
use Source\Exceptions\EntityNotFoundException;
Jacob Priddy's avatar
Jacob Priddy committed
11

12
13
class DatabaseOverridesRepository implements OverridesRepository
{
Jacob Priddy's avatar
Jacob Priddy committed
14
15
    use CastsTo;

16
17
18
19
20
21
22
23
24
25
    /**
     * @var \Illuminate\Database\ConnectionInterface
     */
    protected ConnectionInterface $db;

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

Jacob Priddy's avatar
Jacob Priddy committed
26
27
28
29
30
31
32
33
    /**
     * @param \App\Override $override
     * @return \Source\Entities\Override
     */
    protected static function toOverride(\App\Override $override): Override
    {
        return new Override(
            $override->getAttribute('id'),
34
            $override->getAttribute('reason'),
Jacob Priddy's avatar
Jacob Priddy committed
35
36
37
38
39
40
41
42
43
44
            $override->getAttribute('user_id'),
            $override->getAttribute('door_id'),
            $override->getAttribute('type'),
            $override->getAttribute('start'),
            $override->getAttribute('end'),
            $override->getAttribute('created_at'),
            $override->getAttribute('updated_at')
        );
    }

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    /**
     * @param $override
     * @return \Source\Entities\Override
     */
    protected function toOverrideFromRaw($override): Override
    {
        return new Override(
            $override->id,
            $override->reason,
            $override->user_id,
            $override->door_id,
            $override->type,
            $this->castToCarbon($override->start),
            $this->castToCarbon($override->end),
            $this->castToCarbon($override->created_at),
            $this->castToCarbon($override->updated_at)
        );
    }

Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
67
68
69
70
    /**
     * @inheritDoc
     */
    public function overrideHistoryForDoor(string $doorId): array
    {
        $overrides = \App\Override::query()
            ->where('door_id', $this->castToInt($doorId))
71
            ->orderByDesc('created_at')
Jacob Priddy's avatar
Jacob Priddy committed
72
73
74
75
76
77
78
79
80
81
82
83
            ->get()->values()->all();

        return array_map(static function (\App\Override $override) {
            return self::toOverride($override);
        }, $overrides);
    }

    /**
     * @inheritDoc
     */
    public function overrideHistoryBetween(Carbon $begin, Carbon $end): array
    {
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        $query = <<<QUERY
select A.id, A.reason, A.user_id, A.door_id, A.type, A.start, A.end, A.created_at, A.updated_at
from overrides as A
WHERE ((:BEGIN, :END) OVERLAPS (A.start, A.end))
  ORDER BY A.created_at DESC
QUERY;

        $overrides = $this->db->select($query, [
            ':BEGIN' => $begin,
            ':END' => $end,
        ]);

        return array_map(function ($override) {
            return $this->toOverrideFromRaw($override);
Jacob Priddy's avatar
Jacob Priddy committed
98
99
100
101
102
103
        }, $overrides);
    }

    /**
     * @inheritDoc
     */
104
    public function activeOverrideForDoorBetween(string $doorId, Carbon $begin, Carbon $end): ?Override
Jacob Priddy's avatar
Jacob Priddy committed
105
    {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        $query = <<<QUERY
select A.id, A.reason, A.user_id, A.door_id, A.type, A.start, A.end, A.created_at, A.updated_at
from overrides as A
WHERE A.door_id = :DOOR_ID
  AND ((:BEGIN, :END) OVERLAPS (A.start, A.end))
  ORDER BY A.created_at DESC
  LIMIT 1
QUERY;

        $override = $this->db->selectOne($query, [
            ':DOOR_ID' => $this->castToInt($doorId),
            ':BEGIN' => $begin,
            ':END' => $end,
        ]);
Jacob Priddy's avatar
Jacob Priddy committed
120
121
122
123
124

        if (!$override) {
            return null;
        }

125
        return $this->toOverrideFromRaw($override);
Jacob Priddy's avatar
Jacob Priddy committed
126
127
128
129
130
131
132
133
134
135
136
137
138
    }

    /**
     * @inheritDoc
     */
    public function addOverride(Override $override): ?Override
    {
        $o = new \App\Override();
        $o->setAttribute('user_id', $override->getUserId());
        $o->setAttribute('door_id', $override->getDoorId());
        $o->setAttribute('type', $override->getType());
        $o->setAttribute('start', $override->getStart());
        $o->setAttribute('end', $override->getEnd());
139
        $o->setAttribute('reason', $override->getReason());
Jacob Priddy's avatar
Jacob Priddy committed
140

141
142
143
144
        if ($override->getCreatedAt()) {
            $o->setCreatedAt($override->getCreatedAt());
        }

Jacob Priddy's avatar
Jacob Priddy committed
145
146
147
148
149
150
        if (!$o->save()) {
            return null;
        }

        return self::toOverride($o);
    }
151
152
153
154
155
156
157
158
159
160
161
162
163

    /**
     * @inheritDoc
     */
    public function updateOverride(string $overrideId, Override $override): ?Override
    {
        /** @var \App\Override $o */
        $o = \App\Override::query()->find($this->castToInt($overrideId));

        if (!$o) {
            throw new EntityNotFoundException('Override with id "' . $overrideId . '" does not exist.');
        }

164
165
166
        $o->setAttribute('user_id', $override->getUserId());
        $o->setAttribute('door_id', $override->getDoorId());
        $o->setAttribute('type', $override->getType());
167
168
169
170
171
172
173
174
175
176
        $o->setAttribute('start', $override->getStart());
        $o->setAttribute('end', $override->getEnd());
        $o->setAttribute('reason', $override->getReason());

        if (!$o->save()) {
            return null;
        }

        return self::toOverride($o);
    }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

    /**
     * @inheritDoc
     */
    public function get(string $overrideId): ?Override
    {
        /** @var \App\Override $o */
        $o = \App\Override::query()->find($this->castToInt($overrideId));

        if (!$o) {
            return null;
        }

        return self::toOverride($o);
    }
192
}