AudienceRestriction::hasAudience()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.8666
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\Assertion;
13
14
use LightSaml\Model\Context\DeserializationContext;
15
use LightSaml\Model\Context\SerializationContext;
16
use LightSaml\SamlConstants;
17
18
class AudienceRestriction extends AbstractCondition
19
{
20
    /**
21
     * @var string[]
22
     */
23
    protected $audience = array();
24
25
    /**
26
     * @param string|string[] $audience
27
     */
28 15
    public function __construct($audience = array())
29
    {
30 15
        if (false == is_array($audience)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
31 1
            $audience = array($audience);
32
        }
33 15
        $this->audience = $audience;
34 15
    }
35
36
    /**
37
     * @param string $audience
38
     *
39
     * @return AudienceRestriction
40
     */
41 6
    public function addAudience($audience)
42
    {
43 6
        $this->audience[] = $audience;
44
45 6
        return $this;
46
    }
47
48
    /**
49
     * @return string[]
50
     */
51 7
    public function getAllAudience()
52
    {
53 7
        return $this->audience;
54
    }
55
56
    /**
57
     * @param string $value
58
     *
59
     * @return bool
60
     */
61 2
    public function hasAudience($value)
62
    {
63 2
        if (is_array($this->audience)) {
64 2
            foreach ($this->audience as $a) {
65 2
                if ($a == $value) {
66 2
                    return true;
67
                }
68
            }
69
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * @param \DOMNode             $parent
76
     * @param SerializationContext $context
77
     */
78 4
    public function serialize(\DOMNode $parent, SerializationContext $context)
79
    {
80 4
        $result = $this->createElement('AudienceRestriction', SamlConstants::NS_ASSERTION, $parent, $context);
81
82 4
        $this->manyElementsToXml($this->getAllAudience(), $result, $context, 'Audience', SamlConstants::NS_ASSERTION);
83 4
    }
84
85
    /**
86
     * @param \DOMNode               $node
87
     * @param DeserializationContext $context
88
     */
89 3 View Code Duplication
    public function deserialize(\DOMNode $node, DeserializationContext $context)
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...
90
    {
91 3
        $this->checkXmlNodeName($node, 'AudienceRestriction', SamlConstants::NS_ASSERTION);
92
93 3
        $this->audience = array();
94 3
        $this->manyElementsFromXml($node, $context, 'Audience', 'saml', null, 'addAudience');
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95 3
    }
96
}
97