Passed
Push — develop ( 47e0bd...b3e3f2 )
by Nikita
06:41
created

ServerPolicy::before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Gameap\Policies;
4
5
use Gameap\Models\User;
6
use Gameap\Models\Server;
7
use Illuminate\Auth\Access\HandlesAuthorization;
8
9
class ServerPolicy
10
{
11
    use HandlesAuthorization;
12
13 6
    public function before(?User $user, $ability)
0 ignored issues
show
Unused Code introduced by
The parameter $ability 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

13
    public function before(?User $user, /** @scrutinizer ignore-unused */ $ability)

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...
14
    {
15 6
        if ($user->can('admin roles & permissions')) {
0 ignored issues
show
Bug introduced by
The method can() 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

15
        if ($user->/** @scrutinizer ignore-call */ can('admin roles & permissions')) {

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...
16
            return true;
17
        }
18
19 6
        return null;
20
    }
21
22
    /**
23
     * Determine whether the user can view the server.
24
     *
25
     * @param User $user
26
     * @param Server $server
27
     * @return mixed
28
     */
29 6
    public function control(?User $user, Server $server)
30
    {
31 6
        $exists = $user->servers()
32 6
            ->where('id', $server->id)
33 6
            ->exists();
34 6
        return $exists && $user->can('game-server-common', $server);
35
    }
36
37
    /**
38
     * @param User $user
39
     * @param Server $server
40
     * @return bool
41
     */
42
    public function start(User $user, Server $server)
43
    {
44
        return $user->can(['game-server-common', 'game-server-start'], $server);
45
    }
46
47
    /**
48
     * @param User $user
49
     * @param Server $server
50
     * @return bool
51
     */
52
    public function stop(User $user, Server $server)
53
    {
54
        return $user->can(['game-server-common', 'game-server-stop'], $server);
55
    }
56
57
    /**
58
     * @param User $user
59
     * @param Server $server
60
     * @return bool
61
     */
62
    public function restart(User $user, Server $server)
63
    {
64
        return $user->can(['game-server-common', 'game-server-restart'], $server);
65
    }
66
67
    /**
68
     * @param User $user
69
     * @param Server $server
70
     * @return bool
71
     */
72
    public function pause(User $user, Server $server)
73
    {
74
        return $user->can(['game-server-common', 'game-server-pause'], $server);
75
    }
76
77
    /**
78
     * @param User $user
79
     * @param Server $server
80
     * @return bool
81
     */
82
    public function update(User $user, Server $server)
83
    {
84
        return $user->can(['game-server-common', 'game-server-update'], $server);
85
    }
86
87
    /**
88
     * @param User $user
89
     * @param Server $server
90
     * @return bool
91
     */
92
    public function tasks(User $user, Server $server)
93
    {
94
        return $user->can(['game-server-common', 'game-server-tasks'], $server)
95
            && (
96
                $this->start($user, $server)
97
                || $this->stop($user, $server)
98
                || $this->restart($user, $server)
99
                || $this->update($user, $server)
100
            );
101
    }
102
103
    /**
104
     * @param User $user
105
     * @param Server $server
106
     * @return bool
107
     */
108
    public function consoleView(User $user, Server $server)
109
    {
110
        return $user->can(['game-server-common', 'game-server-console-view'], $server);
111
    }
112
113
    /**
114
     * @param User $user
115
     * @param Server $server
116
     * @return bool
117
     */
118
    public function consoleSend(User $user, Server $server)
119
    {
120
        return $user->can(['game-server-common', 'game-server-console-send'], $server);
121
    }
122
123
    /**
124
     * @param User $user
125
     * @param Server $server
126
     * @return bool
127
     */
128
    public function files(User $user, Server $server)
129
    {
130
        return $user->can(['game-server-common', 'game-server-files'], $server);
131
    }
132
133
    /**
134
     * @param User $user
135
     * @param Server $server
136
     * @return bool
137
     */
138
    public function settings(User $user, Server $server)
139
    {
140
        return $user->can(['game-server-common', 'game-server-settings'], $server);
141
    }
142
143
    /**
144
     * Main Rcon
145
     *
146
     * @param User $user
147
     * @param Server $server
148
     * @return bool
149
     */
150
    public function rcon(User $user, Server $server)
151
    {
152
        if (!$user->can('game-server-common', $server)) {
153
            return false;
154
        }
155
156
        return $user->can('game-server-rcon-console', $server)
157
            || $user->can('game-server-rcon-players', $server);
158
    }
159
160
    /**
161
     * @param User $user
162
     * @param Server $server
163
     * @return bool
164
     */
165
    public function rconConsole(User $user, Server $server)
166
    {
167
        return $user->can(['game-server-common', 'game-server-rcon-console'], $server);
168
    }
169
170
    /**
171
     * @param User $user
172
     * @param Server $server
173
     * @return bool
174
     */
175
    public function rconPlayers(User $user, Server $server)
176
    {
177
        if (!$user->can('game-server-common', $server)) {
178
            return false;
179
        }
180
181
        return $user->can('game-server-rcon-console')
182
            || $user->can('game-server-rcon-players', $server);
183
    }
184
}
185