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


namespace Source\Gateways\Attempts;

use Carbon\Carbon;
use Source\Entities\Attempt;

class InMemoryAttemptsRepository implements AttemptsRepository
{
    /**
     * @var \Source\Entities\Attempt[]
     */
    protected array $attempts = [];

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

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(Attempt $attempt): bool
    {
        $this->attempts[] = $attempt;

        return true;
    }

    /**
     * @inheritDoc
     */
    public function getBetween(Carbon $begin, Carbon $end): array
    {
        return array_values(array_filter($this->attempts, static function (Attempt $attempt) use ($begin, $end) {
            return $attempt->isBetween($begin, $end);
        }));
    }

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

    /**
     * @inheritDoc
     */
    public function getCountForDoorBetween(string $doorId, Carbon $begin, Carbon $end): int
    {
        return count(array_filter($this->attempts, static function (Attempt $attempt) use ($doorId, $begin, $end) {
            return $attempt->isBetween($begin, $end) && $attempt->hasDoorIdOf($doorId);
        }));
    }
}