Passed
Push — master ( f5da16...2e3bf1 )
by Jean-Christophe
12:28
created

ApiTokens::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
	}
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
		if (isset ( $this->tokens [$key] ))
49
			return $this->tokens [$key];
50
		return false;
51
	}
52
53
	public function isExpired($key) {
54
		$token = $this->getToken ( $key );
55
		if ($token !== false)
56
			return \time () - $token ['creationTime'] > $this->duration;
57
		return true;
58
	}
59
60
	public function addToken($datas=null) {
61
		$key = $this->generateToken ();
62
		$token=[ 'creationTime' => \time () ];
63
		if(isset($datas)){
64
			$token['datas']=$datas;
65
		}
66
		$this->tokens [$key] = $token;
67
		return $key;
68
	}
69
70
	public function clearAll() {
71
		$this->tokens = [ ];
72
	}
73
74
	public function removeExpireds() {
75
		$tokens = $this->tokens;
76
		foreach ( $tokens as $key => $value ) {
77
			if ($this->isExpired ( $key )) {
78
				unset ( $this->tokens [$key] );
79
			}
80
		}
81
	}
82
83
	public function remove($key) {
84
		if (isset ( $this->tokens [$key] )) {
85
			unset ( $this->tokens [$key] );
86
			return true;
87
		}
88
		return false;
89
	}
90
91
	public function storeToCache($key = '_apiTokens') {
92
		$fileContent = [ 'duration' => $this->duration,'length' => $this->length,'tokens' => $this->tokens ];
93
		self::$cache->store ( $key, $fileContent );
94
	}
95
96
	/**
97
	 *
98
	 * @param $folder
99
	 * @param string $key
100
	 * @return ApiTokens
101
	 */
102 1
	public function getFromCache($folder, $key = '_apiTokens') {
103 1
		if (! isset ( self::$cache )) {
104 1
			self::$cache = new ArrayCache ( $folder . 'rest/tokens', '.rest' );
105
		}
106 1
		if (self::$cache->exists ( $key )) {
107 1
			$filecontent = self::$cache->fetch ( $key );
108 1
			if (isset ( $filecontent ['tokens'] )) {
109 1
				$this->tokens = $filecontent ['tokens'];
110
			}
111 1
			if (isset ( $filecontent ['length'] )) {
112 1
				$this->length = $filecontent ['length'];
113
			}
114 1
			if (isset ( $filecontent ['duration'] )) {
115 1
				$this->duration = $filecontent ["duration"];
116
			}
117
		}
118 1
		return $this;
119
	}
120
}
121