date_range::get_year()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class date_range
13
{
14
	/** @var \phpbb\user */
15
	protected $user;
16
17
	/** @var string */
18
	protected $time;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \phpbb\user		$user		User object
24
	 * @param string			$time		String in a format accepted by strtotime().
25
	 */
26 27
	public function __construct(\phpbb\user $user, $time = 'now')
27
	{
28 27
		$this->user = $user;
29 27
		$this->time = $time;
30 27
	}
31
32
	/**
33
	 * @param string $range Date range to get (today, week, month, year)
34
	 * @return array
35
	 */
36 20
	public function get($range)
37
	{
38 20
		$time = $this->user->create_datetime($this->time);
39 20
		$now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset());
40
41 20
		$method = 'get_' . $range;
42
		$data = array(
43 20
			'start'	=> 0,
44 20
			'stop'	=> 0,
45 20
			'date'	=> '',
46 20
		);
47
48 20
		if (is_callable(array($this, $method)))
49 20
		{
50 7
			$data = call_user_func_array(array($this, $method), array($now));
51 7
		}
52
53 20
		return $data;
54
	}
55
56
	/**
57
	 * @param array $now
58
	 * @return array
59
	 */
60 3
	public function get_today(array $now)
61
	{
62 3
		$start = $this->user->create_datetime()
63 3
			->setDate($now['year'], $now['mon'], $now['mday'])
64 3
			->setTime(0, 0, 0)
65 3
			->getTimestamp();
66
67
		return array(
68 3
			'start'	=> $start,
69 3
			'stop'	=> $start + 86399,
70 3
			'date'	=> $this->user->format_date($start, 'Y-m-d', true),
71 3
		);
72
	}
73
74
	/**
75
	 * @param array $now
76
	 * @return array
77
	 */
78 1
	public function get_week(array $now)
79
	{
80 1
		$info = getdate($now[0] - (86400 * $now['wday']));
81 1
		$start = $this->user->create_datetime()
82 1
			->setDate($info['year'], $info['mon'], $info['mday'])
83 1
			->setTime(0, 0, 0)
84 1
			->getTimestamp();
85
86
		return array(
87 1
			'start'	=> $start,
88 1
			'stop'	=> $start + 604799,
89 1
			'date'	=> $this->user->format_date($start, 'Y-m-d', true),
90 1
		);
91
	}
92
93
	/**
94
	 * @param array $now
95
	 * @return array
96
	 */
97 2
	public function get_month(array $now)
98
	{
99 2
		$start = $this->user->create_datetime()
100 2
			->setDate($now['year'], $now['mon'], 1)
101 2
			->setTime(0, 0, 0)
102 2
			->getTimestamp();
103 2
		$num_days = gmdate('t', $start);
104
105
		return array(
106 2
			'start'	=> $start,
107 2
			'stop'	=> $start + (86400 * $num_days) - 1,
108 2
			'date'	=> $this->user->format_date($start, 'Y-m', true),
109 2
		);
110
	}
111
112
	/**
113
	 * @param array $now
114
	 * @return array
115
	 */
116 1
	public function get_year(array $now)
117
	{
118 1
		$start = $this->user->create_datetime()
119 1
			->setDate($now['year'], 1, 1)
120 1
			->setTime(0, 0, 0)
121 1
			->getTimestamp();
122 1
		$leap_year = gmdate('L', $start);
123 1
		$num_days = ($leap_year) ? 366 : 365;
124
125
		return array(
126 1
			'start'	=> $start,
127 1
			'stop'	=> $start + (86400 * $num_days) - 1,
128 1
			'date'	=> $this->user->format_date($start, 'Y', true),
129 1
		);
130
	}
131
}
132