ConfigurableSettings::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\Identity\Entity;
20
21
use Exception;
22
use Surfnet\Stepup\DateTime\DateTime;
23
use Surfnet\Stepup\Exception\InvalidArgumentException;
24
use Surfnet\Stepup\Identity\Value\EmailVerificationWindow;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...EmailVerificationWindow was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Surfnet\Stepup\Identity\Value\Locale;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Value\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Surfnet\Stepup\Identity\Value\TimeFrame;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Value\TimeFrame was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * Entity that contains the User Defined Settings that are relevant to the domain
30
 */
31
final class ConfigurableSettings
32
{
33
    /**
34
     * @var Locale[]
35
     */
36
    private readonly array $locales;
37
38
    /**
39
     * @param Locale[] $locales
40
     */
41
    private function __construct(
42
        private readonly TimeFrame $emailVerificationTimeFrame,
43
        array $locales,
44
    ) {
45
        foreach ($locales as $index => $locale) {
46
            if (!$locale instanceof Locale) {
47
                throw InvalidArgumentException::invalidType(
48
                    Locale::class,
49
                    sprintf('locales[%s]', $index),
50
                    $locale,
51
                );
52
            }
53
        }
54
        $this->locales = $locales;
0 ignored issues
show
Bug introduced by
The property locales is declared read-only in Surfnet\Stepup\Identity\...ty\ConfigurableSettings.
Loading history...
55
    }
56
57
    /**
58
     * @param int $emailVerificationTimeFrame positive integer
59
     * @param string[] $locales
60
     * @return ConfigurableSettings
61
     * @throws Exception
62
     * @throws Exception
63
     */
64
    public static function create(int $emailVerificationTimeFrame, array $locales): self
65
    {
66
        return new self(
67
            TimeFrame::ofSeconds($emailVerificationTimeFrame),
68
            array_map(
69
                fn($locale): Locale => new Locale($locale),
70
                $locales,
71
            ),
72
        );
73
    }
74
75
    /**
76
     * @return EmailVerificationWindow
77
     */
78
    public function createNewEmailVerificationWindow(): EmailVerificationWindow
79
    {
80
        return EmailVerificationWindow::createFromTimeFrameStartingAt(
81
            $this->emailVerificationTimeFrame,
82
            DateTime::now(),
83
        );
84
    }
85
86
    public function isSupportedLocale(Locale $locale): bool
87
    {
88
        return array_reduce(
89
            $this->locales,
90
            fn($supported, Locale $supportedLocale): bool => $supported || $supportedLocale->equals($locale),
91
            false,
92
        );
93
    }
94
}
95