GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 958609...1e02fa )
by Samuel
02:51
created

Acl::__call()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 12
nop 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Samshal\Acl library
5
 *
6
 * @license MIT
7
 * @copyright Copyright (c) 2016 Samshal http://samshal.github.com
8
 */
9
namespace Samshal\Acl;
10
11
use Samshal\Acl\Role\{
12
    DefaultRole as Role,
13
    RoleInterface
14
};
15
use Samshal\Acl\Resource\{
16
    DefaultResource as Resource,
17
    ResourceInterface
18
};
19
use Samshal\Acl\Permission\{
20
    DefaultPermission as Permission,
21
    PermissionInterface
22
};
23
use Samshal\Acl\Registry\{
24
    GlobalRegistry,
25
    Registry
26
};
27
28
29
/**
30
 * Class Acl
31
 *
32
 * @package samshal.acl
33
 * @author Samuel Adeshina <[email protected]>
34
 * @since 30/05/2016
35
 */
36
class Acl implements AclInterface
37
{
38
    /**
39
     * @var Samshal\Acl\Registry\RegistryInterface $roleRegistry
40
     */
41
    public $roleRegistry;
42
43
    /**
44
     * @var Samshal\Acl\Registry\RegistryInterface $resourceRegistry
45
     */
46
    protected $resourceRegistry;
47
48
    /**
49
     * @var Samshal\Acl\Registry\RegistryInterface $permissionRegistry
50
     */
51
    protected $permissionRegistry;
52
53
    /**
54
     * @var Samshal\Acl\Registry\RegistryInterface $globalRegistry
55
     */
56
    public $globalRegistry;
57
58
    /**
59
     *  @var string[] $sesion
60
     */
61
    protected $session = [];
62
63
    /**
64
     * @var string SYN_ALLOW
65
     */
66
    const SYN_ALLOW = "can";
67
68
    /**
69
     * @var string SYN_DENY
70
     */
71
    const SYN_DENY = "cannot";
72
73
    /**
74
     * Performs bootstrapping
75
     */
76
    public function __construct()
77
    {
78
        self::initRegistries();
79
        self::initSession();
80
    }
81
82
    /**
83
     * Initalizes the registries
84
     *
85
     * @return void
86
     */
87
    protected function initRegistries()
88
    {
89
        $this->roleRegistry = new Registry();
1 ignored issue
show
Documentation Bug introduced by
It seems like new \Samshal\Acl\Registry\Registry() of type object<Samshal\Acl\Registry\Registry> is incompatible with the declared type object<Samshal\Acl\Samsh...stry\RegistryInterface> of property $roleRegistry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        $this->resourceRegistry = new Registry();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Samshal\Acl\Registry\Registry() of type object<Samshal\Acl\Registry\Registry> is incompatible with the declared type object<Samshal\Acl\Samsh...stry\RegistryInterface> of property $resourceRegistry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
        $this->permissionRegistry = new Registry();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Samshal\Acl\Registry\Registry() of type object<Samshal\Acl\Registry\Registry> is incompatible with the declared type object<Samshal\Acl\Samsh...stry\RegistryInterface> of property $permissionRegistry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
        $this->globalRegistry = new GlobalRegistry();
1 ignored issue
show
Documentation Bug introduced by
It seems like new \Samshal\Acl\Registry\GlobalRegistry() of type object<Samshal\Acl\Registry\GlobalRegistry> is incompatible with the declared type object<Samshal\Acl\Samsh...stry\RegistryInterface> of property $globalRegistry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
    }
94
95
    /**
96
     * Initializes the global session array and sets them to the default value
97
     *
98
     * @return void
99
     */
100
    protected function initSession()
101
    {
102
        $this->session["query"] = true;
103
        unset($this->session["role"], $this->session["status"]);
104
    }
105
106
    /**
107
     * Listen for and intercept properties that're not set
108
     *
109
     * @param string $role;
0 ignored issues
show
Bug introduced by
There is no parameter named $role;. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
     * @throws \Exception
111
     * @return AclInterface
112
     */
113
    public function __get(string $role) : AclInterface
114
    {
115
        if ($role === self::SYN_ALLOW || $role === self::SYN_DENY)
116
        {
117
            $this->session["status"] = ($role === self::SYN_ALLOW);
118
119
            if (!empty($this->session["role"]))
120
            {
121
                $this->session["query"] = false;
122
            }
123
124
            return $this;
125
        }
126
127
        if (!$this->roleRegistry->exists($role)) {
128
            throw new \Exception(
129
                sprintf(
130
                    "The role: %s doesnt exist",
131
                    (string)$role
132
                )
133
            );
134
        }
135
136
		$this->session["role"] = $role;
137
138
		return $this;
139
	}
140
141
	/**
142
	 * Listen for and intercept undefined methods
143
	 *
144
	 * @param string $permission
145
	 * @param string[] $args
146
	 * @throws \Exception
147
	 * @return boolean|null
148
	 */
149
	public function __call(string $permission, array $args)
150
	{
151
		if (!$this->permissionRegistry->exists($permission)) {
152
            throw new \Exception(
153
                sprintf(
154
                    "The permission: %s doesnt exist",
155
                    (string)$permission
156
                )
157
            );
158
        }
159
160
        foreach ($args as $arg) {
161
            if (!$this->resourceRegistry->exists($arg)) {
162
                throw new \Exception(
163
                    sprintf(
164
                        "The resource: %s doesnt exist",
165
                        (string)$arg
166
                    )
167
                );
168
            }
169
        }
170
171
		if ($this->session["query"])
172
		{
173
			$result = $this->getPermissionStatus(
174
                $this->session["role"],
175
                $permission,
176
                $args[0]
177
            );
178
179
			$this->initSession();
180
181
			return $result;
182
		}
183
184
        $args = (count($args) > 0) ? $args : $this->resourceRegistry->getRegistryNames();
185
        foreach ($args as $arg) {
186
            $this->allow(
187
                $this->session["role"],
188
                $permission,
189
                $arg,
190
                (boolean)$this->session["status"]
191
            );
192
        }
193
194
		$this->initSession();
195
	}
196
197
	/**
198
	 * Add a new role object to the registry
199
	 *
200
	 * @param string[] $role
201
	 * @return void
202
	 */
203
	public function addRole(string ...$role)
204
	{
205
		foreach ($role as $_role)
206
		{
207
			$this->roleRegistry->save($_role);
208
		}
209
	}
210
211
	/**
212
	 * Add a new resource object to the registry
213
	 *
214
	 * @param string[] $resource
215
	 * @return void
216
	 */
217
	public function addResource(string ...$resource)
218
	{
219
		foreach ($resource as $_resource)
220
		{
221
			$this->resourceRegistry->save($_resource);
222
		}
223
	}
224
225
	/**
226
	 * Add a new permission object to the registry
227
	 *
228
	 * @param string[] $permission
229
	 * @return void
230
	 */
231
	public function addPermission(string ...$permission)
232
	{
233
		foreach ($permission as $_permission)
234
		{
235
			$this->permissionRegistry->save($_permission);
236
		}
237
	}
238
239
	/**
240
	 * Adds objects lazily.
241
	 *
242
	 * Automatically determine the type of an object and call the appropriate
243
	 * add method on it.
244
	 *
245
	 * @param ObjectInterface[] $objects
246
	 * @throws \Exception
247
	 * @return void
248
	 */
249
	public function add(ObjectInterface ...$objects)
250
	{
251
		foreach ($objects as $object)
252
		{
253
			if ($object instanceof RoleInterface)
254
			{
255
				$this->addRole((string)$object);
256
			}
257
			else if ($object instanceof ResourceInterface)
258
			{
259
				$this->addResource((string)$object);
260
			}
261
			else if ($object instanceof PermissionInterface)
262
			{
263
				$this->addPermission((string)$object);
264
			}
265
			else {
266
	            throw new \Exception(
267
	                sprintf(
268
	                    "%s must implement one of RoleInterface, '.
269
	                    'ResourceInterface and PermissionInterface",
270
	                    $object
271
	                )
272
	            );
273
	        }	
274
		}
275
	}
276
277
	/**
278
	 * Change the status option of an assigned permission to true
279
	 *
280
	 * @param string $role;
0 ignored issues
show
Bug introduced by
There is no parameter named $role;. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
281
	 * @param string $permission
282
	 * @param string $resource
283
	 * @param boolean $status Optional
284
	 * @throws \Exception
285
	 * @return void
286
	 */
287
	public function allow(string $role, string $permission, string $resource, bool $status=null)
288
	{
289
        $status = $status ?? true;
290
		if (!$this->roleRegistry->exists($role)) {
291
            throw new \Exception(
292
                sprintf(
293
                    "The role: %s doesnt exist",
294
                    (string)$role
295
                )
296
            );
297
        }
298
299
        if (!$this->permissionRegistry->exists($permission)) {
300
            throw new \Exception(
301
                sprintf(
302
                    "The permission: %s doesnt exist",
303
                    (string)$permission
304
                )
305
            );
306
        }
307
308
        if (!$this->resourceRegistry->exists($resource)) {
309
            throw new \Exception(
310
                sprintf(
311
                    "The resource: %s doesnt exist",
312
                    (string)$resource
313
                )
314
            );
315
        }
316
317
        $this->globalRegistry->save($role, $resource, $permission, $status);
318
    }
319
320
    /**
321
     * Change the status option of an assigned permission to false
322
     *
323
     * @param string $role;
0 ignored issues
show
Bug introduced by
There is no parameter named $role;. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
324
     * @param string $permission
325
     * @param string $resource
326
     * @return void
327
     */
328
    public function deny(string $role, string $permission, string $resource)
329
    {
330
        $this->allow($role, $permission, $resource, false);
331
    }
332
333
    /**
334
     * Retrieve the status of a permission assigned to a role
335
     *
336
     * @param string $role;
0 ignored issues
show
Bug introduced by
There is no parameter named $role;. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
337
     * @param string $permission
338
     * @param string $resource
339
     * @return boolean
340
     */
341
    public function getPermissionStatus(string $role, string $permission, string $resource) : bool
342
    {
343
        if (!$this->roleRegistry->exists($role)) {
344
            throw new \Exception(
345
                sprintf(
346
                    "The role: %s doesnt exist",
347
                    (string)$role
348
                )
349
            );
350
        }
351
352
        if (!$this->permissionRegistry->exists($permission)) {
353
            throw new \Exception(
354
                sprintf(
355
                    "The permission: %s doesnt exist",
356
                    (string)$permission
357
                )
358
            );
359
        }
360
361
        if (!$this->resourceRegistry->exists($resource)) {
362
            throw new \Exception(
363
                sprintf(
364
                    "The resource: %s doesnt exist",
365
                    (string)$resource
366
                )
367
            );
368
        }
369
370
371
        $role = $this->globalRegistry->get($role);
372
373
        return $role[$resource][$permission]["status"] ?? false;
374
    }
375
}
376