Completed
Push — master ( 1f65a3...0feaa6 )
by Jean-Christophe
01:28
created

ApiTokens   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 99
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateToken() 0 6 2
A getTokens() 0 3 1
A getDuration() 0 3 1
A getToken() 0 5 2
A isExpired() 0 6 2
A addToken() 0 5 1
A clearAll() 0 3 1
A removeExpireds() 0 8 3
A remove() 0 7 2
A storeToCache() 0 4 1
B getFromCache() 0 20 6
1
<?php
2
3
namespace micro\controllers\rest;
4
5
use micro\cache\ArrayCache;
6
use micro\utils\JArray;
7
8
class ApiTokens {
9
	private $tokens;
10
	private $length;
11
	private $duration;
12
	private static $cache;
13
14
	public function __construct($length=78,$duration=3600,$tokens=[]){
15
		$this->length=$length;
16
		$this->duration=$duration;
17
		$this->tokens=$tokens;
18
	}
19
20
	protected function generateToken(){
21
		do{
22
			$token= \bin2hex(\random_bytes($this->length));
23
		}while (\array_search($token, $this->tokens,true)===true);
24
		return $token;
25
	}
26
27
	public function getTokens() {
28
		return $this->tokens;
29
	}
30
31
	public function getDuration() {
32
		return $this->duration;
33
	}
34
35
	public function getToken($key){
36
		if(isset($this->tokens[$key]))
37
			return $this->tokens[$key];
38
		return false;
39
	}
40
41
	public function isExpired($key){
42
		$token=$this->getToken($key);
43
		if($token!==false)
44
			return \time() - $token["creationTime"] > $this->duration;
45
		return true;
46
	}
47
48
	public function addToken(){
49
		$key=$this->generateToken();
50
		$this->tokens[$key]=["creationTime"=>\time()];
51
		return $key;
52
	}
53
54
	public function clearAll(){
55
		$this->tokens=[];
56
	}
57
58
	public function removeExpireds(){
59
		$tokens=$this->tokens;
60
		foreach ($tokens as $key=>$value){
61
			if($this->isExpired($key)){
62
				unset($this->tokens[$key]);
63
			}
64
		}
65
	}
66
67
	public function remove($key){
68
		if(isset($this->tokens[$key])){
69
			unset($this->tokens[$key]);
70
			return true;
71
		}
72
		return false;
73
	}
74
75
	public function storeToCache($key="_apiTokens"){
76
		$fileContent=["duration"=>$this->duration,"length"=>$this->length,"tokens"=>$this->tokens];
77
		self::$cache->store($key, "return " . JArray::asPhpArray($fileContent,"array").";");
78
	}
79
80
	/**
81
	 * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
	 * @param number $length
83
	 * @param number $duration
84
	 * @return ApiTokens
85
	 */
86
	public static function getFromCache($folder,$key="_apiTokens",$length=78,$duration=3600){
87
		if(!isset(self::$cache)){
88
			self::$cache=new ArrayCache($folder."rest/tokens",".rest");
89
		}
90
		$tokens=[];
91
		if(self::$cache->exists($key)){
92
			$filecontent=self::$cache->fetch($key);
93
			if(isset($filecontent["tokens"])){
94
				$tokens=$filecontent["tokens"];
95
			}
96
			if(isset($filecontent["length"])){
97
				$length=$filecontent["length"];
98
			}
99
			if(isset($filecontent["duration"])){
100
				$duration=$filecontent["duration"];
101
			}
102
		}
103
		$apiTokens=new ApiTokens($length,$duration,$tokens);
104
		return $apiTokens;
105
	}
106
}
107