UTCDateTimeType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 1
b 0
f 0
dl 0
loc 67
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHPValue() 0 15 4
A convertToDatabaseValue() 0 7 2
A checkConvertedValue() 0 10 2
A getUtcDateTimeZone() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Doctrine/DBAL/Types/UTCDateTimeType.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Doctrine\DBAL\Types;
10
11
use DateTime;
12
use DateTimeInterface;
13
use DateTimeZone;
14
use Doctrine\DBAL\Platforms\AbstractPlatform;
15
use Doctrine\DBAL\Types\ConversionException;
16
use Doctrine\DBAL\Types\DateTimeType;
17
18
/**
19
 * Class UTCDateTimeType
20
 *
21
 * @see http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html
22
 *
23
 * @package App\Doctrine\DBAL\Types
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class UTCDateTimeType extends DateTimeType
27
{
28
    private static ?DateTimeZone $utc = null;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws ConversionException
34
     */
35 2
    public function convertToDatabaseValue($value, AbstractPlatform $platform): string
36
    {
37 2
        if ($value instanceof DateTime) {
38 2
            $value->setTimezone($this->getUtcDateTimeZone());
39
        }
40
41 2
        return (string)parent::convertToDatabaseValue($value, $platform);
42
    }
43
44
    /**
45
     * @param T $value
46
     *
47
     * @return (T is null ? null : DateTimeInterface)
0 ignored issues
show
Documentation Bug introduced by
The doc comment (T at position 1 could not be parsed: Expected ')' at position 1, but found 'T'.
Loading history...
48
     *
49
     * @template T
50
     *
51
     * @throws ConversionException
52
     */
53 5
    public function convertToPHPValue($value, AbstractPlatform $platform): DateTimeInterface|null
54
    {
55 5
        if ($value instanceof DateTime) {
56 2
            $value->setTimezone($this->getUtcDateTimeZone());
57 3
        } elseif ($value !== null) {
58 3
            $converted = DateTime::createFromFormat(
59 3
                $platform->getDateTimeFormatString(),
60 3
                (string)$value,
61 3
                $this->getUtcDateTimeZone()
62 3
            );
63
64 3
            $value = $this->checkConvertedValue((string)$value, $platform, $converted !== false ? $converted : null);
65
        }
66
67 4
        return parent::convertToPHPValue($value, $platform);
68
    }
69
70
    /**
71
     * Method to initialize DateTimeZone as in UTC.
72
     */
73 7
    private function getUtcDateTimeZone(): DateTimeZone
74
    {
75 7
        return self::$utc ??= new DateTimeZone('UTC');
76
    }
77
78
    /**
79
     * Method to check if conversion was successfully or not.
80
     *
81
     * @throws ConversionException
82
     */
83 3
    private function checkConvertedValue(string $value, AbstractPlatform $platform, ?DateTime $converted): DateTime
84
    {
85 3
        if ($converted instanceof DateTime) {
86 2
            return $converted;
87
        }
88
89 1
        throw ConversionException::conversionFailedFormat(
90 1
            $value,
91 1
            $this->getName(),
92 1
            $platform->getDateTimeFormatString()
93 1
        );
94
    }
95
}
96