DoorsController.php 10.1 KB
Newer Older
1
2
3
4
5
<?php


namespace App\Http\Controllers\Web\Admin;

6
use Illuminate\Contracts\View\View;
7
use App\Http\Controllers\Controller;
8
use Illuminate\Http\RedirectResponse;
9
10
use Source\Exceptions\EntityExistsException;
use Illuminate\Validation\ValidationException;
11
12
use Source\UseCases\Doors\GetDoor\GetDoorUseCase;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
13
use Source\UseCases\Groups\GetGroups\GetGroupsUseCase;
14
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCase;
15
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
16
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase;
17
use Source\UseCases\DoorUser\UserDoorAccess\UsersWebPresenter;
18
use Source\UseCases\Doors\CreateDoor\WebTokenGeneratePresenter;
Jacob Priddy's avatar
Jacob Priddy committed
19
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase;
20
21
use Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
22
23
use Source\UseCases\Doors\GetDoor\APIPresenter as GetDoorPresenter;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
24
use Source\UseCases\Doors\GetDoors\WebPresenter as GetDoorsPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
25
use Source\UseCases\Groups\GetGroups\WebPresenter as GroupsPresenter;
26
use Source\UseCases\Doors\CreateDoor\WebPresenter as CreateDoorPresenter;
27
use Source\UseCases\Doors\DeleteDoor\APIPresenter as DeleteDoorPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
28
use Source\UseCases\Doors\UpdateDoor\APIPresenter as UpdateDoorPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
29
30
31
32
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase;
use Source\UseCases\DoorGroup\AddDoorToGroup\APIPresenter as DoorGroupPresenter;
use Source\UseCases\DoorGroup\GetDoorGroups\WebPresenter as DoorGroupsPresenter;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\APIPresenter as RemoveDoorGroupPresenter;
33
34
35

class DoorsController extends Controller
{
36
37
38
39
40
41
42
43
44
45
46
47
48
    /**
     * Since a checkbox input sends "on" when selected and null when not selected.
     * So here if it is set to "on" we turn it to false, and if it is not present, we set it to false.
     */
    protected function processPublicField(): void
    {
        if (!$this->request->has('public')) {
            $this->request['public'] = false;
        } else {
            $this->request['public'] = true;
        }
    }

49
50
    /**
     * @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase $doors
51
     * @return \Illuminate\Contracts\View\View
52
53
     * @throws \Illuminate\Validation\ValidationException
     */
54
55
    public function index(GetDoorsUseCase $doors): View
    {
56
57
58
59
60
61
        $this->validate($this->request, [
            'query' => 'nullable|string',
            'awol_seconds' => 'nullable|int',
            'active_seconds' => 'nullable|int',
        ]);

62
        $presenter = new GetDoorsPresenter();
63
        $doors->query($this->request->all(), false, $presenter);
64
65
66
67

        return view('admin.doors', $presenter->getViewModel($this->request->all()));
    }

68
69
70
    /**
     * @param string                                        $doorId
     * @param \Source\UseCases\Doors\GetDoor\GetDoorUseCase $getDoor
71
     * @return \Illuminate\Contracts\View\View
72
73
74
75
76
77
78
79
80
81
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function edit(string $doorId, GetDoorUseCase $getDoor): View
    {
        $presenter = new GetDoorPresenter();

        $getDoor->get($doorId, $presenter);

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

82
        $this->request->session()->flashInput($door);
83
84
85
86
87
88
89
90
91
92
93
94
95

        return view('admin.entities.door', ['id' => $door['id']]);
    }

    /**
     * @param string                                              $doorId
     * @param \Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase $updateDoor
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function update(string $doorId, UpdateDoorUseCase $updateDoor): RedirectResponse
    {
96
97
        $this->processPublicField();

98
99
100
        $this->validate($this->request, [
            'location' => 'required|string|max:255',
            'name' => 'required|string|max:255',
101
102
            'public' => 'required|boolean',
            'notes' => 'nullable|string|max:255',
103
104
105
        ]);

        $presenter = new UpdateDoorPresenter();
106
107
108
109
110
        try {
            $updateDoor->update($doorId, $this->request->all(), $presenter);
        } catch (EntityExistsException $e) {
            throw ValidationException::withMessages(['errors' => $e->getMessage()]);
        }
111
112
113
114
115
116
117
118
119

        return redirect(route('web.admin.doors.index'))->with($presenter->getViewModel());
    }

    /**
     * @param \Source\UseCases\Doors\CreateDoor\CreateDoorUseCase $createDoor
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
120
    public function store(CreateDoorUseCase $createDoor): RedirectResponse
121
    {
122
123
        $this->processPublicField();

124
125
126
        $this->validate($this->request, [
            'location' => 'required|string|max:255',
            'name' => 'required|string|max:255',
127
128
            'public' => 'required|boolean',
            'notes' => 'nullable|string|max:255',
129
130
131
132
        ]);

        $presenter = new CreateDoorPresenter();

133
134
135
136
137
        try {
            $createDoor->create($this->request->all(), $presenter);
        } catch (EntityExistsException $e) {
            throw ValidationException::withMessages(['errors' => $e->getMessage()]);
        }
138
139

        return redirect(route('web.admin.doors.index'))->with($presenter->getViewModel());
140
141
142
143
144
145
146
147
    }

    /**
     * @param string                                              $doorId
     * @param \Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase $deleteDoor
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Source\Exceptions\EntityNotFoundException
     */
148
    public function destroy(string $doorId, DeleteDoorUseCase $deleteDoor): RedirectResponse
149
150
151
152
153
154
155
156
157
158
159
160
161
    {
        $presenter = new DeleteDoorPresenter();

        $deleteDoor->delete($doorId, $presenter);

        return redirect()->back()->with($presenter->getViewModel());
    }

    /**
     * @param string                                                            $doorId
     * @param \Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase $tokenGenerator
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Source\Exceptions\EntityNotFoundException
162
     * @throws \Illuminate\Validation\ValidationException
163
164
165
166
167
     */
    public function regenerateToken(string $doorId, GenerateDoorTokenUseCase $tokenGenerator): RedirectResponse
    {
        $presenter = new WebTokenGeneratePresenter();

168
169
170
171
172
        try {
            $tokenGenerator->generate($doorId, $presenter);
        } catch (EntityExistsException $e) {
            throw ValidationException::withMessages(['error' => 'Please re-perform this action. ' . $e->getMessage()]);
        }
173
174

        return redirect()->back()->with($presenter->getViewModel());
175
    }
176
177
178
179
180
181
182
183
184

    /**
     * @param string $doorId
     * @return \Illuminate\View\View
     */
    public function schedule(string $doorId): View
    {
        return view('admin.entities.doorAccess', ['id' => $doorId]);
    }
Jacob Priddy's avatar
Jacob Priddy committed
185
186
187
188
189

    /**
     * @param string                                                        $doorId
     * @param \Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase $doorGroups
     * @param \Source\UseCases\Groups\GetGroups\GetGroupsUseCase            $groups
190
     * @return \Illuminate\Contracts\View\View
Jacob Priddy's avatar
Jacob Priddy committed
191
192
193
194
195
196
197
198
199
200
201
202
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function groups(string $doorId, GetDoorGroupsUseCase $doorGroups, GetGroupsUseCase $groups): View
    {
        $groupsPresenter = new GroupsPresenter();

        $groups->search(null, $groupsPresenter);

        $presenter = new DoorGroupsPresenter();

        $doorGroups->getGroupsForDoor($doorId, $presenter);

Jacob Priddy's avatar
Jacob Priddy committed
203
        return view('layouts.admin.groups', array_merge(
Jacob Priddy's avatar
Jacob Priddy committed
204
205
            $presenter->getViewModel(),
            $groupsPresenter->all('allGroups'),
Jacob Priddy's avatar
Jacob Priddy committed
206
207
208
209
210
            [
                'resourceId' => $doorId,
                'control' => 'detachDoorGroup',
                'resource' => 'door',
            ],
Jacob Priddy's avatar
Jacob Priddy committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        ));
    }

    /**
     * @param string                                                          $doorId
     * @param \Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase $doorGroup
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function addGroup(string $doorId, AddDoorToGroupUseCase $doorGroup): RedirectResponse
    {
        $this->validate($this->request, [
            'group_id' => 'required|string',
        ]);

        $presenter = new DoorGroupPresenter();

        $doorGroup->addDoorToGroup([$doorId], [$this->request->input('group_id')], $presenter);

Jacob Priddy's avatar
Jacob Priddy committed
230
        return redirect()->back()->with($presenter->getViewModel());
Jacob Priddy's avatar
Jacob Priddy committed
231
232
    }

233
234
235
236
    /**
     * @param string                                                                    $doorId
     * @param \Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase $doorGroup
     * @return \Illuminate\Http\RedirectResponse
237
     * @throws \Illuminate\Validation\ValidationException
238
     */
239
    public function removeGroup(string $doorId, RemoveDoorFromGroupUseCase $doorGroup): RedirectResponse
Jacob Priddy's avatar
Jacob Priddy committed
240
    {
241
242
243
244
        $this->validate($this->request, [
            'group_id' => 'required|string',
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
245
246
        $presenter = new RemoveDoorGroupPresenter();

247
        $doorGroup->removeDoorFromGroup([$doorId], [$this->request->input('group_id')], $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
248

Jacob Priddy's avatar
Jacob Priddy committed
249
        return redirect()->back()->with($presenter->getViewModel());
Jacob Priddy's avatar
Jacob Priddy committed
250
    }
251
252
253
254

    /**
     * @param string                                                         $doorId
     * @param \Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase $doorUsers
255
     * @return \Illuminate\Contracts\View\View
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function users(string $doorId, UserDoorAccessUseCase $doorUsers): View
    {
        $presenter = new UsersWebPresenter();

        $doorUsers->getDoorUserRelationships($doorId, $presenter);

        return view('layouts.admin.tableData', array_merge(
            $presenter->getViewModel(),
            [
                'route' => route('web.admin.doors.users', ['doorId' => $doorId]),
            ]
        ));
    }
271
}