MeController.php 7.92 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;
16
use Source\UseCases\Users\UpdateUser\DoorcodeWebPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
17
use Source\UseCases\Requests\GetRequests\GetRequestsUseCase;
18
use Source\UseCases\Requests\CreateRequest\CreateRequestUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
19
use Source\UseCases\Requests\DeleteRequest\DeleteRequestUseCase;
20
use Source\UseCases\Tokens\GetTokens\WebPresenter as GetTokensWebPresenter;
21
use Source\UseCases\Requests\InteractWithRequest\InteractWithRequestUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
22
use Source\UseCases\Users\UpdateUser\WebPresenter as UpdateUserWebPresenter;
23
24
use Source\UseCases\Requests\GetRequests\WebPresenter as RequestsWebPresenter;
use Source\UseCases\Requests\DeleteRequest\WebPresenter as DeleteRequestWebPresenter;
25
use Source\UseCases\Users\UpdateUser\DoorcodeWebPresenter as UpdateUserDoorcodeWebPresenter;
26
27
28

class MeController extends Controller
{
29
30
    use CastsTo;

Jacob Priddy's avatar
Jacob Priddy committed
31
32
    /**
     * @param \Source\UseCases\Users\UpdateUser\UpdateCurrentUser $userUpdate
33
     * @return \Illuminate\Contracts\View\View
Jacob Priddy's avatar
Jacob Priddy committed
34
35
36
37
38
39
40
41
     * @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',
        ]);
42

43
        $presenter = new DoorcodeWebPresenter();
44

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

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

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    /**
     * @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());
    }

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

        $doorsPresenter = new MePresenter();

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

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

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

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

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

    /**
     * @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',
        ]);

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

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

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

    /**
     * @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());
    }
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
222
223

    /**
     * @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.');
    }
224
}