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

CommonNameType::requiresSQLCommentHint()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 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\Exception\InvalidArgumentException;
25
use Surfnet\Stepup\Identity\Value\CommonName;
26
use TypeError;
27
28
/**
29
 * Custom Type for the CommonName Value Object
30
 */
31
class CommonNameType extends Type
32
{
33
    public const NAME = 'stepup_common_name';
34
35
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
36
    {
37
        if (!isset($column['length'])) {
38
            $column['length'] = 255;
39
        }
40
41
        return $platform->getStringTypeDeclarationSQL($column);
42
    }
43
44
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
45
    {
46
        if (is_null($value)) {
47
            return null;
48
        }
49
50
        return (string)$value;
51
    }
52
53
    public function convertToPHPValue($value, AbstractPlatform $platform): ?CommonName
54
    {
55
        if (is_null($value)) {
56
            return null;
57
        }
58
59
        try {
60
            $commonName = new CommonName($value);
61
        } 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...
62
            // get nice standard message, so we can throw it keeping the exception chain
63
            $doctrineExceptionMessage = ConversionException::conversionFailed(
64
                $value,
65
                $this->getName(),
66
            )->getMessage();
67
68
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
69
        }
70
71
        return $commonName;
72
    }
73
74
    public function getName(): string
75
    {
76
        return self::NAME;
77
    }
78
79
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
80
    {
81
        return false;
82
    }
83
}
84