Completed
Push — 4.0 ( 87d096...bcc1be )
by Kiyotaka
05:44 queued 11s
created

Eccube/Doctrine/DBAL/Types/UTCDateTimeTzType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Doctrine\DBAL\Types;
15
16
use Doctrine\DBAL\Platforms\AbstractPlatform;
17
use Doctrine\DBAL\Types\ConversionException;
18
use Doctrine\DBAL\Types\DateTimeTzType;
19
20 View Code Duplication
class UTCDateTimeTzType extends DateTimeTzType
0 ignored issues
show
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...
21
{
22
    /**
23
     * UTCのタイムゾーン
24
     *
25
     * @var \DateTimeZone
26
     */
27
    protected static $utc;
28
29
    /**
30
     * アプリケーションのタイムゾーン
31
     *
32
     * @var \DateTimeZone
33
     */
34
    protected static $timezone;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 794
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
40
    {
41 794
        if ($value instanceof \DateTime) {
42 794
            $value->setTimezone(self::getUtcTimeZone());
43
        }
44
45 794
        return parent::convertToDatabaseValue($value, $platform);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 905
    public function convertToPHPValue($value, AbstractPlatform $platform)
52
    {
53 905
        if ($value === null || $value instanceof \DateTime) {
54 500
            return $value;
55
        }
56
57 905
        $converted = \DateTime::createFromFormat(
58 905
            $platform->getDateTimeTzFormatString(),
59 905
            $value,
60 905
            self::getUtcTimeZone()
61
        );
62
63 905
        if (!$converted) {
64
            throw ConversionException::conversionFailedFormat(
65
                $value,
66
                $this->getName(),
67
                $platform->getDateTimeTzFormatString()
68
            );
69
        }
70
71 905
        $converted->setTimezone(self::getTimezone());
72
73 905
        return $converted;
74
    }
75
76
    /**
77
     * @return \DateTimeZone
78
     */
79 909
    protected static function getUtcTimeZone()
80
    {
81 909
        if (is_null(self::$utc)) {
82 1
            self::$utc = new \DateTimeZone('UTC');
83
        }
84
85 909
        return self::$utc;
86
    }
87
88
    /**
89
     * @return \DateTimeZone
90
     */
91 905
    public static function getTimezone()
92
    {
93 905
        if (is_null(self::$timezone)) {
94
            throw new \LogicException(sprintf('%s::$timezone is undefined.', self::class));
95
        }
96
97 905
        return self::$timezone;
98
    }
99
100
    /**
101
     * @param string $timezone
102
     */
103 1332
    public static function setTimeZone($timezone = 'Asia/Tokyo')
104
    {
105 1332
        self::$timezone = new \DateTimeZone($timezone);
106
    }
107
}
108