Passed
Push — main ( 7c62ee...93b57a )
by Johan
22:59 queued 17:41
created

VettingTypeHintsType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 39
rs 10
c 2
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 3 1
A getName() 0 3 1
A convertToPHPValue() 0 20 3
A requiresSQLCommentHint() 0 3 1
1
<?php
2
3
/**
4
 * Copyright 2022 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\JsonType;
24
use Surfnet\Stepup\Exception\InvalidArgumentException;
25
use Surfnet\Stepup\Identity\Collection\VettingTypeHintCollection;
26
27
/**
28
 * Custom Type for the vetting type hints Value Object
29
 */
30
class VettingTypeHintsType extends JsonType
31
{
32
    public const NAME = 'stepup_vetting_type_hints';
33
34
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
35
    {
36
        return $platform->getJsonTypeDeclarationSQL($column);
37
    }
38
39
    public function convertToPHPValue($value, AbstractPlatform $platform): ?VettingTypeHintCollection
40
    {
41
        if (is_null($value)) {
42
            return null;
43
        }
44
45
        try {
46
            $data = json_decode((string)$value, true);
47
            $vettingTypeHints = VettingTypeHintCollection::deserialize($data);
48
        } catch (InvalidArgumentException $e) {
49
            // get nice standard message, so we can throw it keeping the exception chain
50
            $doctrineExceptionMessage = ConversionException::conversionFailed(
51
                $value,
52
                $this->getName(),
53
            )->getMessage();
54
55
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
56
        }
57
58
        return $vettingTypeHints;
59
    }
60
61
    public function getName(): string
62
    {
63
        return self::NAME;
64
    }
65
66
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
67
    {
68
        return false;
69
    }
70
}
71