InMemoryDoorsRepository.php 2.19 KB
Newer Older
1
2
3
4
5
<?php


namespace Source\Gateways\Doors;

6
use Carbon\Carbon;
7
use Source\Entities\Door;
8
use Source\Entities\HashedSearchable;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\Exceptions\EntityExistsException;
10

Jacob Priddy's avatar
Jacob Priddy committed
11
12
class InMemoryDoorsRepository implements DoorsRepository
{
13
14
15
16
17
18
    /** @var Door[] */
    protected array $doors = [];

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
19
    public function create(Door $door): ?Door
Jacob Priddy's avatar
Jacob Priddy committed
20
    {
Jacob Priddy's avatar
Jacob Priddy committed
21
22
23
24
        if ($this->findByName($door->getName())) {
            throw new EntityExistsException();
        }

25
        $this->doors[] = $door;
26

27
28
29
30
31
32
        return $door;
    }

    /**
     * @inheritDoc
     */
33
    public function getByToken(?HashedSearchable $token): ?Door
Jacob Priddy's avatar
Jacob Priddy committed
34
    {
35
36
37
38
39
40
41
42
        foreach ($this->doors as $door) {
            if ($door->hasTokenOf($token)) {
                return $door;
            }
        }

        return null;
    }
43
44
45
46
47
48
49
50
51
52
53
54
55
56

    /**
     * @inheritDoc
     */
    public function get(string $doorId): ?Door
    {
        foreach ($this->doors as $door) {
            if ($door->hasIdOf($doorId)) {
                return $door;
            }
        }

        return null;
    }
Jacob Priddy's avatar
Jacob Priddy committed
57
58
59
60
61
62
63
64

    /**
     * @inheritDoc
     */
    public function all(): array
    {
        return $this->doors;
    }
Jacob Priddy's avatar
Jacob Priddy committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78

    /**
     * @inheritDoc
     */
    public function findByName(?string $name): ?Door
    {
        foreach ($this->doors as $door) {
            if ($door->hasNameOf($name)) {
                return $door;
            }
        }

        return null;
    }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

    /**
     * @inheritDoc
     */
    public function update(string $doorId, Door $door): ?Door
    {
        foreach ($this->doors as &$doorBase) {
            if ($doorBase->hasIdOf($doorId)) {
                $door->setId($doorBase->getId());

                $doorBase = $door;

                return $doorBase;
            }
        }

        return null;
    }
Jacob Priddy's avatar
Jacob Priddy committed
97
98
99
100
101
102

    /**
     * @inheritDoc
     */
    public function delete(string $doorId): bool
    {
103
        $this->doors = array_values(array_filter($this->doors, static function (Door $door) use ($doorId) {
Jacob Priddy's avatar
Jacob Priddy committed
104
            return !$door->hasIdOf($doorId);
105
        }));
Jacob Priddy's avatar
Jacob Priddy committed
106
107
108

        return true;
    }
109
110
111
112
113
114
115
116

    /**
     * @inheritDoc
     */
    public function exists(string $doorId): bool
    {
        return (bool)$this->get($doorId);
    }
117
}