Completed
Push — develop ( 646fcc...d8d88a )
by A.
11s
created

AllowedSecondFactorList::equals()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2017 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Configuration\Value;
20
21
use ArrayIterator;
22
use Broadway\Serializer\SerializableInterface;
23
use IteratorAggregate;
24
use JsonSerializable;
25
use Surfnet\StepupBundle\Value\SecondFactorType;
26
27
final class AllowedSecondFactorList implements JsonSerializable, IteratorAggregate, SerializableInterface
28
{
29
    /**
30
     * @var SecondFactorType[]
31
     */
32
    private $allowedSecondFactors = [];
33
34
    private function __construct(array $allowedSecondFactors)
35
    {
36
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
37
            $this->initializeWith($allowedSecondFactor);
38
        }
39
    }
40
41
    /**
42
     * @return AllowedSecondFactorList
43
     */
44
    public static function blank()
45
    {
46
        return new self([]);
47
    }
48
49
    /**
50
     * @param $allowedSecondFactors
51
     * @return AllowedSecondFactorList
52
     */
53
    public static function ofTypes($allowedSecondFactors)
54
    {
55
        return new self($allowedSecondFactors);
56
    }
57
58
    /**
59
     * @param SecondFactorType $secondFactor
60
     * @return bool
61
     */
62
    public function allows(SecondFactorType $secondFactor)
63
    {
64
        return $this->isBlank() || $this->contains($secondFactor);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function isBlank()
71
    {
72
        return empty($this->allowedSecondFactors);
73
    }
74
75
    /**
76
     * @param SecondFactorType $secondFactor
77
     * @return bool
78
     */
79
    public function contains(SecondFactorType $secondFactor)
80
    {
81
        foreach ($this->allowedSecondFactors as $allowedSecondFactor) {
82
            if ($allowedSecondFactor->equals($secondFactor)) {
0 ignored issues
show
Documentation introduced by
$secondFactor is of type object<Surfnet\StepupBun...Value\SecondFactorType>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
                return true;
84
            }
85
        }
86
87
        return false;
88
    }
89
90
    public function equals(AllowedSecondFactorList $other)
91
    {
92
        if (count($other->allowedSecondFactors) !== count($this->allowedSecondFactors)) {
93
            return false;
94
        }
95
96
        foreach ($other->allowedSecondFactors as $allowedSecondFactor) {
97
            if (!$this->contains($allowedSecondFactor)) {
98
                return false;
99
            }
100
        }
101
102
        return true;
103
    }
104
105
    public static function deserialize(array $data)
106
    {
107
        $secondFactorTypes = array_map(
108
            function ($secondFactorString) {
109
                return new SecondFactorType($secondFactorString);
110
            },
111
            $data['allowed_second_factors']
112
        );
113
114
        return new self($secondFactorTypes);
115
    }
116
117
    public function serialize()
118
    {
119
        $allowedSecondFactors = array_map(
120
            function (SecondFactorType $secondFactorType) {
121
                return $secondFactorType->getSecondFactorType();
122
            },
123
            $this->allowedSecondFactors
124
        );
125
126
        return [
127
            'allowed_second_factors' => $allowedSecondFactors,
128
        ];
129
    }
130
131
    public function getIterator()
132
    {
133
        return new ArrayIterator($this->allowedSecondFactors);
134
    }
135
136
    public function jsonSerialize()
137
    {
138
        return [
139
            'allowed_second_factors' => $this->allowedSecondFactors
140
        ];
141
    }
142
143
    private function initializeWith(SecondFactorType $allowedSecondFactor)
144
    {
145
        if (!$this->contains($allowedSecondFactor)) {
146
            $this->allowedSecondFactors[] = $allowedSecondFactor;
147
        }
148
    }
149
}
150