ShibbolethMetadataScope   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 2
cbo 2
dl 0
loc 101
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A literal() 0 6 1
A regexp() 0 6 1
A __construct() 0 12 2
A allows() 0 11 2
A equals() 0 4 2
A deserialize() 0 7 1
A serialize() 0 7 1
A __toString() 0 8 2
1
<?php
2
3
namespace OpenConext\Value\Saml\Metadata;
4
5
use OpenConext\Value\Assert\Assertion;
6
use OpenConext\Value\RegularExpression;
7
use OpenConext\Value\Serializable;
8
9
final class ShibbolethMetadataScope implements Serializable
10
{
11
    /**
12
     * @var string
13
     */
14
    private $scope;
15
16
    /**
17
     * @var bool
18
     */
19
    private $isRegexp;
20
21
    /**
22
     * @param string $literal
23
     * @return ShibbolethMetadataScope
24
     */
25
    public static function literal($literal)
26
    {
27
        Assertion::nonEmptyString($literal, 'literal');
28
29
        return new self($literal);
30
    }
31
32
    /**
33
     * @param string $regexp
34
     * @return ShibbolethMetadataScope
35
     */
36
    public static function regexp($regexp)
37
    {
38
        Assertion::nonEmptyString($regexp, 'regexp');
39
40
        return new self($regexp, true);
41
    }
42
43
    /**
44
     * @param string $scope    the scope as defined
45
     * @param bool   $isRegexp whether or not the scope is a regular expression as identified by the regexp attribute
46
     */
47
    public function __construct($scope, $isRegexp = false)
48
    {
49
        Assertion::nonEmptyString($scope, 'scope');
50
        Assertion::boolean($isRegexp);
51
52
        if ($isRegexp) {
53
            Assertion::validRegularExpression('#' . $scope . '#i', 'scope');
54
        }
55
56
        $this->scope    = $scope;
57
        $this->isRegexp = $isRegexp;
58
    }
59
60
    /**
61
     * @param string $string
62
     * @return bool
63
     */
64
    public function allows($string)
65
    {
66
        Assertion::string($string, 'Scope to check should be a string, "%s" given');
67
68
        if (!$this->isRegexp) {
69
            return strcasecmp($this->scope, $string) === 0;
70
        }
71
72
        $regexp = new RegularExpression('#' . $this->scope . '#i');
73
        return $regexp->matches($string);
74
    }
75
76
    /**
77
     * @param ShibbolethMetadataScope $other
78
     * @return bool
79
     */
80
    public function equals(ShibbolethMetadataScope $other)
81
    {
82
        return (strcasecmp($this->scope, $other->scope) === 0 && $this->isRegexp === $other->isRegexp);
83
    }
84
85
    public static function deserialize($data)
86
    {
87
        Assertion::isArray($data);
88
        Assertion::keysExist($data, array('is_regexp', 'scope'));
89
90
        return new self($data['scope'], $data['is_regexp']);
91
    }
92
93
    public function serialize()
94
    {
95
        return array(
96
            'scope'     => $this->scope,
97
            'is_regexp' => $this->isRegexp
98
        );
99
    }
100
101
    public function __toString()
102
    {
103
        return sprintf(
104
            'ShibbolethMetadataScope(scope=%s, regexp=%s)',
105
            $this->scope,
106
            $this->isRegexp ? 'true' : 'false'
107
        );
108
    }
109
}
110