Passed
Push — feature/doctrine-dbal4 ( 5ae582 )
by Johan
05:56
created

SsoRegistrationBypassOptionType::getBindingType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2022 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\StepupMiddleware\ApiBundle\Doctrine\Type;
20
21
use Doctrine\DBAL\ParameterType;
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use Doctrine\DBAL\Types\ConversionException;
24
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
25
use Doctrine\DBAL\Types\Type;
26
use Surfnet\Stepup\Configuration\Value\SsoRegistrationBypassOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configura...egistrationBypassOption 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
use TypeError;
28
29
/**
30
 * Custom Type for the SsoRegistrationBypassOption Value Object
31
 *
32
 * This option enables and disables the "GSSP fallback" option in the Stepup-Gateway for an institution.
33
 * "GSSP fallback" forwards the second factor authentications at LoA 1.5 to the fallback GSSP when a user does not have
34
 * any active tokens
35
 */
36
class SsoRegistrationBypassOptionType extends Type
37
{
38
    public const NAME = 'stepup_sso_registration_bypass_option';
39
40
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
41
    {
42
        return $platform->getIntegerTypeDeclarationSQL($column);
43
    }
44
45
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
46
    {
47
        if (is_null($value)) {
48
            return null;
49
        }
50
51
        if (!$value instanceof SsoRegistrationBypassOption) {
52
            throw new ConversionException(
53
                sprintf(
54
                    "Encountered illegal sso registration bypass option %s '%s', expected a 
55
                    SsoRegistrationBypassOption instance",
56
                    get_debug_type($value),
57
                    is_scalar($value) ? (string)$value : '',
58
                ),
59
            );
60
        }
61
62
        return (int)$value->isEnabled();
63
    }
64
65
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?SsoRegistrationBypassOption
66
    {
67
        if (is_null($value)) {
68
            return null;
69
        }
70
71
        try {
72
            $ssoRegistrationBypassOption = new SsoRegistrationBypassOption((bool)$value);
73
        } catch (TypeError $e) {
74
            throw ValueNotConvertible::new(
75
                $value,
76
                $this->getName(),
77
                $e->getMessage(),
78
                $e,
79
            );
80
        }
81
82
        return $ssoRegistrationBypassOption;
83
    }
84
85
    public function getName(): string
86
    {
87
        return self::NAME;
88
    }
89
90
    public function getBindingType(): ParameterType
91
    {
92
        return ParameterType::INTEGER;
93
    }
94
}
95