DatabaseGroupUserRepository.php 2.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php


namespace Source\Gateways\GroupUser;

use App\User;
use App\Group;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;

class DatabaseGroupUserRepository implements GroupUserRepository
{
    /**
     * @inheritDoc
     */
    public function getGroupsForUser(string $userId): array
    {
        /** @var User|null $user */
20
        $user = User::find((int)$userId);
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

        if (!$user) {
            throw new EntityNotFoundException();
        }

        return array_map(static function (Group $group) {
            return DatabaseGroupsRepository::makeGroupFromDbGroup($group);
        }, $user->groups()->get()->values()->all());
    }

    /**
     * @inheritDoc
     */
    public function getUsersForGroup(string $groupId): array
    {
        /** @var Group|null $group */
37
        $group = Group::find((int)$groupId);
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

        if (!$group) {
            throw new EntityNotFoundException();
        }

        return array_map(static function (User $user) {
            return DatabaseUsersRepository::makeUserFromDbUser($user);
        }, $group->users()->get()->values()->all());
    }

    /**
     * @inheritDoc
     */
    public function addUserToGroup(string $userId, string $groupId): void
    {
        /** @var User|null $user */
54
        $user = User::find((int)$userId);
55
        /** @var Group|null $group */
56
        $group = Group::find((int)$groupId);
57

Jacob Priddy's avatar
Jacob Priddy committed
58
59
60
61
62
63
        if (!$user) {
            throw new EntityNotFoundException('User not found.');
        }

        if (!$group) {
            throw new EntityNotFoundException('Group not found.');
64
65
66
67
68
69
70
71
72
73
74
        }

        $user->groups()->attach($groupId);
    }

    /**
     * @inheritDoc
     */
    public function removeUserFromGroup(string $userId, string $groupId): void
    {
        /** @var User|null $user */
75
        $user = User::find((int)$userId);
76
        /** @var Group|null $group */
77
        $group = Group::find((int)$groupId);
78

Jacob Priddy's avatar
Jacob Priddy committed
79
80
81
82
83
84
        if (!$user) {
            throw new EntityNotFoundException('User not found.');
        }

        if (!$group) {
            throw new EntityNotFoundException('Group not found.');
85
86
87
88
        }

        $user->groups()->detach($groupId);
    }
Jacob Priddy's avatar
Jacob Priddy committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    /**
     * @inheritDoc
     */
    public function getGroup(string $groupId): ?\Source\Entities\Group
    {
        if ($g = Group::find((int)$groupId)) {
            return DatabaseGroupsRepository::makeGroupFromDbGroup($g);
        }

        return null;
    }

    /**
     * @inheritDoc
     */
    public function getUser(string $userId): ?\Source\Entities\User
    {
        if ($user = User::find((int)$userId)) {
            return DatabaseUsersRepository::makeUserFromDbUser($user);
        }

        return null;
    }
113
}