Completed
Push — feature/authz-service ( 007e40...3c7a6c )
by
unknown
02:53
created

InstitutionRoleType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 4 1
A convertToDatabaseValue() 0 14 4
A convertToPHPValue() 0 16 2
A getName() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2018 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\InstitutionOption;
25
use Surfnet\Stepup\Configuration\Value\InstitutionRole;
26
use Surfnet\Stepup\Exception\InvalidArgumentException;
27
28
/**
29
 * Custom Type for the InstitutionRole Value Object
30
 */
31
class InstitutionRoleType extends Type
32
{
33
    const NAME = 'stepup_institution_role';
34
35
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
36
    {
37
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
38
    }
39
40
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
41
    {
42
        if (!$value instanceof InstitutionRole) {
43
            throw new ConversionException(
44
                sprintf(
45
                    "Encountered illegal location of type %s '%s', expected a InstitutionOption instance",
46
                    is_object($value) ? get_class($value) : gettype($value),
47
                    is_scalar($value) ? (string) $value : ''
48
                )
49
            );
50
        }
51
52
        return $value->getType();
53
    }
54
55
    public function convertToPHPValue($value, AbstractPlatform $platform)
56
    {
57
        try {
58
            $institutionRole = InstitutionRole::create($value);
59
        } catch (InvalidArgumentException $e) {
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 $institutionRole;
70
    }
71
72
    public function getName()
73
    {
74
        return self::NAME;
75
    }
76
}
77