Completed
Push — master ( f066c0...72d72a )
by Yaro
01:50 queued 10s
created

CreateHandlerTrait::handleCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 9
cts 11
cp 0.8182
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.054
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 CreateHandlerTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
11
{
12
    /**
13
     * Show create form page.
14
     *
15
     * @param Request $request
16
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
17
     * @throws PermissionDenied
18
     * @throws UnauthorizedException
19
     */
20 1
    public function handleCreate(Request $request)
21
    {
22 1
        $this->init();
23 1
        $this->bound();
24
25 1
        if (!$this->crud()->actions()->isAllowed('create')) {
26
            throw new PermissionDenied();
27
        }
28
29 1
        if (!$this->can('create')) {
30
            throw UnauthorizedException::forPermissions(['create']);
31
        }
32
33 1
        return view($this->viewCrudCreate, [
0 ignored issues
show
Bug introduced by
The property viewCrudCreate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 1
            'crud' => $this->crud(),
35 1
            'viewsAbove' => $this->getCreateViewsAbove(),
36 1
            'viewsBelow' => $this->getCreateViewsBelow(),
37
        ]);
38
    }
39
40
    /**
41
     * Get array of view's objects, that should be rendered above content of `create` view.
42
     *
43
     * @return array
44
     */
45 2
    protected function getCreateViewsAbove(): array
46
    {
47 2
        return [];
48
    }
49
50
    /**
51
     * Get array of view's objects, that should be rendered below content of `create` view.
52
     *
53
     * @return array
54
     */
55 2
    protected function getCreateViewsBelow(): array
56
    {
57 2
        return [];
58
    }
59
60
    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...
61
    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...
62
    abstract protected function crud(): CRUD;
63
    abstract protected function can($action): bool;
64
}
65