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

VerifyEmailOptionType::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 2018 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\VerifyEmailOption;
27
use Surfnet\Stepup\Exception\InvalidArgumentException;
28
29
/**
30
 * Custom Type for the VerifyEmailOption Value Object
31
 */
32
class VerifyEmailOptionType extends Type
33
{
34
    public const NAME = 'stepup_verify_email_option';
35
36
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
37
    {
38
        return $platform->getBooleanTypeDeclarationSQL($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 VerifyEmailOption) {
48
            throw new ConversionException(
49
                sprintf(
50
                    "Encountered illegal location of type %s '%s', expected a VerifyEmailOption instance",
51
                    get_debug_type($value),
52
                    is_scalar($value) ? (string)$value : '',
53
                ),
54
            );
55
        }
56
57
        return (int)$value->isEnabled();
58
    }
59
60
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?VerifyEmailOption
61
    {
62
        if (is_null($value)) {
63
            return null;
64
        }
65
66
        try {
67
            $verifyEmailOption = new VerifyEmailOption($platform->convertFromBoolean($value));
0 ignored issues
show
Bug introduced by
It seems like $platform->convertFromBoolean($value) can also be of type null; however, parameter $verifyEmailOption of Surfnet\Stepup\Configura...ilOption::__construct() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $verifyEmailOption = new VerifyEmailOption(/** @scrutinizer ignore-type */ $platform->convertFromBoolean($value));
Loading history...
68
        } catch (InvalidArgumentException $e) {
69
            throw ValueNotConvertible::new(
70
                $value,
71
                $this->getName(),
72
                $e->getMessage(),
73
                $e,
74
            );
75
        }
76
77
        return $verifyEmailOption;
78
    }
79
80
    public function getName(): string
81
    {
82
        return self::NAME;
83
    }
84
85
    public function getBindingType(): ParameterType
86
    {
87
        return ParameterType::INTEGER;
88
    }
89
}
90