Completed
Push — master ( 20c83d...440e81 )
by Derek Stephen
01:23
created

PassportControl::findUserPassport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Del\Passport;
4
5
use Del\Passport\Passport;
6
use Del\Passport\Entity\PassportRole;
7
use Del\Passport\Entity\Role;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\EntityManager;
10
11
class PassportControl
12
{
13
    /** @var EntityManager */
14
    private $entityManager;
15
16
    /**
17
     * PassportControl constructor.
18
     * @param EntityManager $entityManager
19
     */
20
    public function __construct(EntityManager $entityManager)
21
    {
22
        $this->entityManager = $entityManager;
23
    }
24
25
    /**
26
     * @param PassportInterface $passport
27
     * @param ResourceInterface $resource
28
     * @return bool
29
     */
30
    public function isAuthorized(PassportInterface $passport, ResourceInterface $resource): bool
0 ignored issues
show
Unused Code introduced by
The parameter $passport is not used and could be removed.

This check looks from 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 $resource is not used and could be removed.

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

Loading history...
31
    {
32
        return true;
33
    }
34
35
    /**
36
     * @param PassportInterface $passport
37
     * @param RoleInterface $role
38
     * @param ResourceInterface $resource
39
     * @return bool
40
     */
41
    public function grantEntitlement(PassportInterface $passport, RoleInterface $role, ResourceInterface $resource = null): bool
42
    {
43
        $userId = $passport->getUserId();
44
        $resource ? $resourceId = $resource->getResourceId() : null;
45
        $entitlement = new PassportRole();
46
        $entitlement->setUserId($userId);
47
        $entitlement->setRole($role);
0 ignored issues
show
Compatibility introduced by
$role of type object<Del\Passport\RoleInterface> is not a sub-type of object<Del\Passport\Entity\Role>. It seems like you assume a concrete implementation of the interface Del\Passport\RoleInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
        $resourceId ? $entitlement->setEntityId($resourceId) : null;
0 ignored issues
show
Bug introduced by
The variable $resourceId does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
49
        $this->entityManager->persist($entitlement);
50
        $this->entityManager->flush($entitlement);
51
52
        return true;
53
    }
54
55
    /**
56
     * @param PassportInterface $passport
57
     * @param RoleInterface $role
58
     * @param ResourceInterface $resource
59
     * @return bool
60
     */
61
    public function revokeEntitlement(PassportInterface $passport, RoleInterface $role, ResourceInterface $resource): bool
0 ignored issues
show
Unused Code introduced by
The parameter $passport is not used and could be removed.

This check looks from 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 $role is not used and could be removed.

This check looks from 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 $resource is not used and could be removed.

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

Loading history...
62
    {
63
        return true;
64
    }
65
66
    /**
67
     * @param int $userId
0 ignored issues
show
Bug introduced by
There is no parameter named $userId. 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...
68
     * @return PassportInterface
69
     */
70
    public function createNewRole(RoleInterface $role): RoleInterface
71
    {
72
        $this->entityManager->persist($role);
73
        $this->entityManager->flush($role);
74
75
        return $role;
76
    }
77
78
    /**
79
     * @param int $userId
0 ignored issues
show
Bug introduced by
There is no parameter named $userId. 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...
80
     * @return PassportInterface
81
     */
82
    public function removeRole(RoleInterface $role): void
83
    {
84
        $this->entityManager->remove($role);
85
        $this->entityManager->flush($role);
86
    }
87
88
    /**
89
     * @param string $name
90
     * @return Role|null
91
     */
92
    public function findRole(string $name): ?Role
93
    {
94
        return $this->entityManager->getRepository(Role::class)->findOneBy([
95
            'roleName' => $name,
96
        ]);
97
    }
98
99
    /**
100
     * @param string $name
101
     * @param int $id
102
     * @return PassportRole|null
103
     */
104
    public function findPassportRole(string $name, int $id, int $entityId = null): ?PassportRole
105
    {
106
        $criteria = [
107
            'role' => $name,
108
            'userId' => $id,
109
        ];
110
111
        if ($entityId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entityId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
112
            $criteria['entityId'] = $entityId;
113
        }
114
115
        return $this->entityManager->getRepository(PassportRole::class)->findOneBy($criteria);
116
    }
117
118
    /**
119
     * @param int $id
120
     * @return Passport
121
     */
122
    public function findUserPassport(int $id): Passport
123
    {
124
        $roles = $this->entityManager->getRepository(PassportRole::class)->findBy([
125
            'userId' => $id,
126
        ]);
127
128
        return new Passport($id, $roles);
129
    }
130
}
131