ShowRaaContactInformationOptionType::getName()   A
last analyzed

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 2016 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\Platforms\AbstractPlatform;
22
use Doctrine\DBAL\Types\ConversionException;
23
use Doctrine\DBAL\Types\Type;
24
use Surfnet\Stepup\Configuration\Value\ShowRaaContactInformationOption;
25
use Surfnet\Stepup\Exception\InvalidArgumentException;
26
27
/**
28
 * Custom Type for the ShowRaaContactInformationOption Value Object
29
 */
30
class ShowRaaContactInformationOptionType extends Type
31
{
32
    public const NAME = 'stepup_show_raa_contact_information_option';
33
34
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
35
    {
36
        return $platform->getBooleanTypeDeclarationSQL($column);
37
    }
38
39
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
40
    {
41
        if (is_null($value)) {
42
            return null;
43
        }
44
45
        if (!$value instanceof ShowRaaContactInformationOption) {
46
            throw new ConversionException(
47
                sprintf(
48
                    "Encountered illegal location of type %s '%s', expected a ShowRaaContactInformationOption instance",
49
                    get_debug_type($value),
50
                    is_scalar($value) ? (string)$value : '',
51
                ),
52
            );
53
        }
54
55
        return (int)$value->isEnabled();
56
    }
57
58
    public function convertToPHPValue($value, AbstractPlatform $platform): ?ShowRaaContactInformationOption
59
    {
60
        if (is_null($value)) {
61
            return null;
62
        }
63
64
        try {
65
            $showRaaContactInformationOption = new ShowRaaContactInformationOption(
66
                $platform->convertFromBoolean($value),
67
            );
68
        } catch (InvalidArgumentException $e) {
69
            // get nice standard message, so we can throw it keeping the exception chain
70
            $doctrineExceptionMessage = ConversionException::conversionFailed(
71
                $value,
72
                $this->getName(),
73
            )->getMessage();
74
75
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
76
        }
77
78
        return $showRaaContactInformationOption;
79
    }
80
81
    public function getName(): string
82
    {
83
        return self::NAME;
84
    }
85
86
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
87
    {
88
        return false;
89
    }
90
}
91