1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
8
|
|
|
use Yaro\Jarboe\Exceptions\PermissionDenied; |
9
|
|
|
use Yaro\Jarboe\Table\CRUD; |
10
|
|
|
|
11
|
|
|
trait RevertHandlerTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Revert model to specific version. |
15
|
|
|
* |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param $id |
18
|
|
|
* @return \Illuminate\Http\JsonResponse |
19
|
|
|
* @throws PermissionDenied |
20
|
|
|
* @throws UnauthorizedException |
21
|
|
|
*/ |
22
|
|
|
public function handleRevert(Request $request, $id) |
23
|
|
|
{ |
24
|
|
|
$this->beforeInit(); |
25
|
|
|
$this->init(); |
26
|
|
|
$this->bound(); |
27
|
|
|
|
28
|
|
|
if (!$this->can('revert')) { |
29
|
|
|
throw UnauthorizedException::forPermissions(['revert']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$model = $this->crud()->repo()->find($id); |
33
|
|
|
if (!$this->crud()->actions()->isAllowed('history', $model)) { |
34
|
|
|
throw new PermissionDenied(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$version = $model->versions()->find($request->get('version')); |
38
|
|
|
if (is_null($version)) { |
39
|
|
|
throw new RuntimeException('Version does not exist'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$version->revert(); |
43
|
|
|
|
44
|
|
|
return response()->json([ |
|
|
|
|
45
|
|
|
'ok' => true, |
46
|
|
|
'timeline' => view('jarboe::crud.inc.history.timeline', [ |
|
|
|
|
47
|
|
|
'crud' => $this->crud(), |
48
|
|
|
'item' => $model, |
49
|
|
|
'versions' => $model->versions()->latest()->paginate(8), |
50
|
|
|
])->render(), |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
abstract protected function beforeInit(); |
|
|
|
|
55
|
|
|
abstract protected function init(); |
|
|
|
|
56
|
|
|
abstract protected function bound(); |
|
|
|
|
57
|
|
|
abstract protected function crud(): CRUD; |
58
|
|
|
abstract protected function can($action): bool; |
59
|
|
|
} |
60
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: