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

MonthIntervalType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Month\MonthInterval;
17
use GpsLab\Component\Interval\Exception\InvalidIntervalFormatException;
18
19 View Code Duplication
class MonthIntervalType 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 MonthInterval|null $value
23
     * @param AbstractPlatform $platform
24
     *
25
     * @return null|string
26
     */
27
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
28
    {
29
        return $value instanceof MonthInterval ? (string)$value : null;
30
    }
31
32
    /**
33
     * @throws ConversionException
34
     *
35
     * @param mixed $value
36
     * @param AbstractPlatform $platform
37
     *
38
     * @return null|MonthInterval
39
     */
40
    public function convertToPHPValue($value, AbstractPlatform $platform)
41
    {
42
        if ($value === null) {
43
            return null;
44
        }
45
46
        try {
47
            return MonthInterval::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 'MonthInterval';
59
    }
60
}
61