|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
|
7
|
|
|
use Yaro\Jarboe\Exceptions\PermissionDenied; |
|
8
|
|
|
use Yaro\Jarboe\Table\CRUD; |
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
trait RestoreHandlerTrait |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Restore record by its id. |
|
14
|
|
|
* |
|
15
|
|
|
* @param $id |
|
16
|
|
|
* @param Request $request |
|
17
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
18
|
|
|
* @throws PermissionDenied |
|
19
|
|
|
* @throws UnauthorizedException |
|
20
|
|
|
*/ |
|
21
|
6 |
|
public function handleRestore(Request $request, $id) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$this->init(); |
|
24
|
6 |
|
$this->bound(); |
|
25
|
|
|
|
|
26
|
6 |
|
if (!$this->crud()->isSoftDeleteEnabled()) { |
|
27
|
1 |
|
throw new PermissionDenied(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
5 |
|
if (!$this->can('restore')) { |
|
31
|
2 |
|
throw UnauthorizedException::forPermissions(['restore']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
$model = $this->crud()->repo()->find($id); |
|
35
|
3 |
|
if (!$this->crud()->actions()->isAllowed('restore', $model)) { |
|
36
|
1 |
|
throw new PermissionDenied(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$this->idEntity = $model->getKey(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
2 |
|
if (!$this->crud()->repo()->restore($id) || $model->trashed()) { |
|
42
|
1 |
|
return response()->json([ |
|
|
|
|
|
|
43
|
1 |
|
'message' => __('jarboe::common.list.restore_failed_message', ['id' => $id]), |
|
44
|
1 |
|
], 422); |
|
45
|
|
|
} |
|
46
|
1 |
|
return response()->json([ |
|
47
|
1 |
|
'message' => __('jarboe::common.list.restore_success_message', ['id' => $id]), |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
abstract protected function init(); |
|
|
|
|
|
|
52
|
|
|
abstract protected function bound(); |
|
|
|
|
|
|
53
|
|
|
abstract protected function crud(): CRUD; |
|
54
|
|
|
abstract protected function can($action): bool; |
|
55
|
|
|
} |
|
56
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.