Test Failed
Push — master ( 696af1...07df9d )
by Jean-Christophe
09:00
created

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