Completed
Push — master ( fc24bd...39caf5 )
by Sam
03:18
created

TimeFrame::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Jalle19\StatusManager;
4
5
/**
6
 * Represents a time frame, can be used to limit
7
 * @package   Jalle19\StatusManager
8
 * @copyright Copyright &copy; Sam Stenvall 2016-
9
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
10
 */
11
class TimeFrame implements \JsonSerializable
12
{
13
14
	const TIME_FRAME_ALL_TIME   = 'allTime';
15
	const TIME_FRAME_LAST_MONTH = 'lastMonth';
16
	const TIME_FRAME_LAST_WEEK  = 'lastWeek';
17
	const TIME_FRAME_LAST_DAY   = 'lastDay';
18
19
	/**
20
	 * @var array
21
	 */
22
	private static $_timeFrames = [
23
		self::TIME_FRAME_ALL_TIME,
24
		self::TIME_FRAME_LAST_MONTH,
25
		self::TIME_FRAME_LAST_WEEK,
26
		self::TIME_FRAME_LAST_DAY,
27
	];
28
29
	/**
30
	 * @var string
31
	 */
32
	private $_timeFrame;
33
34
35
	/**
36
	 * TimeFrame constructor.
37
	 *
38
	 * @param string $timeFrame
39
	 *
40
	 * @throws \InvalidArgumentException when an invalid time frame is specified
41
	 */
42 3
	public function __construct($timeFrame)
43
	{
44 3
		if (!in_array($timeFrame, self::$_timeFrames))
45 3
			throw new \InvalidArgumentException('Invalid time frame specified');
46
47 2
		$this->_timeFrame = $timeFrame;
48 2
	}
49
50
51
	/**
52
	 * @return string
53
	 */
54 1
	public function getType()
55
	{
56 1
		return $this->_timeFrame;
57
	}
58
59
60
	/**
61
	 * @return int
62
	 */
63 1
	public function getTimestamp()
64
	{
65 1
		$dateTime = new \DateTime();
66
67 1
		switch ($this->_timeFrame)
68
		{
69 1
			case self::TIME_FRAME_LAST_MONTH:
70 1
				$dateTime = $dateTime->modify('-1 month');
71 1
				break;
72 1
			case self::TIME_FRAME_LAST_WEEK:
73 1
				$dateTime = $dateTime->modify('-1 week');
74 1
				break;
75 1
			case self::TIME_FRAME_LAST_DAY:
76 1
				$dateTime = $dateTime->modify('-1 day');
77 1
				break;
78 1
		}
79
80 1
		return $dateTime->getTimestamp();
81
	}
82
83
84
	/**
85
	 * @inheritdoc
86
	 */
87
	public function jsonSerialize()
88
	{
89
		return $this->_timeFrame;
90
	}
91
92
}
93