InMemoryEntriesRepository.php 1.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php


namespace Source\Gateways\Entries;

use Carbon\Carbon;
use Source\Entities\Entry;

class InMemoryEntriesRepository implements EntriesRepository
{
    /**
     * @var \Source\Entities\Entry[]
     */
    protected array $entries = [];

16
17
18
19
20
21
22
23
    /**
     * @return \Source\Entities\Entry[]
     */
    public function all(): array
    {
        return $this->entries;
    }

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    /**
     * @inheritDoc
     */
    public function add(Entry $entry): bool
    {
        $this->entries[] = $entry;

        return true;
    }

    /**
     * @inheritDoc
     */
    public function getForUserBetween(string $userId, Carbon $begin, Carbon $end): array
    {
        return array_values(array_filter($this->entries, static function (Entry $entry) use ($userId, $begin, $end) {
            return $entry->isBetween($begin, $end) && $entry->hasUserIdOf($userId);
        }));
    }

    /**
     * @inheritDoc
     */
    public function getForDoorBetween(string $doorId, Carbon $begin, Carbon $end): array
    {
        return array_values(array_filter($this->entries, static function (Entry $entry) use ($doorId, $begin, $end) {
            return $entry->isBetween($begin, $end) && $entry->hasDoorIdOf($doorId);
        }));
    }

    /**
     * @inheritDoc
     */
    public function getForUserAndDoorBetween(string $userId, string $doorId, Carbon $begin, Carbon $end): array
    {
        return array_values(array_filter($this->entries, static function (Entry $entry) use ($userId, $doorId, $begin, $end) {
            return $entry->isBetween($begin, $end) && $entry->hasDoorIdOf($doorId) && $entry->hasUserIdOf($userId);
        }));
    }
}