Completed
Push — feature/sf-subset/institution-... ( 7b1c92 )
by A.
04:11
created

AllowedSecondFactorList::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
91
    public function equals(AllowedSecondFactorList $other)
92
    {
93
        if (count($other->allowedSecondFactors) !== count($this->allowedSecondFactors)) {
94
            return false;
95
        }
96
97
        foreach ($other->allowedSecondFactors as $allowedSecondFactor) {
98
            if (!$this->contains($allowedSecondFactor)) {
99
                return false;
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    public static function deserialize(array $data)
107
    {
108
        $secondFactorTypes = array_map(
109
            function ($secondFactorString) {
110
                return new SecondFactorType($secondFactorString);
111
            },
112
            $data['allowed_second_factors']
113
        );
114
115
        return new self($secondFactorTypes);
116
    }
117
118
    public function serialize()
119
    {
120
        $allowedSecondFactors = array_map(
121
            function (SecondFactorType $secondFactorType) {
122
                return $secondFactorType->getSecondFactorType();
123
            },
124
            $this->allowedSecondFactors
125
        );
126
127
        return [
128
            'allowed_second_factors' => $allowedSecondFactors,
129
        ];
130
    }
131
132
    public function getIterator()
133
    {
134
        return new ArrayIterator($this->allowedSecondFactors);
135
    }
136
137
    public function jsonSerialize()
138
    {
139
        return [
140
            'allowed_second_factors' => $this->allowedSecondFactors
141
        ];
142
    }
143
144
    private function initializeWith(SecondFactorType $allowedSecondFactor)
145
    {
146
        if (!$this->contains($allowedSecondFactor)) {
147
            $this->allowedSecondFactors[] = $allowedSecondFactor;
148
        }
149
    }
150
}
151