LocationType::requiresSQLCommentHint()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
0 ignored issues
show
Coding Style introduced by
The file-level docblock must follow the opening PHP tag in the file header
Loading history...
6
 * Copyright 2014 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type;
22
23
use Doctrine\DBAL\Platforms\AbstractPlatform;
24
use Doctrine\DBAL\Types\ConversionException;
25
use Doctrine\DBAL\Types\Type;
26
use Surfnet\Stepup\Exception\InvalidArgumentException;
27
use Surfnet\Stepup\Identity\Value\Location;
28
use TypeError;
29
30
/**
31
 * Custom Type for the Location Value Object
32
 */
33
class LocationType extends Type
34
{
35
    public const NAME = 'stepup_location';
36
37
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
38
    {
39
        return $platform->getClobTypeDeclarationSQL($column);
40
    }
41
42
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
43
    {
44
        if (is_null($value)) {
45
            return null;
46
        }
47
48
        return (string)$value;
49
    }
50
51
    public function convertToPHPValue($value, AbstractPlatform $platform): ?Location
52
    {
53
        if (is_null($value)) {
54
            return null;
55
        }
56
57
        try {
58
            $location = new Location($value);
59
        } catch (InvalidArgumentException|TypeError $e) {
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "|"; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "|"; 0 found
Loading history...
60
            // get nice standard message, so we can throw it keeping the exception chain
61
            $doctrineExceptionMessage = ConversionException::conversionFailed(
62
                $value,
63
                $this->getName(),
64
            )->getMessage();
65
66
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
67
        }
68
69
        return $location;
70
    }
71
72
    public function getName(): string
73
    {
74
        return self::NAME;
75
    }
76
77
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
78
    {
79
        return false;
80
    }
81
}
82