Completed
Branch master (458557)
by T
02:07
created

Resource::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 8
nop 1
crap 5
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 20 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 20
        if ( ! is_array($resources)) {
20 19
            $resources = (array)$resources;
21 19
        }
22
23 20
        foreach ($resources as $resource) {
24 20
            if ($resource === '*') {
25 2
                continue;
26
            }
27
28 18
            if (strpos($resource, self::$prefix . ':') !== 0) {
29 1
                throw new \InvalidArgumentException('Invalid resource "' . $resource . '".');
30
            }
31 19
        }
32
33 19
        $this->resources = $resources;
34 19
    }
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 13
    public function matches($requestedResource)
49
    {
50 13
        foreach ($this->resources as $resource) {
51 13
            $resourceRegex = '/^'.str_replace('\*', '.*', preg_quote($resource, '/')).'$/';
52 13
            if (preg_match($resourceRegex, $requestedResource)) {
53 7
                return true;
54
            }
55 6
        }
56
57 6
        return false;
58
    }
59
}
60