Completed
Push — master ( 6b9cde...8b40f4 )
by Peter
06:55
created

YearIntervalType::convertToPHPValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2016, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Interval\Persistence\Doctrine\DBAL\Types;
12
13
use Doctrine\DBAL\Platforms\AbstractPlatform;
14
use Doctrine\DBAL\Types\ConversionException;
15
use Doctrine\DBAL\Types\TextType;
16
use GpsLab\Component\Interval\Year\YearInterval;
17
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
18
19 View Code Duplication
class YearIntervalType extends TextType
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...
20
{
21
    /**
22
     * @param YearInterval|null $value
23
     * @param AbstractPlatform $platform
24
     *
25
     * @return null|string
26
     */
27
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
28
    {
29
        return $value instanceof YearInterval ? (string)$value : null;
30
    }
31
32
    /**
33
     * @throws ConversionException
34
     *
35
     * @param mixed $value
36
     * @param AbstractPlatform $platform
37
     *
38
     * @return null|YearInterval
39
     */
40
    public function convertToPHPValue($value, AbstractPlatform $platform)
41
    {
42
        if ($value === null) {
43
            return null;
44
        }
45
46
        try {
47
            return YearInterval::fromString($value);
48
        } catch (InvalidIntervalFormatException $e) {
49
            throw ConversionException::conversionFailed($value, $this->getName());
50
        }
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getName()
57
    {
58
        return 'YearInterval';
59
    }
60
}
61