Completed
Pull Request — develop (#171)
by A.
13:27 queued 08:55
created

AllowedSecondFactorList::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
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 IteratorAggregate;
23
use JsonSerializable;
24
use Surfnet\StepupBundle\Value\SecondFactorType;
25
26
final class AllowedSecondFactorList implements JsonSerializable, IteratorAggregate
27
{
28
    /**
29
     * @var SecondFactorType[]
30
     */
31
    private $allowedSecondFactors = [];
32
33
    private function __construct(array $allowedSecondFactors)
34
    {
35
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
36
            $this->initializeWith($allowedSecondFactor);
37
        }
38
    }
39
40
    /**
41
     * @return AllowedSecondFactorList
42
     */
43
    public static function blank()
44
    {
45
        return new self([]);
46
    }
47
48
    /**
49
     * @param $allowedSecondFactors
50
     * @return AllowedSecondFactorList
51
     */
52
    public static function ofTypes($allowedSecondFactors)
53
    {
54
        return new self($allowedSecondFactors);
55
    }
56
57
    /**
58
     * @param SecondFactorType $secondFactor
59
     * @return bool
60
     */
61
    public function allows(SecondFactorType $secondFactor)
62
    {
63
        return $this->isBlank() || $this->contains($secondFactor);
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isBlank()
70
    {
71
        return empty($this->allowedSecondFactors);
72
    }
73
74
    /**
75
     * @param SecondFactorType $secondFactor
76
     * @return bool
77
     */
78
    public function contains(SecondFactorType $secondFactor)
79
    {
80
        foreach ($this->allowedSecondFactors as $allowedSecondFactor) {
81
            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...
82
                return true;
83
            }
84
        }
85
86
        return false;
87
    }
88
89
    public function getIterator()
90
    {
91
        return new ArrayIterator($this->allowedSecondFactors);
92
    }
93
94
    public function jsonSerialize()
95
    {
96
        return [
97
            'allowedSecondFactors' => $this->allowedSecondFactors
98
        ];
99
    }
100
101
    private function initializeWith(SecondFactorType $allowedSecondFactor)
102
    {
103
        if (!$this->contains($allowedSecondFactor)) {
104
            $this->allowedSecondFactors[] = $allowedSecondFactor;
105
        }
106
    }
107
}
108