Failed Conditions
Push — experimental/3.1 ( b8586b...76ccda )
by chihiro
19s
created

TimeZoneSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
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
25
namespace Eccube\Doctrine\EventSubscriber;
26
27
use Doctrine\Common\EventSubscriber;
28
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
29
use Doctrine\ORM\Events;
30
use Eccube\Application;
31
32
class TimeZoneSubscriber implements EventSubscriber
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    /**
35
     * @var Application
36
     */
37
    protected $app;
38
39
    /**
40
     * @var
41
     */
42
    protected static $timezone;
43
44
    /**
45
     * @param Application $app
46
     */
47
    public function __construct(Application $app)
48
    {
49
        $this->app = $app;
50
    }
51
52
    public function getSubscribedEvents()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
53
    {
54
        return array(
55
            Events::postLoad,
56
        );
57
    }
58
59
    public function postLoad(LifecycleEventArgs $args)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
60
    {
61
        $entity = $args->getObject();
62
63
        $refl = new \ReflectionObject($entity);
64
        foreach ($refl->getProperties() as $prop) {
65
            if (!$prop->isStatic()) {
66
                $prop->setAccessible(true);
67
                $value = $prop->getValue($entity);
68
                if (!is_null($value) && $value instanceof \DateTime) {
69
                    $timezone = is_null(self::$timezone)
70
                        ? self::$timezone = new \DateTimeZone($this->app['config']['timezone'])
71
                        : self::$timezone;
72
                    $value->setTimezone($timezone);
73
                }
74
            }
75
        }
76
    }
77
}
78