MeController.php 7.77 KB
Newer Older
1
2
3
4
<?php

namespace App\Http\Controllers\Web;

5
use Source\Sanitize\CastsTo;
6
use Illuminate\Contracts\View\View;
7
use App\Http\Controllers\Controller;
8
use Illuminate\Http\RedirectResponse;
9
10
use Source\UseCases\Requests\WebPresenter;
use Illuminate\Validation\ValidationException;
11
12
use Source\UseCases\Doors\GetDoors\MePresenter;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
13
use Source\UseCases\Tokens\GetTokens\GetTokensUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
14
use Source\UseCases\Users\UpdateUser\UpdateCurrentUser;
15
use Source\UseCases\Requests\GetRequest\GetRequestUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
16
use Source\UseCases\Requests\GetRequests\GetRequestsUseCase;
17
use Source\UseCases\Requests\CreateRequest\CreateRequestUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
18
use Source\UseCases\Requests\DeleteRequest\DeleteRequestUseCase;
19
use Source\UseCases\Tokens\GetTokens\WebPresenter as GetTokensWebPresenter;
20
use Source\UseCases\Requests\InteractWithRequest\InteractWithRequestUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
21
use Source\UseCases\Users\UpdateUser\WebPresenter as UpdateUserWebPresenter;
22
23
use Source\UseCases\Requests\GetRequests\WebPresenter as RequestsWebPresenter;
use Source\UseCases\Requests\DeleteRequest\WebPresenter as DeleteRequestWebPresenter;
24
25
26

class MeController extends Controller
{
27
28
    use CastsTo;

Jacob Priddy's avatar
Jacob Priddy committed
29
30
    /**
     * @param \Source\UseCases\Users\UpdateUser\UpdateCurrentUser $userUpdate
31
     * @return \Illuminate\Contracts\View\View
Jacob Priddy's avatar
Jacob Priddy committed
32
33
34
35
36
37
38
39
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function doorcodeUpdate(UpdateCurrentUser $userUpdate): View
    {
        $this->validate($this->request, [
            'doorcode' => 'required|string|numeric|digits_between:4,248',
        ]);
40

Jacob Priddy's avatar
Jacob Priddy committed
41
        $presenter = new UpdateUserWebPresenter();
42

Jacob Priddy's avatar
Jacob Priddy committed
43
44
45
46
47
48
49
50
51
        $userUpdate->update(
            $this->authorizer->getCurrentUserId(),
            ['doorcode' => $this->request->input('doorcode')],
            $presenter
        );

        return view('doorcode', $presenter->getViewModel());
    }

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    /**
     * @param \Source\UseCases\Users\UpdateUser\UpdateCurrentUser $userUpdate
     * @return \Illuminate\View\View
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function passwordUpdate(UpdateCurrentUser $userUpdate): View
    {
        $this->validate($this->request, [
            'password' => 'required|string|min:20|max:255',
        ]);

        $presenter = new UpdateUserWebPresenter();

        $userUpdate->update(
            $this->authorizer->getCurrentUserId(),
            ['password' => $this->request->input('password')],
            $presenter
        );

        return view('password', $presenter->getViewModel());
    }

75
    /**
76
     * @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase $doors
77
     * @return \Illuminate\View\View
78
79
80
81
82
83
84
85
86
87
     * @throws \Illuminate\Validation\ValidationException
     */
    public function access(GetDoorsUseCase $doors): View
    {
        $this->validate($this->request, [
            'door_id' => 'integer',
        ]);

        $doorsPresenter = new MePresenter();

88
        $doors->query([], true, $doorsPresenter);
89
90
91
92
93
94

        return view('access')
            ->with('doorId', $this->request->input('door_id'))
            ->with('doors', $doorsPresenter->getViewModel());
    }

Jacob Priddy's avatar
Jacob Priddy committed
95
96
97
98
99
100
101
102
    /**
     * @param \Source\UseCases\Tokens\GetTokens\GetTokensUseCase $userTokens
     * @return \Illuminate\View\View
     * @throws \Exception
     */
    public function tokens(GetTokensUseCase $userTokens): View
    {
        $presenter = new GetTokensWebPresenter();
103

Jacob Priddy's avatar
Jacob Priddy committed
104
        $userTokens->filter($this->authorizer->getCurrentUserId(), null, $presenter);
105

106
        return view('tokens', $presenter->getViewModel($this->request->all()));
107
    }
Jacob Priddy's avatar
Jacob Priddy committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    /**
     * @param \Source\UseCases\Requests\GetRequests\GetRequestsUseCase $requestsUseCase
     * @return \Illuminate\View\View
     * @throws \Illuminate\Validation\ValidationException
     */
    public function requests(GetRequestsUseCase $requestsUseCase): View
    {
        $this->validate($this->request, [
            'query' => 'string|nullable|max:100',
            'resolved' => 'boolean|nullable',
            'replied' => 'boolean|nullable',
        ]);

122
        $presenter = new RequestsWebPresenter();
Jacob Priddy's avatar
Jacob Priddy committed
123
124
125
126
127
128
129
130

        $requestsUseCase->filter(
            array_merge($this->request->all(), ['user_id' => $this->authorizer->getCurrentUserId()]),
            $presenter
        );

        return view('requests', $presenter->getRequests($this->request->all()));
    }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

    /**
     * @param string                                                       $requestId
     * @param \Source\UseCases\Requests\DeleteRequest\DeleteRequestUseCase $deleteRequests
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function requestsDelete(string $requestId, DeleteRequestUseCase $deleteRequests): RedirectResponse
    {
        $presenter = new DeleteRequestWebPresenter();

        $deleteRequests->delete($requestId, $presenter);

        return redirect()->back()->with($presenter->getViewModel());
    }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    /**
     * @param \Source\UseCases\Requests\CreateRequest\CreateRequestUseCase $createRequest
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function requestsCreate(CreateRequestUseCase $createRequest): RedirectResponse
    {
        $this->validate($this->request, [
            'title' => 'required|string|max:255',
            'request' => 'required|string|max:10000',
        ]);

        $presenter = new WebPresenter();

        $createRequest->create(
            array_merge($this->request->all(), ['user_id' => $this->authorizer->getCurrentUserId()]),
            $presenter
        );

        if ($presenter->hasError()) {
            throw ValidationException::withMessages(['error' => $presenter->getError()]);
        }

        return redirect(route('web.requests'))->with('message', 'Successfully created the access request.');
    }

    /**
     * @param string                                                 $requestId
     * @param \Source\UseCases\Requests\GetRequest\GetRequestUseCase $requestGet
     * @return \Illuminate\View\View
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function requestsUpdateView(string $requestId, GetRequestUseCase $requestGet): View
    {
        $presenter = new WebPresenter();

        $requestGet->get($requestId, $presenter);

        $request = $presenter->getViewModel()['request'];

        $this->request->session()->flashInput([
            'title' => $request['title'],
            'request' => $request['request'],
        ]);

        return view('request', ['id' => $request['id']]);
    }

    /**
     * @param string                                                                   $requestId
     * @param \Source\UseCases\Requests\InteractWithRequest\InteractWithRequestUseCase $interactor
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function requestsUpdate(string $requestId, InteractWithRequestUseCase $interactor): RedirectResponse
    {
        $this->validate($this->request, [
            'title' => 'required|string|max:255',
            'request' => 'required|string|max:10000',
        ]);

        $presenter = new WebPresenter();

        $interactor->update(
            $requestId,
            array_merge($this->request->all(), ['user_id' => $this->authorizer->getCurrentUserId()]),
            $presenter
        );

        return redirect(route('web.requests'))->with('message', 'Successfully updated the request.');
    }
222
}