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 ( 7bf065...13ae28 )
by Axel
02:07
created

PolicyRuleManager::getRule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 9
nc 3
nop 3
crap 3.0175
1
<?php
2
3
namespace PhpAbac\Manager;
4
5
use PhpAbac\Model\PolicyRule;
6
use PhpAbac\Model\PolicyRuleAttribute;
7
8
class PolicyRuleManager
9
{
10
    /** @var \PhpAbac\Manager\AttributeManager **/
11
    private $attributeManager;
12
    /** @var array **/
13
    private $rules;
14
15
    /**
16
     * @param \PhpAbac\Manager\AttributeManager $attributeManager
17
     * @param array $rules
18
     */
19 2
    public function __construct(AttributeManager $attributeManager, $rules)
20
    {
21 2
        $this->attributeManager = $attributeManager;
22 2
        $this->rules = $rules;
23 2
    }
24
25
    /**
26
     * @param string $ruleName
27
     * @param object $user
28
     * @param object $resource
29
     * @return PolicyRule
30
     * @throws \InvalidArgumentException
31
     */
32 2
    public function getRule($ruleName, $user, $resource)
33
    {
34 2
        if(!isset($this->rules[$ruleName])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
35
            throw new \InvalidArgumentException('The given rule "' . $ruleName . '" is not configured');
36
        }
37
        $rule =
38 2
            (new PolicyRule())
39 2
            ->setName($ruleName)
40
        ;
41
        // For each policy rule attribute, the data is formatted
42 2
        foreach($this->processRuleAttributes($this->rules[$ruleName]['attributes'], $user, $resource) as $pra) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
43 2
            $rule->addPolicyRuleAttribute($pra);
44
        }
45 2
        return $rule;
46
    }
47
48
    /**
49
     * This method is meant to convert attribute data from array to formatted policy rule attribute
50
     *
51
     * @param array $attributes
52
     * @param object $user
53
     * @param object $resource
54
     */
55 2
    public function processRuleAttributes($attributes, $user, $resource) {
56 2
        foreach($attributes as $attributeName => $attribute) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
57 2
            $pra = (new PolicyRuleAttribute())
58 2
                ->setAttribute($this->attributeManager->getAttribute($attributeName))
59 2
                ->setComparison($attribute['comparison'])
60 2
                ->setComparisonType($attribute['comparison_type'])
61 2
                ->setValue((isset($attribute['value'])) ? $attribute['value'] : null)
62
            ;
63 2
            $this->processRuleAttributeComparisonType($pra, $user, $resource);
64
            // In the case the user configured more keys than the basic ones
65
            // it will be stored as extra data
66 2
            foreach($attribute as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
67 2
                if(!in_array($key, ['comparison', 'comparison_type', 'value'])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
68 2
                    $pra->addExtraData($key, $value);
69
                }
70
            }
71
            // This generator avoid useless memory consumption instead of returning a whole array
72 2
            yield $pra;
73
        }
74 2
    }
75
76
    /**
77
     * This method is meant to set appropriated extra data to $pra depending on comparison type
78
     *
79
     * @param PolicyRuleAttribute $pra
80
     * @param object $user
81
     * @param object $resource
82
     */
83 2
    public function processRuleAttributeComparisonType(PolicyRuleAttribute $pra, $user, $resource) {
84 2
        switch($pra->getComparisonType()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
85 2
            case 'user': $pra->setExtraData(['user' => $user]); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
86 2
            case 'object': $pra->setExtraData(['resource' => $resource]); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
87
        }
88 2
    }
89
}
90