Passed
Push — develop ( c02995...7d7bf0 )
by Septianata
18:18
created

ItemPolicy::viewAny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Policies;
4
5
use App\Models\Item;
6
use App\Models\Role;
7
use App\Models\User;
8
use Illuminate\Auth\Access\HandlesAuthorization;
9
10
class ItemPolicy
11
{
12
    use HandlesAuthorization;
13
14
    /**
15
     * Perform pre-authorization checks.
16
     *
17
     * @param  \App\Models\User|null  $user
18
     * @param  string  $ability
19
     * @return void|bool
20
     */
21
    public function before(?User $user, $ability)
22
    {
23
        if (in_array($ability, ['create', 'update', 'delete'])) {
24
            return;
25
        }
26
27
        if ($user && $user->hasRole([Role::ROLE_ADMIN, ROLE::ROLE_STAFF])) {
28
            return true;
29
        }
30
    }
31
32
    /**
33
     * Determine whether the user can view any models.
34
     *
35
     * @param  \App\Models\User|null  $user
36
     * @return \Illuminate\Auth\Access\Response|bool
37
     */
38
    public function viewAny(?User $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function viewAny(/** @scrutinizer ignore-unused */ ?User $user)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        return false;
41
    }
42
43
    /**
44
     * Determine whether the user can view the model.
45
     *
46
     * @param  \App\Models\User|null  $user
47
     * @param  \App\Models\Item  $item
48
     * @return \Illuminate\Auth\Access\Response|bool
49
     */
50
    public function view(?User $user, Item $item)
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public function view(?User $user, /** @scrutinizer ignore-unused */ Item $item)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public function view(/** @scrutinizer ignore-unused */ ?User $user, Item $item)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        return true;
53
    }
54
55
    /**
56
     * Determine whether the user can create models.
57
     *
58
     * @param  \App\Models\User|null  $user
59
     * @return \Illuminate\Auth\Access\Response|bool
60
     */
61
    public function create(User $user)
62
    {
63
        $order = request()->route('order');
64
65
        return
66
            $user && $user->hasRole(Role::ROLE_ADMIN) &&
67
            !is_null($order) && !$order->latestStatus->hasBeenFinished() && !$order->latestStatus->hasBeenCanceled();
0 ignored issues
show
Bug introduced by
The method hasBeenFinished() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            !is_null($order) && !$order->latestStatus->/** @scrutinizer ignore-call */ hasBeenFinished() && !$order->latestStatus->hasBeenCanceled();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
    }
69
70
    /**
71
     * Determine whether the user can update the model.
72
     *
73
     * @param  \App\Models\User|null  $user
74
     * @param  \App\Models\Item  $item
75
     * @return \Illuminate\Auth\Access\Response|bool
76
     */
77
    public function update(?User $user, Item $item)
78
    {
79
        return
80
            $user && $user->hasRole(Role::ROLE_ADMIN) &&
81
            !$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled();
0 ignored issues
show
Bug introduced by
The method hasBeenFinished() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            !$item->order->latestStatus->/** @scrutinizer ignore-call */ hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
    }
83
84
    /**
85
     * Determine whether the user can delete the model.
86
     *
87
     * @param  \App\Models\User|null  $user
88
     * @param  \App\Models\Item  $item
89
     * @return \Illuminate\Auth\Access\Response|bool
90
     */
91
    public function delete(?User $user, Item $item)
92
    {
93
        return
94
            $user && $user->hasRole(Role::ROLE_ADMIN) &&
95
            !$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled();
96
    }
97
98
    /**
99
     * Determine whether the user can restore the model.
100
     *
101
     * @param  \App\Models\User|null  $user
102
     * @param  \App\Models\Item  $item
103
     * @return \Illuminate\Auth\Access\Response|bool
104
     */
105
    public function restore(?User $user, Item $item)
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    public function restore(?User $user, /** @scrutinizer ignore-unused */ Item $item)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        return $user && $user->hasRole(Role::ROLE_ADMIN);
108
    }
109
110
    /**
111
     * Determine whether the user can permanently delete the model.
112
     *
113
     * @param  \App\Models\User|null  $user
114
     * @param  \App\Models\Item  $item
115
     * @return \Illuminate\Auth\Access\Response|bool
116
     */
117
    public function forceDelete(?User $user, Item $item)
118
    {
119
        return
120
            $user && $user->hasRole(Role::ROLE_ADMIN) &&
121
            !$item->order->latestStatus->hasBeenFinished() && !$item->order->latestStatus->hasBeenCanceled();
122
    }
123
}
124