InMemoryDoorUserRepository.php 1.62 KB
Newer Older
1
2
3
4
5
<?php


namespace Source\Gateways\DoorUser;

6
use Source\Entities\Door;
7
8
use Source\Entities\Group;

9
10
11
12
13
14
15
16
17
18
19
20
21
class InMemoryDoorUserRepository implements DoorUserRepository
{
    /**
     * @var array
     */
    protected array $doorGroupMap = [];

    /**
     * @var array
     */
    protected array $userGroupMap = [];

    /**
22
23
24
25
26
27
28
     * @var \Source\Entities\Group[]
     */
    protected array $groups = [];

    /**
     * @param string                 $doorId
     * @param \Source\Entities\Group $group
29
     */
30
    public function addGroupToDoor(string $doorId, Group $group): void
31
32
    {
        if (isset($this->doorGroupMap[$doorId])) {
33
            $this->doorGroupMap[$doorId][] = $group;
34
        } else {
35
            $this->doorGroupMap[$doorId] = [$group];
36
37
38
39
        }
    }

    /**
40
41
     * @param string                 $userId
     * @param \Source\Entities\Group $group
42
     */
43
    public function addGroupToUser(string $userId, Group $group): void
44
45
    {
        if (isset($this->userGroupMap[$userId])) {
46
            $this->userGroupMap[$userId][] = $group;
47
        } else {
48
            $this->userGroupMap[$userId] = [$group];
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        }
    }

    /**
     * @inheritDoc
     */
    public function getDoorUserGroupIntersection(string $doorId, string $userId): array
    {
        if (!isset($this->doorGroupMap[$doorId], $this->userGroupMap[$userId])) {
            return [];
        }

        $doorGroups = $this->doorGroupMap[$doorId];
        $userGroups = $this->userGroupMap[$userId];

64
65
66
        return array_uintersect($doorGroups, $userGroups, static function (Group $a, Group $b) {
            return $a->getId() - $b->getId();
        });
67
68
    }
}