Passed
Pull Request — master (#197)
by
unknown
27:39
created

ApiTokens::addToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ubiquity\controllers\rest;
4
5
use Ubiquity\cache\system\ArrayCache;
6
7
/**
8
 * Manage the token api for the Rest part.
9
 * Ubiquity\controllers\rest$ApiTokens
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.2
14
 *
15
 */
16
class ApiTokens {
17
	protected $tokens;
18
	protected $length;
19
	protected $duration;
20
	protected static $cache;
21
22 1
	public function __construct($length = 10, $duration = 3600, $tokens = [ ]) {
23 1
		$this->length = $length ?? 10;
24 1
		$this->duration = $duration ?? 3600;
25 1
		$this->tokens = $tokens;
26
	}
27
28
	protected function generateToken() {
29
		do {
30
			$token = $this->tokenGenerator ();
31
		} while ( \array_search ( $token, $this->tokens, true ) === true );
32
		return $token;
33
	}
34
35
	protected function tokenGenerator() {
36
		return \bin2hex ( \random_bytes ( $this->length ) );
37
	}
38
39
	public function getTokens() {
40
		return $this->tokens;
41
	}
42
43
	public function getDuration() {
44
		return $this->duration;
45
	}
46
47
	public function getToken($key) {
48
		return $this->tokens [$key]??false;
49
	}
50
51
	public function isExpired($key) {
52
		$token = $this->getToken ( $key );
53
		if ($token !== false) {
54
			return \time() - $token ['creationTime'] > $this->duration;
55
		}
56
		return true;
57
	}
58
59
	public function addToken($datas=null) {
60
		$key = $this->generateToken ();
61
		$token=[ 'creationTime' => \time () ];
62
		if(isset($datas)){
63
			$token['datas']=$datas;
64
		}
65
		$this->tokens [$key] = $token;
66
		return $key;
67
	}
68
69
	/**
70
	 * Refresh the token using an existing one.
71
	 * @param string $key
72
	 * @return false|string
73
	 */
74
	public function refreshToken(string $key) {
75
		if($token=$this->getToken($key)){
76
			$token=$this->addToken($token['datas']??null);
77
			$this->remove($key);
78
			return $token;
79
		}
80
		return false;
81
	}
82
83
	public function clearAll() {
84
		$this->tokens = [ ];
85
	}
86
87
	public function removeExpireds() {
88
		$tokens = $this->tokens;
89
		foreach ( $tokens as $key => $value ) {
90
			if ($this->isExpired ( $key )) {
91
				unset ( $this->tokens [$key] );
92
			}
93
		}
94
	}
95
96
	public function remove($key) {
97
		if (isset ( $this->tokens [$key] )) {
98
			unset ( $this->tokens [$key] );
99
			return true;
100
		}
101
		return false;
102
	}
103
104
	public function storeToCache($key = '_apiTokens') {
105
		$fileContent = [ 'duration' => $this->duration,'length' => $this->length,'tokens' => $this->tokens ];
106
		self::$cache->store ( $key, $fileContent );
107
	}
108
109
	/**
110
	 *
111
	 * @param $folder
112
	 * @param string $key
113
	 * @return ApiTokens
114
	 */
115 1
	public function getFromCache($folder, $key = '_apiTokens') {
116 1
		if (! isset ( self::$cache )) {
117 1
			self::$cache = new ArrayCache ( $folder . 'rest/tokens', '.rest' );
118
		}
119 1
		if (self::$cache->exists ( $key )) {
120 1
			$filecontent = self::$cache->fetch ( $key );
121 1
			if (isset ( $filecontent ['tokens'] )) {
122 1
				$this->tokens = $filecontent ['tokens'];
123
			}
124 1
			if (isset ( $filecontent ['length'] )) {
125 1
				$this->length = $filecontent ['length'];
126
			}
127 1
			if (isset ( $filecontent ['duration'] )) {
128 1
				$this->duration = $filecontent ["duration"];
129
			}
130
		}
131 1
		return $this;
132
	}
133
}
134