InMemoryOverridesRepository.php 2.81 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\Entities\Override;

9
10
class InMemoryOverridesRepository implements OverridesRepository
{
11
12
13
    /**
     * @var \Source\Entities\Override[]
     */
Jacob Priddy's avatar
Jacob Priddy committed
14
15
16
17
18
    protected array $overrides = [];

    /**
     * @inheritDoc
     */
19
    public function filterHistory(?string $doorId, ?string $userId, ?Carbon $begin, ?Carbon $end): array
Jacob Priddy's avatar
Jacob Priddy committed
20
    {
21
22
        return array_filter($this->overrides, static function (Override $override) use ($begin, $end, $doorId, $userId) {
            $include = true;
Jacob Priddy's avatar
Jacob Priddy committed
23

24
25
26
27
28
29
30
31
            if ($doorId) {
                $include = $include && $override->hasDoorIdOf($doorId);
            }

            if ($userId) {
                $include = $include && $override->hasUserIdOf($userId);
            }

32
33
34
35
            if ($begin || $end) {
                $include = $override->isActiveForDate($begin) || $override->isActiveForDate($end);
            }

36
            return $include;
Jacob Priddy's avatar
Jacob Priddy committed
37
38
39
40
41
42
        });
    }

    /**
     * @inheritDoc
     */
43
    public function activeOverrideForDoorBetween(string $doorId, Carbon $begin, Carbon $end): ?Override
Jacob Priddy's avatar
Jacob Priddy committed
44
    {
45
        $overrides = collect(array_filter($this->overrides, static function (Override $override) use ($doorId, $begin, $end) {
Jacob Priddy's avatar
Jacob Priddy committed
46
47
48
            return $override->hasDoorIdOf($doorId) &&
                ($override->isActiveForDate($begin) || $override->isActiveForDate($end) ||
                    ($begin->isBefore($override->getStart()) && $end->isAfter($override->getEnd())));
49
        }));
Jacob Priddy's avatar
Jacob Priddy committed
50

51
52
53
        return $overrides
            ->sortBy(fn (Override $item) => $item->getCreatedAt(), SORT_DESC, true)
            ->first();
54
    }
Jacob Priddy's avatar
Jacob Priddy committed
55

56
57
58
59
60
    /**
     * @inheritDoc
     */
    public function addOverride(Override $override): ?Override
    {
61
62
63
64
65
66
67
68
69
70
71
72
        $override = new Override(
            $override->getId(),
            $override->getReason(),
            $override->getUserId(),
            $override->getDoorId(),
            $override->getType(),
            $override->getStart(),
            $override->getEnd(),
            $override->getCreatedAt() ?? Carbon::now(),
            $override->getUpdatedAt() ?? Carbon::now()
        );

73
        $this->overrides[] = $override;
Jacob Priddy's avatar
Jacob Priddy committed
74

75
        return $override;
Jacob Priddy's avatar
Jacob Priddy committed
76
77
78
79
80
    }

    /**
     * @inheritDoc
     */
81
    public function updateOverride(string $overrideId, Override $override): ?Override
Jacob Priddy's avatar
Jacob Priddy committed
82
    {
83
84
85
86
        $this->overrides = array_filter($this->overrides, static function (Override $o) use ($overrideId) {
            return !$o->hasIdOf($overrideId);
        });

Jacob Priddy's avatar
Jacob Priddy committed
87
88
89
90
        $this->overrides[] = $override;

        return $override;
    }
91
92
93
94
95
96
97
98
99
100
101
102
103
104

    /**
     * @inheritDoc
     */
    public function get(string $overrideId): ?Override
    {
        foreach ($this->overrides as $override) {
            if ($override->hasIdOf($overrideId)) {
                return $override;
            }
        }

        return null;
    }
105
}