Failed Conditions
Push — experimental/3.1 ( d21b3a...70c10e )
by Kiyotaka
22s
created

UTCDateTimeTzType::setTimeZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Doctrine\DBAL\Types;
25
26
use Doctrine\DBAL\Platforms\AbstractPlatform;
27
use Doctrine\DBAL\Types\ConversionException;
28
use Doctrine\DBAL\Types\DateTimeType;
29
use Doctrine\DBAL\Types\DateTimeTzType;
30
31 View Code Duplication
class UTCDateTimeTzType extends DateTimeTzType
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...
introduced by
Missing class doc comment
Loading history...
32
{
33
    /**
34
     * UTCのタイムゾーン
35
     *
36
     * @var \DateTimeZone
37
     */
38
    static protected $utc;
39
40
    /**
41
     * アプリケーションのタイムゾーン
42
     *
43
     * @var \DateTimeZone
44
     */
45
    static protected $timezone;
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
51
    {
52
        if ($value instanceof \DateTime) {
53
            $value->setTimezone(self::getUtcTimeZone());
54
        }
55
56
        return parent::convertToDatabaseValue($value, $platform);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function convertToPHPValue($value, AbstractPlatform $platform)
63
    {
64
        if ($value === null || $value instanceof \DateTime) {
65
            return $value;
66
        }
67
68
        $converted = \DateTime::createFromFormat(
69
            $platform->getDateTimeTzFormatString(),
70
            $value,
71
            self::getUtcTimeZone()
72
        );
73
74
        if (!$converted) {
75
            throw ConversionException::conversionFailedFormat(
76
                $value,
77
                $this->getName(),
78
                $platform->getDateTimeTzFormatString()
79
            );
80
        }
81
82
        $converted->setTimezone(self::getTimezone());
83
84
        return $converted;
85
    }
86
87
    /**
88
     * @return \DateTimeZone
89
     */
90
    protected static function getUtcTimeZone()
91
    {
92
        if (is_null(self::$utc)) {
93
            self::$utc = new \DateTimeZone('UTC');
94
        }
95
96
        return self::$utc;
97
    }
98
99
    /**
100
     * @return \DateTimeZone
101
     */
102
    public static function getTimezone()
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
103
    {
104
        if (is_null(self::$timezone)) {
105
            throw new \LogicException(sprintf('%s::$timezone is undefined.', self::class));
106
        }
107
108
        return self::$timezone;
109
    }
110
111
    /**
112
     * @param string $timezone
113
     */
114
    public static function setTimeZone($timezone = 'Asia/Tokyo')
115
    {
116
        self::$timezone = new \DateTimeZone($timezone);
117
    }
118
}
119