Completed
Push — master ( 64908a...328605 )
by
unknown
03:10
created

VerifyEmailOptionType::convertToPHPValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
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\VerifyEmailOption;
25
use Surfnet\Stepup\Exception\InvalidArgumentException;
26
27
/**
28
 * Custom Type for the VerifyEmailOption Value Object
29
 */
30 View Code Duplication
class VerifyEmailOptionType extends Type
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
{
32
    const NAME = 'stepup_verify_email_option';
33
34
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
35
    {
36
        return $platform->getBooleanTypeDeclarationSQL($fieldDeclaration);
37
    }
38
39
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
40
    {
41
        if (is_null($value)) {
42
            return $value;
43
        }
44
45
        if (!$value instanceof VerifyEmailOption) {
46
            throw new ConversionException(
47
                sprintf(
48
                    "Encountered illegal location of type %s '%s', expected a VerifyEmailOption instance",
49
                    is_object($value) ? get_class($value) : gettype($value),
50
                    is_scalar($value) ? (string) $value : ''
51
                )
52
            );
53
        }
54
55
        return $value->isEnabled();
56
    }
57
58
    public function convertToPHPValue($value, AbstractPlatform $platform)
59
    {
60
        if (is_null($value)) {
61
            return $value;
62
        }
63
64
        try {
65
            $verifyEmailOption = new VerifyEmailOption($platform->convertFromBoolean($value));
66
        } catch (InvalidArgumentException $e) {
67
            // get nice standard message, so we can throw it keeping the exception chain
68
            $doctrineExceptionMessage = ConversionException::conversionFailed(
69
                $value,
70
                $this->getName()
71
            )->getMessage();
72
73
            throw new ConversionException($doctrineExceptionMessage, 0, $e);
74
        }
75
76
        return $verifyEmailOption;
77
    }
78
79
    public function getName()
80
    {
81
        return self::NAME;
82
    }
83
}
84