AbstractAuthtokens::setSelector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
namespace Ubiquity\security\auth\models;
3
4
/**
5
 * Ubiquity\security\auth\models$AbstractAuthtokens
6
 * This class is part of Ubiquity
7
 *
8
 * @author jc
9
 * @version 1.0.0
10
 *
11
 */
12
abstract class AbstractAuthtokens {
13
14
	/**
15
	 *
16
	 * @id
17
	 * @column("name"=>"id","nullable"=>false,"dbType"=>"int(11)")
18
	 * @validator("id","constraints"=>array("autoinc"=>true))
19
	 */
20
	protected $id;
21
22
	/**
23
	 *
24
	 * @column("name"=>"selector","nullable"=>false,"dbType"=>"char(24)")
25
	 * @validator("length","constraints"=>array("max"=>24,"notNull"=>true))
26
	 */
27
	protected $selector;
28
29
	/**
30
	 *
31
	 * @column("name"=>"hashedValidator","nullable"=>false,"dbType"=>"char(64)")
32
	 * @validator("length","constraints"=>array("max"=>64,"notNull"=>true))
33
	 */
34
	protected $hashedValidator;
35
36
	/**
37
	 *
38
	 * @column("name"=>"userid","nullable"=>false,"dbType"=>"int(11)")
39
	 * @validator("notNull")
40
	 */
41
	protected $userid;
42
43
	/**
44
	 *
45
	 * @column("name"=>"expires","nullable"=>true,"dbType"=>"datetime")
46
	 * @validator("type","dateTime")
47
	 * @transformer("datetime")
48
	 */
49
	protected $expires;
50
51
	public function __construct() {
52
		$this->selector = uniqid('', true);
53
		$this->hashedValidator = \bin2hex(\random_bytes(20));
54
		$this->setDuration('+1 day');
55
	}
56
57
	public function getId() {
58
		return $this->id;
59
	}
60
61
	public function setId($id) {
62
		$this->id = $id;
63
	}
64
65
	public function getSelector() {
66
		return $this->selector;
67
	}
68
69
	public function setSelector($selector) {
70
		$this->selector = $selector;
71
	}
72
73
	public function getHashedValidator() {
74
		return $this->hashedValidator;
75
	}
76
77
	public function setHashedValidator($hashedValidator) {
78
		$this->hashedValidator = $hashedValidator;
79
	}
80
81
	public function getUserid() {
82
		return $this->userid;
83
	}
84
85
	public function setUserid($userid) {
86
		$this->userid = $userid;
87
	}
88
89
	public function getExpires() {
90
		return $this->expires;
91
	}
92
93
	public function setExpires($expires) {
94
		$this->expires = $expires;
95
	}
96
97
	public function setDuration($duration) {
98
		$d = new \DateTime();
99
		$this->expires = $d->modify($duration);
100
	}
101
102
	public function isExpired() {
103
		return $this->expires->getTimestamp() < (new \DateTime())->getTimestamp();
104
	}
105
106
	public function checkValidator($validator) {
107
		return \hash_equals($this->hashedValidator, $validator);
108
	}
109
110
	/**
111
	 *
112
	 * @return mixed
113
	 */
114
	abstract public function getUser();
115
116
	/**
117
	 *
118
	 * @param mixed $user
119
	 */
120
	abstract public function setUser($user);
121
122
	public function __toString() {
123
		return $this->selector . ':' . $this->hashedValidator;
124
	}
125
}