Kohana_Jam_Timezone::time()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
/**
3
 * Core class that all fields must extend
4
 *
5
 * @package    Jam
6
 * @category   Fields
7
 * @author     Ivan Kerin
8
 * @copyright  (c) 2011-2012 Despark Ltd.
9
 * @license    http://www.opensource.org/licenses/isc-license.txt
10
 */
11
abstract class Kohana_Jam_Timezone {
12
13
	const DEFAULT_TIMEZONE = 'default_timezone';
14
	const MASTER_TIMEZONE = 'master_timezone';
15
	const USER_TIMEZONE = 'user_timezone';
16
17
	protected static $_instance;
18
19 6
	public static function instance()
20
	{
21 6
		if ( ! Jam_Timezone::$_instance)
0 ignored issues
show
Bug introduced by
The property _instance cannot be accessed from this context as it is declared protected in class Kohana_Jam_Timezone.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
22
		{
23
			Jam_Timezone::$_instance = new Jam_Timezone();
0 ignored issues
show
Bug introduced by
The property _instance cannot be accessed from this context as it is declared protected in class Kohana_Jam_Timezone.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
24
		}
25 6
		return Jam_Timezone::$_instance;
0 ignored issues
show
Bug introduced by
The property _instance cannot be accessed from this context as it is declared protected in class Kohana_Jam_Timezone.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
26
	}
27
28 5
	public static function shift($value, DateTimeZone $timezone_from, DateTimeZone $timezone_to)
29
	{
30 5
		$time = new DateTime();
31 5
		$time->setTimestamp($value);
32
33 5
		return $value + $timezone_to->getOffset($time) - $timezone_from->getOffset($time);
34
	}
35
36
	protected $_user_timezone = NULL;
37
	protected $_master_timezone = NULL;
38
	protected $_default_timezone = NULL;
39
40 3
	public function master_timezone($timezone = NULL)
41
	{
42 3
		if ($timezone !== NULL)
43
		{
44
			$this->_master_timezone = new DateTimeZone($timezone);
45
			return $this;
46
		}
47
48 3
		if ( ! $this->_master_timezone)
49
		{
50 3
			$this->_master_timezone = new DateTimeZone('UTC');
51
		}
52
53 3
		return $this->_master_timezone;
54
	}
55
56 7
	public function default_timezone($timezone = NULL)
57
	{
58 7
		if ($timezone !== NULL)
59
		{
60 7
			$this->_default_timezone = new DateTimeZone($timezone);
61 7
			return $this;
62
		}
63
64 2
		if ( ! $this->_default_timezone)
65
		{
66 1
			$this->_default_timezone = new DateTimeZone(date_default_timezone_get());
67
		}
68
69 2
		return $this->_default_timezone;
70
	}
71
72 7
	public function user_timezone($timezone = NULL)
73
	{
74 7
		if ($timezone !== NULL)
75
		{
76 7
			$this->_user_timezone = new DateTimeZone($timezone);
77 7
			return $this;
78
		}
79
80 1
		return $this->_user_timezone;
81
	}
82
83 8
	public function is_active()
84
	{
85 8
		return $this->_user_timezone !== NULL;
86
	}
87
88 1
	public function date($format, $time = NULL)
89
	{
90 1
		if ( ! $this->is_active())
91 1
			return ($time !== NULL) ? date($format, $time) : date($format);
92
93 1
		$time = ($time !== NULL) ? $time : time();
94 1
		$time = Jam_Timezone::shift($time, $this->default_timezone(), $this->master_timezone());
95
96 1
		return date($format, $time);
97
	}
98
99 3
	public function time()
100
	{
101 3
		if ( ! $this->is_active())
102 3
			return time();
103
104
		return Jam_Timezone::shift(time(), $this->default_timezone(), $this->master_timezone());
105
	}
106
107
	public function strtotime($time_string)
108
	{
109
		if ( ! $this->is_active())
110
			return strtotime($time_string);
111
112
		return Jam_Timezone::shift(strtotime($time_string), $this->default_timezone(), $this->master_timezone());
113
	}
114
115 6
	public function convert($value, $from, $to)
116
	{
117 6
		if ( ! $this->is_active())
118 5
			return $value;
119
120 1
		return Jam_Timezone::shift($value, $this->$from(), $this->$to());
121
	}
122
123
	public function to_db($value, $from = 'user_timezone')
124
	{
125
		if ( ! $this->is_active())
126
			return $value;
127
128
		if ( ! is_numeric($value))
129
		{
130
			$value = strtotime($value);
131
		}
132
133
		$value = $this->convert($value, $from, Jam_Timezone::MASTER_TIMEZONE);
134
		return date('Y-m-d H:i:s', $value);
135
	}
136
137
} // End Kohana_Jam_Field
138