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

SsoOn2faOptionType::convertToDatabaseValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 18
rs 9.9332
c 1
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\SsoOn2faOption;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Configuration\Value\SsoOn2faOption 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 SsoOn2faOption Value Object
31
 */
32
class SsoOn2faOptionType extends Type
33
{
34
    public const NAME = 'stepup_sso_on_2fa_option';
35
36
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
37
    {
38
        return $platform->getIntegerTypeDeclarationSQL($column);
39
    }
40
41
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
42
    {
43
        if (is_null($value)) {
44
            return null;
45
        }
46
47
        if (!$value instanceof SsoOn2faOption) {
48
            throw new ConversionException(
49
                sprintf(
50
                    "Encountered illegal sso on 2fo vet option %s '%s', expected a 
51
                    SsoOn2faOption instance",
52
                    get_debug_type($value),
53
                    is_scalar($value) ? (string)$value : '',
54
                ),
55
            );
56
        }
57
58
        return (int)$value->isEnabled();
59
    }
60
61
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?SsoOn2faOption
62
    {
63
        if (is_null($value)) {
64
            return null;
65
        }
66
67
        try {
68
            $ssoOn2faOption = new SsoOn2faOption((bool)$value);
69
        } catch (TypeError $e) {
70
            throw ValueNotConvertible::new(
71
                $value,
72
                $this->getName(),
73
                $e->getMessage(),
74
                $e,
75
            );
76
        }
77
78
        return $ssoOn2faOption;
79
    }
80
81
    public function getName(): string
82
    {
83
        return self::NAME;
84
    }
85
86
    public function getBindingType(): ParameterType
87
    {
88
        return ParameterType::INTEGER;
89
    }
90
}
91