Completed
Push — master ( 458557...42185b )
by T
02:03
created

Resource::mapVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace tomzx\PolicyEvaluator;
4
5
class Resource
6
{
7
    public static $prefix = 'urn';
8
9
    /**
10
     * @var array
11
     */
12
    private $resources;
13
14
    /**
15
     * @param array|string $resources
16
     */
17 24 View Code Duplication
    public function __construct($resources)
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...
18
    {
19 24
        if ( ! is_array($resources)) {
20 23
            $resources = (array)$resources;
21 23
        }
22
23 24
        foreach ($resources as $resource) {
24 24
            if ($resource === '*') {
25 2
                continue;
26
            }
27
28 22
            if (strpos($resource, self::$prefix . ':') !== 0) {
29 1
                throw new \InvalidArgumentException('Invalid resource "' . $resource . '".');
30
            }
31 23
        }
32
33 23
        $this->resources = $resources;
34 23
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getResources()
40
    {
41
        return $this->resources;
42
    }
43
44
    /**
45
     * @param string $requestedResource
46
     * @return bool
47
     */
48 17
    public function matches($requestedResource, array $variables = [])
49
    {
50 17
        $variableStrings = $this->mapVariables($variables);
51 17
        foreach ($this->resources as $resource) {
52 17
            $preparedResource = $this->replaceWildcard($this->replaceVariables($resource, $variableStrings));
53 17
            $resourceRegex = '/^'.$preparedResource.'$/';
54 17
            if (preg_match($resourceRegex, $requestedResource)) {
55 10
                return true;
56
            }
57 7
        }
58
59 7
        return false;
60
    }
61
62
    /**
63
     * @param array $variables
64
     * @return array
65
     */
66 17
    private function mapVariables(array $variables)
67
    {
68 17
        $mappedVariables = [];
69 17
        foreach ($variables as $key => $value) {
70 3
            $mappedVariables['${'.$key.'}'] = $value;
71 17
        }
72 17
        return $mappedVariables;
73
    }
74
75
    /**
76
     * @param string $resource
77
     * @param array $variables
78
     * @return string
79
     */
80 17
    private function replaceVariables($resource, array $variables)
81
    {
82 17
        return strtr($resource, $variables);
83
    }
84
85
    /**
86
     * @param string $resource
87
     * @return string
88
     */
89 17
    private function replaceWildcard($resource)
90
    {
91 17
        return str_replace('\*', '.*', preg_quote($resource, '/'));
92
    }
93
}
94