Completed
Push — master ( dcbdba...73e86c )
by Tarmo
16s queued 12s
created

UTCDateTimeType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

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