Action::matches()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace tomzx\PolicyEvaluator;
4
5
class Action
6
{
7
    /**
8
     * @var array
9
     */
10
    private $actions;
11
12
    /**
13
     * @param array|string $actions
14
     */
15 23 View Code Duplication
    public function __construct($actions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17 23
        if ( ! is_array($actions)) {
18 21
            $actions = (array)$actions;
19
        }
20
21 23
        foreach ($actions as $action) {
22 23
            if ($action === '*') {
23 2
                continue;
24
            }
25
26 21
            if (strpos($action, ':') === false) {
27 21
                throw new \InvalidArgumentException('Invalid service prefix for action "' . $action . '".');
28
            }
29
        }
30
31 21
        $this->actions = $actions;
32 21
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getActions()
38
    {
39
        return $this->actions;
40
    }
41
42
    /**
43
     * @param string $requestedAction
44
     * @return bool
45
     */
46 16
    public function matches($requestedAction)
47
    {
48 16
        foreach ($this->actions as $action) {
49 16
            $actionRegex = '/^'.str_replace('\*', '.*', preg_quote($action, '/')).'$/';
50 16
            if (preg_match($actionRegex, $requestedAction)) {
51 16
                return true;
52
            }
53
        }
54
55 6
        return false;
56
    }
57
}
58