Completed
Push — master ( bf102e...fafcea )
by Nazar
06:46
created

Key   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.21%

Importance

Changes 0
Metric Value
dl 0
loc 152
ccs 55
cts 56
cp 0.9821
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A del() 0 12 3
A generate() 0 20 4
B get() 0 30 6
C add() 0 43 8
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2017, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
10
/**
11
 * @method static $this instance($check = false)
12
 */
13
class Key {
14
	use
15
		Singleton;
16
	/**
17
	 * Generates guaranteed unique key
18
	 *
19
	 * @param int|\cs\DB\_Abstract $database Keys database
20
	 *
21
	 * @return string
22
	 *
23
	 * @throws ExitException
24
	 */
25 2
	public function generate ($database) {
26 2
		if (!is_object($database)) {
27 2
			$database = DB::instance()->db_prime($database);
28
		}
29 2
		while (true) {
30 2
			$key  = hash('sha224', random_bytes(1000));
31 2
			$time = time();
32 2
			if (!$database->qfs(
33
				"SELECT `id`
34
				FROM `[prefix]keys`
35
				WHERE
36 2
					`key`		= '$key' AND
37 2
					`expire`	>= $time
38
				LIMIT 1"
39
			)
40
			) {
41 2
				return $key;
42
			}
43
		}
44
	}
45
	/**
46
	 * Adding key into specified database
47
	 *
48
	 * @param int|\cs\DB\_Abstract $database Keys database
49
	 * @param bool|string          $key      If <b>false</b> - key will be generated automatically, otherwise must contain 56 character [0-9a-z] key
50
	 * @param null|mixed           $data     Data to be stored with key
51
	 * @param int                  $expire   Timestamp of key expiration, if not specified - default system value will be used
52
	 *
53
	 * @return false|string
54
	 *
55
	 * @throws ExitException
56
	 */
57 2
	public function add ($database, $key, $data = null, $expire = 0) {
58 2
		if (!is_object($database)) {
59 2
			$database = DB::instance()->db_prime($database);
60
		}
61 2
		if ($key === false) {
62 2
			$key = $this->generate($database);
63 2
		} elseif (!preg_match('/^[a-z0-9]{56}$/', $key)) {
64 2
			return false;
65
		}
66 2
		$expire = (int)$expire;
67 2
		$Config = Config::instance();
68 2
		$time   = time();
69 2
		if ($expire == 0 || $expire < $time) {
70 2
			$expire = $time + $Config->core['key_expire'];
71
		}
72 2
		$this->del($database, $key);
73 2
		$database->q(
74 2
			"INSERT INTO `[prefix]keys`
75
				(
76
					`key`,
77
					`expire`,
78
					`data`
79
				) VALUES (
80
					'%s',
81
					'%s',
82
					'%s'
83
				)",
84 2
			$key,
85 2
			$expire,
86 2
			_json_encode($data)
87
		);
88 2
		$id = $database->id();
89
		/**
90
		 * Cleaning old keys
91
		 */
92 2
		if ($id && ($id % $Config->core['inserts_limit']) == 0) {
93 2
			$database->q(
94
				"DELETE FROM `[prefix]keys`
95 2
				WHERE `expire` < $time"
96
			);
97
		}
98 2
		return $key;
99
	}
100
	/**
101
	 * Check key existence and/or getting of data stored with key. After this key will be deleted automatically.
102
	 *
103
	 * @param int|\cs\DB\_Abstract $database Keys database
104
	 * @param string               $key      56 character [0-9a-z] key
105
	 * @param bool                 $get_data If <b>true</d> - stored data will be returned on success, otherwise boolean result of key existence will be
106
	 *                                       returned
107
	 *
108
	 * @return bool|mixed
109
	 *
110
	 * @throws ExitException
111
	 */
112 2
	public function get ($database, $key, $get_data = false) {
113 2
		if (!preg_match('/^[a-z0-9]{56}$/', $key)) {
114 2
			return false;
115
		}
116 2
		if (!is_object($database)) {
117 2
			$database = DB::instance()->db_prime($database);
118
		}
119 2
		$time   = time();
120 2
		$result = $database->qf(
121
			"SELECT
122
				`id`,
123
				`data`
124
			FROM `[prefix]keys`
125
			WHERE
126
				(
127 2
					`key`	= '$key'
128
				) AND
129 2
				`expire` >= $time
130
			ORDER BY `id` DESC
131
			LIMIT 1"
132
		);
133 2
		$this->del($database, $key);
134 2
		if (!$result || !is_array($result)) {
135 2
			return false;
136 2
		} elseif ($get_data) {
137 2
			return _json_decode($result['data']);
138
		} else {
139 2
			return true;
140
		}
141
	}
142
	/**
143
	 * Key deletion from database
144
	 *
145
	 * @param int|\cs\DB\_Abstract $database Keys database
146
	 * @param string               $key      56 character [0-9a-z] key
147
	 *
148
	 * @return bool
149
	 *
150
	 * @throws ExitException
151
	 */
152 2
	public function del ($database, $key) {
153 2
		if (!preg_match('/^[a-z0-9]{56}$/', $key)) {
154 2
			return false;
155
		}
156 2
		if (!is_object($database)) {
157 2
			$database = DB::instance()->db_prime($database);
158
		}
159 2
		return (bool)$database->q(
160
			"DELETE FROM `[prefix]keys`
161 2
			WHERE `key`	= '$key'"
162
		);
163
	}
164
}
165