Resource::replaceVariables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
        }
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 22
                throw new \InvalidArgumentException('Invalid resource "' . $resource . '".');
30
            }
31
        }
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
     * @return array
46
     */
47 17
    public function getResourcesCompiled(array $variables = [])
48
    {
49 17
        $variableStrings = $this->mapVariables($variables);
50 17
        $resources = [];
51
52 17
        foreach ($this->resources as $resource) {
53 17
            $resources[] = $this->replaceVariables($resource, $variableStrings);
54
        }
55
56 17
        return $resources;
57
    }
58
59
    /**
60
     * @param string $requestedResource
61
     * @return bool
62
     */
63 17
    public function matches($requestedResource, array $variables = [])
64
    {
65 17
        $resources = $this->getResourcesCompiled($variables);
66
67 17
        foreach ($resources as $resource) {
68 17
            $preparedResource = $this->replaceWildcard($resource);
69 17
            $resourceRegex    = '/^'.$preparedResource.'$/';
70
71 17
            if (preg_match($resourceRegex, $requestedResource)) {
72 17
                return true;
73
            }
74
        }
75
76 7
        return false;
77
    }
78
79
    /**
80
     * @param array $variables
81
     * @return array
82
     */
83 17
    private function mapVariables(array $variables)
84
    {
85 17
        $mappedVariables = [];
86 17
        foreach ($variables as $key => $value) {
87 3
            $mappedVariables['${'.$key.'}'] = $value;
88
        }
89 17
        return $mappedVariables;
90
    }
91
92
    /**
93
     * @param string $resource
94
     * @param array $variables
95
     * @return string
96
     */
97 17
    private function replaceVariables($resource, array $variables)
98
    {
99 17
        return strtr($resource, $variables);
100
    }
101
102
    /**
103
     * @param string $resource
104
     * @return string
105
     */
106 17
    private function replaceWildcard($resource)
107
    {
108 17
        return str_replace('\*', '.*', preg_quote($resource, '/'));
109
    }
110
}
111