AllowedSecondFactorList::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 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\Serializable as SerializableInterface;
23
use Iterator;
24
use IteratorAggregate;
25
use JsonSerializable;
26
use Surfnet\StepupBundle\Value\SecondFactorType;
27
28
/**
29
 * @implements IteratorAggregate<SecondFactorType>
30
 */
31
final class AllowedSecondFactorList implements JsonSerializable, IteratorAggregate, SerializableInterface
32
{
33
    /**
34
     * @var SecondFactorType[]
35
     */
36
    private array $allowedSecondFactors = [];
37
38
    private function __construct(array $allowedSecondFactors)
39
    {
40
        foreach ($allowedSecondFactors as $allowedSecondFactor) {
41
            $this->initializeWith($allowedSecondFactor);
42
        }
43
    }
44
45
    /**
46
     * @return AllowedSecondFactorList
47
     */
48
    public static function blank(): self
49
    {
50
        return new self([]);
51
    }
52
53
    public static function ofTypes(array $allowedSecondFactors): self
54
    {
55
        return new self($allowedSecondFactors);
56
    }
57
58
    public function allows(SecondFactorType $secondFactor): bool
59
    {
60
        return $this->isBlank() || $this->contains($secondFactor);
61
    }
62
63
    public function isBlank(): bool
64
    {
65
        return $this->allowedSecondFactors === [];
66
    }
67
68
    public function contains(SecondFactorType $secondFactor): bool
69
    {
70
        foreach ($this->allowedSecondFactors as $allowedSecondFactor) {
71
            if ($allowedSecondFactor->equals($secondFactor)) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    public function equals(AllowedSecondFactorList $other): bool
80
    {
81
        if (count($other->allowedSecondFactors) !== count($this->allowedSecondFactors)) {
82
            return false;
83
        }
84
85
        foreach ($other->allowedSecondFactors as $allowedSecondFactor) {
86
            if (!$this->contains($allowedSecondFactor)) {
87
                return false;
88
            }
89
        }
90
91
        return true;
92
    }
93
94
    public static function deserialize(array $data): self
95
    {
96
        $secondFactorTypes = array_map(
97
            fn($secondFactorString): SecondFactorType => new SecondFactorType($secondFactorString),
98
            $data['allowed_second_factors'],
99
        );
100
101
        return new self($secondFactorTypes);
102
    }
103
104
    public function serialize(): array
105
    {
106
        $allowedSecondFactors = array_map(
107
            fn(SecondFactorType $secondFactorType): string => $secondFactorType->getSecondFactorType(),
108
            $this->allowedSecondFactors,
109
        );
110
111
        return [
112
            'allowed_second_factors' => $allowedSecondFactors,
113
        ];
114
    }
115
116
    public function getIterator(): Iterator
117
    {
118
        return new ArrayIterator($this->allowedSecondFactors);
119
    }
120
121
    public function jsonSerialize(): array
122
    {
123
        return $this->allowedSecondFactors;
124
    }
125
126
    private function initializeWith(SecondFactorType $allowedSecondFactor): void
127
    {
128
        if (!$this->contains($allowedSecondFactor)) {
129
            $this->allowedSecondFactors[] = $allowedSecondFactor;
130
        }
131
    }
132
}
133