ScheduleAuthorizer.php 2.56 KB
Newer Older
1
2
3
4
5
6
<?php


namespace Source\UseCases\Door\Access\Authorizers;

use Carbon\Carbon;
7
use Source\Entities\Door;
8
9
use Source\Entities\User;
use InvalidArgumentException;
10
11
use Source\Entities\Schedule;
use Source\Gateways\DoorUser\DoorUserRepository;
12
use Source\UseCases\Door\Access\AccessAuthorizer;
Jacob Priddy's avatar
Jacob Priddy committed
13
use Source\Gateways\GroupSchedule\GroupScheduleRepository;
14
15
16
17
18
19
20
21
22
23
use Source\Gateways\RecurrenceSet\RecurrenceSetRepository;

class ScheduleAuthorizer implements AccessAuthorizer
{
    /**
     * @var \Source\Gateways\RecurrenceSet\RecurrenceSetRepository
     */
    protected RecurrenceSetRepository $recurrenceSet;

    /**
24
     * @var \Source\Gateways\DoorUser\DoorUserRepository
25
     */
26
    protected DoorUserRepository $doorUsers;
27

Jacob Priddy's avatar
Jacob Priddy committed
28
29
30
31
32
    /**
     * @var \Source\Gateways\GroupSchedule\GroupScheduleRepository
     */
    protected GroupScheduleRepository $groupSchedule;

Jacob Priddy's avatar
Jacob Priddy committed
33
34
    protected int $allowedSchedule = 0;

35
    public function __construct(
Jacob Priddy's avatar
Jacob Priddy committed
36
        GroupScheduleRepository $groupSchedule,
37
        RecurrenceSetRepository $recurrenceSet,
38
        DoorUserRepository $doorUsers
39
40
    ) {
        $this->recurrenceSet = $recurrenceSet;
41
        $this->doorUsers = $doorUsers;
Jacob Priddy's avatar
Jacob Priddy committed
42
        $this->groupSchedule = $groupSchedule;
43
44
45
46
47
    }

    /**
     * @inheritDoc
     */
48
    public function check(?User $user, Carbon $date, Door $door, string $doorcode, ?string $commandString): int
49
50
    {
        if ($user) {
51
            $groups = $this->doorUsers->getDoorUserGroupIntersection($door->getId(), $user->getId());
52
            foreach ($groups as $group) {
Jacob Priddy's avatar
Jacob Priddy committed
53
                $schedules = $this->groupSchedule->getActiveForGroupAndDate(
54
55
56
57
58
59
60
61
62
63
64
65
66
67
                    $group->getId(),
                    $date,
                    Schedule::TYPE_USER_ACCESS
                );

                foreach ($schedules as $schedule) {
                    try {
                        $this->recurrenceSet->parse($schedule->getRset());
                    } catch (InvalidArgumentException $e) {
                        // Invalid recurrence set string, ignore it.
                        continue;
                    }

                    if ($this->recurrenceSet->occurrenceHappeningAtDate($date, $schedule->getDuration())) {
Jacob Priddy's avatar
Jacob Priddy committed
68
                        $this->allowedSchedule = $schedule->getId();
69
70
71
72
73
74
75
76
                        return self::ALLOW;
                    }
                }
            }
        }

        return self::CONTINUE;
    }
Jacob Priddy's avatar
Jacob Priddy committed
77
78
79
80
81
82
83
84
85

    /**
     * @inheritDoc
     */
    public function getReason(): string
    {
        // We only allow, can only be one reason.
        return 'Allowed by user access schedule: ' . $this->allowedSchedule;
    }
86
}