Completed
Pull Request — develop (#171)
by A.
06:33 queued 01:40
created

AllowedSecondFactorList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A isAllowed() 0 14 4
A getIterator() 0 4 1
A jsonSerialize() 0 6 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\Stepup\Exception\InvalidArgumentException;
25
26
final class AllowedSecondFactorList implements JsonSerializable, IteratorAggregate
27
{
28
    /**
29
     * @var SecondFactor[]
30
     */
31
    private $allowedSecondFactors;
32
33
    public function __construct(array $allowedSecondFactors)
34
    {
35
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
36
            if (!$allowedSecondFactor instanceof SecondFactor) {
37
                throw InvalidArgumentException::invalidType(
38
                    SecondFactor::class,
39
                    'allowedSecondFactor',
40
                    $allowedSecondFactor
41
                );
42
            }
43
        }
44
45
        $this->allowedSecondFactors = $allowedSecondFactors;
46
    }
47
48
    /**
49
     * @param SecondFactor $other
50
     * @return bool
51
     */
52
    public function isAllowed(SecondFactor $other)
53
    {
54
        if (empty($this->allowedSecondFactors)) {
55
            return true;
56
        }
57
58
        foreach ($this->allowedSecondFactors as $secondFactorType) {
59
            if ($secondFactorType->equals($other)) {
0 ignored issues
show
Documentation introduced by
$other is of type object<Surfnet\Stepup\Co...ion\Value\SecondFactor>, 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...
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    public function getIterator()
68
    {
69
        return new ArrayIterator($this->allowedSecondFactors);
70
    }
71
72
    public function jsonSerialize()
73
    {
74
        return [
75
            'allowedSecondFactors' => $this->allowedSecondFactors
76
        ];
77
    }
78
}
79