1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FreedomCore\VK\API; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use FreedomCore\VK\VKBase; |
7
|
|
|
use FreedomCore\VK\VKException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class VKDataStorage |
11
|
|
|
* @package FreedomCore\VK |
12
|
|
|
*/ |
13
|
|
|
class VKDataStorage extends VKAPI { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* API Method for this class |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $apiMethod = 'storage.'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* VKDataStorage constructor. |
23
|
|
|
* @param VKBase $vkObject |
24
|
|
|
*/ |
25
|
|
|
public function __construct(VKBase $vkObject) { |
26
|
|
|
parent::__construct($vkObject); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns a value of variable with the name set by key parameter |
31
|
|
|
* @param string $storageKey |
32
|
|
|
* @param string $storageKeys |
33
|
|
|
* @param int $userID |
34
|
|
|
* @param int $isGlobal |
35
|
|
|
* @throws VKException |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function get($storageKey, $storageKeys, $userID, $isGlobal) { |
|
|
|
|
39
|
|
|
parent::isAllowed(); |
|
|
|
|
40
|
|
|
$requestParameters = [ |
41
|
|
|
'key' => substr($storageKey, 0, 100), |
42
|
|
|
'keys' => $storageKeys, |
43
|
|
|
'user_id' => $userID, |
44
|
|
|
'global' => ($isGlobal > 1 || $isGlobal < 0) ? 0 : $isGlobal |
45
|
|
|
|
46
|
|
|
]; |
47
|
|
|
return parent::executeQuery(__FUNCTION__, $requestParameters); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Saves a value of variable with the name set by key parameter |
52
|
|
|
* @param string $storageKey |
53
|
|
|
* @param string $keyValue |
54
|
|
|
* @param int $userID |
55
|
|
|
* @param int $isGlobal |
56
|
|
|
* @throws VKException |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function set($storageKey, $keyValue, $userID, $isGlobal) { |
60
|
|
|
parent::isAllowed(); |
|
|
|
|
61
|
|
|
$requestParameters = [ |
62
|
|
|
'key' => substr($storageKey, 0, 100), |
63
|
|
|
'value' => $keyValue, |
64
|
|
|
'user_id' => $userID, |
65
|
|
|
'global' => ($isGlobal > 1 || $isGlobal < 0) ? 0 : $isGlobal |
66
|
|
|
|
67
|
|
|
]; |
68
|
|
|
return parent::executeQuery(__FUNCTION__, $requestParameters); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the names of all variables |
73
|
|
|
* @param int $userID |
74
|
|
|
* @param int $isGlobal |
75
|
|
|
* @param int $setOffset |
76
|
|
|
* @param int $returnResult |
77
|
|
|
* @throws VKException |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getKeys($userID, $isGlobal, $setOffset = 0, $returnResult = 100) { |
81
|
|
|
parent::isAllowed(); |
|
|
|
|
82
|
|
|
$requestParameters = [ |
83
|
|
|
'user_id' => $userID, |
84
|
|
|
'global' => ($isGlobal > 1 || $isGlobal < 0) ? 0 : $isGlobal, |
85
|
|
|
'offset' => $setOffset, |
86
|
|
|
'count' => ($returnResult > 1000 || $returnResult < 0) ? 100 : $returnResult, |
87
|
|
|
|
88
|
|
|
]; |
89
|
|
|
return parent::executeQuery(__FUNCTION__, $requestParameters); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.