InMemoryEntriesRepository.php 1.12 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
    /**
     * @inheritDoc
     */
27
    public function add(Entry $entry): ?Entry
28
29
30
    {
        $this->entries[] = $entry;

31
        return $entry;
32
33
34
35
36
    }

    /**
     * @inheritDoc
     */
37
    public function get(?Carbon $begin = null, ?Carbon $end = null, ?string $userId = null, ?string $doorId = null): array
38
    {
39
        return array_values(array_filter($this->entries, static function (Entry $entry) use ($begin, $end, $userId, $doorId) {
Jacob Priddy's avatar
Jacob Priddy committed
40
            $include = $entry->isBetween($begin, $end);
41

Jacob Priddy's avatar
Jacob Priddy committed
42
43
44
            if ($userId) {
                $include = $include && $entry->hasUserIdOf($userId);
            }
45

46
47
            if ($doorId) {
                $include = $include && $entry->hasDoorIdOf($doorId);
Jacob Priddy's avatar
Jacob Priddy committed
48
            }
49

Jacob Priddy's avatar
Jacob Priddy committed
50
            return $include;
51
52
        }));
    }
53
}