Passed
Push — feature/updates-nov25 ( 8eba28...986d8f )
by Johan
15:38 queued 09:59
created

ConfigurationInstitutionType   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDatabaseValue() 0 17 4
A requiresSQLCommentHint() 0 3 1
A convertToPHPValue() 0 19 3
A getName() 0 3 1
A getSQLDeclaration() 0 7 2
1
<?php
2
3
/**
4
 * Copyright 2016 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\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\Institution;
25
use Surfnet\Stepup\Exception\InvalidArgumentException;
26
27
/**
28
 * Custom Type for the Institution Value Object for the Configuration domain
29
 */
30
class ConfigurationInstitutionType extends Type
31
{
32
    public const NAME = 'stepup_configuration_institution';
33
34
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
35
    {
36
        if (!isset($column['length'])) {
37
            $column['length'] = 255;
38
        }
39
40
        return $platform->getStringTypeDeclarationSQL($column);
41
    }
42
43
    public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
44
    {
45
        if (is_null($value)) {
46
            return null;
47
        }
48
49
        if (!$value instanceof Institution) {
50
            throw new ConversionException(
51
                sprintf(
52
                    "Encountered illegal institution of type %s '%s', expected an Institution instance",
53
                    get_debug_type($value),
54
                    is_scalar($value) ? (string)$value : '',
55
                ),
56
            );
57
        }
58
59
        return $value->getInstitution();
60
    }
61
62
    public function convertToPHPValue($value, AbstractPlatform $platform): ?Institution
63
    {
64
        if (is_null($value)) {
65
            return null;
66
        }
67
68
        try {
69
            $institution = new Institution($value);
70
        } catch (InvalidArgumentException $e) {
71
            // get nice standard message, so we can throw it keeping the exception chain
72
            $doctrineExceptionMessage = ConversionException::conversionFailed(
73
                $value,
74
                $this->getName(),
75
            )->getMessage();
76
77
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
78
        }
79
80
        return $institution;
81
    }
82
83
    public function getName(): string
84
    {
85
        return self::NAME;
86
    }
87
88
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
89
    {
90
        return false;
91
    }
92
}
93