DatabaseDoorUserRepository.php 1.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php


namespace Source\Gateways\DoorUser;

use Source\Entities\Group;
use Source\Sanitize\CastsTo;
use Illuminate\Database\ConnectionInterface;

class DatabaseDoorUserRepository implements DoorUserRepository
{
    use CastsTo;

    /**
     * @var \Illuminate\Database\ConnectionInterface
     */
    protected ConnectionInterface $db;

    public function __construct(ConnectionInterface $db)
    {
        $this->db = $db;
    }

    /**
     * @inheritDoc
     */
    public function getDoorUserGroupIntersection(string $doorId, string $userId): array
    {
        $query = <<<QUERY
SELECT A.id, A.title, A.description, A.created_at, A.updated_at
FROM groups AS A
INNER JOIN door_group AS B ON B.group_id = A.id AND B.door_id = :door_id
INNER JOIN group_user AS C ON C.group_id = A.id AND C.user_id = :user_id
QUERY;

        $commonGroups = $this->db->select($query, [
            $doorId,
            $userId
        ]);

        return array_map(function ($group) {
            return new Group(
                $group->id,
                $group->title,
                $group->description,
46
47
                $this->castToCarbon($group->created_at),
                $this->castToCarbon($group->updated_at)
48
49
50
51
            );
        }, $commonGroups);
    }
}