DoorController.php 1.81 KB
Newer Older
1
2
3
4
<?php

namespace App\Http\Controllers;

5
use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
6
use App\Guards\DoorGuard;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\JsonResponse;
9
use Source\Authorization\Authorizer;
10
use Source\UseCases\Door\Access\AccessUseCase;
11
12
use Source\UseCases\Door\StatusResponse\JsonPresenter;
use Source\UseCases\Door\StatusResponse\StatusResponseUseCase;
13
14
15

class DoorController extends ApiController
{
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
    /**
     * @var \App\Guards\DoorGuard
     */
    protected DoorGuard $doorGuard;

    /**
     * @var \Source\UseCases\Door\StatusResponse\StatusResponseUseCase
     */
    protected StatusResponseUseCase $response;

    public function __construct(Request $request, Authorizer $authorizer, DoorGuard $doorGuard, StatusResponseUseCase $response)
    {
        parent::__construct($request, $authorizer);
        $this->doorGuard = $doorGuard;
        $this->response = $response;
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondStatus(): JsonResponse
    {
        $presenter = new JsonPresenter();

        $this->response->getStatusForDoor(
            $this->doorGuard->id(),
            Carbon::now(),
            Carbon::now()->addMinutes(config('app.status_foresight')),
            $presenter
        );

        return $this->respondWithData($presenter->getViewModel());
    }

50
51
    /**
     * @param string                                     $doorcode
Jacob Priddy's avatar
Jacob Priddy committed
52
     * @param \Source\UseCases\Door\Access\AccessUseCase $access
53
54
55
56
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
57
    public function access(string $doorcode, AccessUseCase $access): JsonResponse
58
    {
59
        $access->protectUserDoorAccess($this->doorGuard->id(), $doorcode);
60

61
        return $this->respondStatus();
62
63
    }
}