UserDoorAccess.php 1.94 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

namespace Source\UseCases\DoorUser\UserDoorAccess;

use Source\Entities\Schedule;
use Source\Gateways\DoorGroup\DoorGroupRepository;
use Source\Gateways\Schedules\SchedulesRepository;
use Source\Gateways\GroupUser\GroupUserRepository;

class UserDoorAccess implements UserDoorAccessUseCase
{
    /**
     * @var \Source\Gateways\DoorGroup\DoorGroupRepository
     */
    protected DoorGroupRepository $doorGroupRepository;

    /**
     * @var \Source\Gateways\Schedules\SchedulesRepository
     */
    protected SchedulesRepository $schedulesRepository;

    /**
     * @var \Source\Gateways\GroupUser\GroupUserRepository
     */
    protected GroupUserRepository $groupUserRepository;

    public function __construct(
        GroupUserRepository $groupUserRepository,
        DoorGroupRepository $doorGroupRepository,
        SchedulesRepository $schedulesRepository
    ) {
        $this->doorGroupRepository = $doorGroupRepository;
        $this->schedulesRepository = $schedulesRepository;
        $this->groupUserRepository = $groupUserRepository;
    }

    /**
     * @inheritDoc
     */
    public function getUserDoorRelationships(string $userId, Presenter $presenter): void
    {
        $response = new ResponseModel();

        $groups = $this->groupUserRepository->getGroupsForUser($userId);

        foreach ($groups as $group) {
            $response->addGroup($group);
            $groupId = $group->getId();

            $doors = $this->doorGroupRepository->getDoorsForGroup($groupId);
            $schedules = $this->schedulesRepository->allForGroup($groupId);

            foreach ($doors as $door) {
                $response->addDoorToGroup($groupId, $door);
            }

            foreach ($schedules as $schedule) {
                if ($schedule->hasTypeOf(Schedule::TYPE_USER_ACCESS)) {
                    $response->addScheduleToGroup($groupId, $schedule);
                }
            }
        }

        $presenter->present($response);
    }
}