Completed
Branch master (49dc1a)
by Yaro
01:42
created

HistoryHandlerTrait::init()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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
trait HistoryHandlerTrait
11
{
12
    protected $historyCrudHistory = 'jarboe::crud.history';
13
14
    /**
15
     * Show history page.
16
     *
17
     * @param Request $request
18
     * @param $id
19
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
20
     * @throws PermissionDenied
21
     * @throws UnauthorizedException
22
     */
23 4
    public function handleHistory(Request $request, $id)
24
    {
25 4
        $this->beforeInit();
26 4
        $this->init();
27 4
        $this->bound();
28
29 4
        if (!$this->can('history')) {
30 2
            throw UnauthorizedException::forPermissions(['history']);
31
        }
32
33 2
        $model = $this->crud()->repo()->find($id);
34 2
        if (!$this->crud()->actions()->isAllowed('history', $model)) {
35 1
            throw new PermissionDenied();
36
        }
37
38 1
        return view($this->historyCrudHistory, [
39 1
            'crud' => $this->crud(),
40 1
            'item' => $model,
41 1
            'versions' => $model->versions()->latest()->paginate(8),
42
        ]);
43
    }
44
45
    abstract protected function beforeInit();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
46
    abstract protected function init();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
47
    abstract protected function bound();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
48
    abstract protected function crud(): CRUD;
49
    abstract protected function can($action): bool;
50
}
51