Passed
Push — master ( 7d7894...4545fa )
by Jan
03:41
created

UTCDateTimeType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToDatabaseValue() 0 11 3
A convertToPHPValue() 0 21 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
 */
24
25
namespace App\Helpers;
26
27
use DateTime;
28
use DateTimeZone;
29
use Doctrine\DBAL\Platforms\AbstractPlatform;
30
use Doctrine\DBAL\Types\ConversionException;
31
use Doctrine\DBAL\Types\DateTimeType;
32
33
/**
34
 * This DateTimeType all dates to UTC, so it can be later used with the timezones.
35
 * Taken from here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/working-with-datetime.html.
36
 */
37
class UTCDateTimeType extends DateTimeType
38
{
39
    private static $utc_timezone;
40
41
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
42
    {
43
        if (!self::$utc_timezone) {
44
            self::$utc_timezone = new DateTimeZone('UTC');
45
        }
46
47
        if ($value instanceof DateTime) {
48
            $value->setTimezone(self::$utc_timezone);
49
        }
50
51
        return parent::convertToDatabaseValue($value, $platform);
52
    }
53
54
    public function convertToPHPValue($value, AbstractPlatform $platform)
55
    {
56
        if (!self::$utc_timezone) {
57
            self::$utc_timezone = new DateTimeZone('UTC');
58
        }
59
60
        if (null === $value || $value instanceof DateTime) {
61
            return $value;
62
        }
63
64
        $converted = DateTime::createFromFormat(
65
            $platform->getDateTimeFormatString(),
66
            $value,
67
            self::$utc_timezone
68
        );
69
70
        if (!$converted) {
71
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
72
        }
73
74
        return $converted;
75
    }
76
}