Passed
Push — master ( 6a9989...c85e47 )
by Jean-Christophe
10:21
created

SessionObject::isExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\utils\http\session;
4
5
class SessionObject {
6
	protected $value;
7
	protected $duration;
8
	protected $creationTime;
9
10 1
	public function __construct($value, $duration) {
11 1
		$this->value = $value;
12 1
		$this->duration = $duration;
13 1
		$this->creationTime = time ();
14 1
	}
15
16
	/**
17
	 *
18
	 * @return mixed
19
	 */
20 1
	public function getValue() {
21 1
		if (! $this->isExpired ())
22 1
			return $this->value;
23 1
		return;
24
	}
25
26
	/**
27
	 *
28
	 * @return mixed
29
	 */
30
	public function getDuration() {
31
		return $this->duration;
32
	}
33
34
	/**
35
	 *
36
	 * @return mixed
37
	 */
38
	public function getCreationTime() {
39
		return $this->creationTime;
40
	}
41
42
	/**
43
	 *
44
	 * @param mixed $value
45
	 */
46
	public function setValue($value) {
47
		if ($value !== $this->value)
48
			$this->creationTime = time ();
49
		return $this->value = $value;
50
	}
51
52
	/**
53
	 *
54
	 * @param mixed $duration
55
	 */
56
	public function setDuration($duration) {
57
		$this->duration = $duration;
58
	}
59
60
	/**
61
	 *
62
	 * @return boolean
63
	 */
64 1
	public function isExpired() {
65 1
		return \time () - $this->creationTime > $this->duration;
66
	}
67
68
	/**
69
	 *
70
	 * @return number
71
	 */
72
	public function getTimeout() {
73
		$timeout = $this->duration - (\time () - $this->creationTime);
74
		if ($timeout > 0)
75
			return $timeout;
76
		return 0;
77
	}
78
}
79
80