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


namespace Source\Gateways\Doors;

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

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

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

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

26
27
28
29
30
31
        return $door;
    }

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

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

    /**
     * @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
56
57
58
59
60
61
62
63

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

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

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

    /**
     * @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
96
97
98
99
100
101
102
103
104
105
106
107

    /**
     * @inheritDoc
     */
    public function delete(string $doorId): bool
    {
        $this->doors = array_filter($this->doors, static function (Door $door) use ($doorId) {
            return !$door->hasIdOf($doorId);
        });

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

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