Authenticated   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
eloc 11
c 3
b 0
f 1
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A can() 0 4 1
A is() 0 4 1
A unauthorized() 0 7 2
A validModel() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author enea dhack <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Enea\Authorization\Support;
13
14
use Enea\Authorization\Contracts\Owner;
15
use Enea\Authorization\Events\UnauthorizedOwner;
16
use Enea\Authorization\Exceptions\InvalidModelException;
17
use Enea\Authorization\Exceptions\UnauthorizedOwnerException;
18
use Enea\Authorization\Facades\Authorizer;
19
use Enea\Authorization\Facades\Helper;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Enea\Authorization\Support\Helper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
21
class Authenticated
22
{
23 8
    public function can(string ...$permissions): void
24
    {
25 8
        $this->validModel();
26 5
        $this->unauthorized(Authorizer::canAny(Helper::authenticated(), $permissions), $permissions);
0 ignored issues
show
Bug introduced by
It seems like Enea\Authorization\Facades\Helper::authenticated() can also be of type Illuminate\Database\Eloquent\Model; however, parameter $| of Enea\Authorization\Facades\Authorizer::canAny() does only seem to accept Enea\Authorization\Contracts\PermissionsOwner, maybe add an additional type check? ( Ignorable by Annotation )

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

26
        $this->unauthorized(Authorizer::canAny(/** @scrutinizer ignore-type */ Helper::authenticated(), $permissions), $permissions);
Loading history...
27 2
    }
28
29 6
    public function is(string ...$roles): void
30
    {
31 6
        $this->validModel();
32 5
        $this->unauthorized(Authorizer::isAny(Helper::authenticated(), $roles), $roles);
0 ignored issues
show
Bug introduced by
It seems like Enea\Authorization\Facades\Helper::authenticated() can also be of type Illuminate\Database\Eloquent\Model; however, parameter $| of Enea\Authorization\Facades\Authorizer::isAny() does only seem to accept Enea\Authorization\Contracts\RolesOwner, maybe add an additional type check? ( Ignorable by Annotation )

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

32
        $this->unauthorized(Authorizer::isAny(/** @scrutinizer ignore-type */ Helper::authenticated(), $roles), $roles);
Loading history...
33 2
    }
34
35 14
    private function validModel(): void
36
    {
37 14
        if (! Helper::authenticated() instanceof Owner) {
38 4
            throw new InvalidModelException();
39
        }
40 10
    }
41
42 10
    private function unauthorized(bool $passed, array $authorizations): void
43
    {
44 10
        if (! $passed) {
45 6
            $authenticated = Helper::authenticated();
46 6
            event(new UnauthorizedOwner($authenticated, $authorizations));
47
48 6
            throw new UnauthorizedOwnerException($authenticated);
49
        }
50 4
    }
51
}
52