Completed
Push — feature/sf-subset/institution-... ( 934b43 )
by A.
03:57
created

AllowedSecondFactorList::isBlank()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 static function deserialize(array $data)
91
    {
92
        $secondFactorTypes = array_map(function ($secondFactorString) {
93
            return new SecondFactorType($secondFactorString);
94
        }, $data['allowed_second_factors']);
95
96
        return new self($secondFactorTypes);
97
    }
98
99
    public function serialize()
100
    {
101
        return [
102
            'allowed_second_factors' => array_map(function (SecondFactorType $secondFactorType) {
103
                return $secondFactorType->getSecondFactorType();
104
            }, $this->allowedSecondFactors),
105
        ];
106
    }
107
108
    public function getIterator()
109
    {
110
        return new ArrayIterator($this->allowedSecondFactors);
111
    }
112
113
    public function jsonSerialize()
114
    {
115
        return [
116
            'allowed_second_factors' => $this->allowedSecondFactors
117
        ];
118
    }
119
120
    private function initializeWith(SecondFactorType $allowedSecondFactor)
121
    {
122
        if (!$this->contains($allowedSecondFactor)) {
123
            $this->allowedSecondFactors[] = $allowedSecondFactor;
124
        }
125
    }
126
}
127