DateTimeType::convertToPHPValue()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 5
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php declare (strict_types = 1);
2
3
namespace Limoncello\Flute\Types;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeInterface;
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use Doctrine\DBAL\Types\ConversionException;
24
use Doctrine\DBAL\Types\DateTimeType as BaseDateTimeType;
25
use Exception;
26
use function assert;
27
28
/**
29
 * @package Limoncello\Flute
30
 */
31 View Code Duplication
class DateTimeType extends BaseDateTimeType
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...
32
{
33
    use TypeTrait;
34
35
    /** Type name */
36
    const NAME = 'limoncelloDateTime';
37
38
    /**
39
     * @inheritdoc
40
     *
41
     * @return DateTime|null
42
     *
43
     * @throws ConversionException
44
     * @throws Exception
45
     *
46
     * @SuppressWarnings(PHPMD.StaticAccess)
47 35
     */
48
    public function convertToPHPValue($value, AbstractPlatform $platform)
49 35
    {
50
        $result = null;
51 35
52 35
        if ($value !== null && ($dateTimeOrNull = parent::convertToPHPValue($value, $platform)) !== null) {
53
            assert($dateTimeOrNull instanceof DateTimeInterface);
54 35
            // despite the name it's not null already
55
            $result = DateTime::createFromDateTime($dateTimeOrNull);
56
        }
57 35
58
        return $result;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     *
64
     * @throws ConversionException
65 14
     */
66
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
67 14
    {
68 14
        return parent::convertToDatabaseValue(
69 12
            $this->convertToDateTimeFromString($value, $platform->getDateTimeFormatString(), static::NAME),
70
            $platform
71
        );
72
    }
73
}
74