1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\components; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\client\models\Client; |
6
|
|
|
use yii\base\Application; |
7
|
|
|
use yii\base\Component; |
8
|
|
|
use yii\di\Instance; |
9
|
|
|
|
10
|
|
|
class SettingsStorage extends Component implements SettingsStorageInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Application |
14
|
|
|
*/ |
15
|
|
|
private $app; |
16
|
|
|
|
17
|
|
|
public function __construct(Application $app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function setGlobal($key, $value) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->perform('set-settings-storage', array_merge([ |
28
|
|
|
'key' => $key, |
29
|
|
|
'data' => json_encode($value), |
30
|
|
|
'oauthClientBound' => false, |
31
|
|
|
])); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function setBounded($key, $value) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this->perform('set-settings-storage', array_merge([ |
40
|
|
|
'key' => $key, |
41
|
|
|
'data' => json_encode($value), |
42
|
|
|
'oauthClientBound' => true, |
43
|
|
|
])); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function getGlobal($key) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$response = $this->perform('get-settings-storage', array_merge([ |
52
|
|
|
'key' => $key, |
53
|
|
|
'oauthClientBound' => false, |
54
|
|
|
])); |
55
|
|
|
|
56
|
|
|
return $this->decodeResponse($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function getBounded($key) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$response = $this->perform('get-settings-storage', array_merge([ |
65
|
|
|
'key' => $key, |
66
|
|
|
'oauthClientBound' => true, |
67
|
|
|
])); |
68
|
|
|
|
69
|
|
|
return $this->decodeResponse($response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Performs request to the API |
74
|
|
|
* @param string $key |
|
|
|
|
75
|
|
|
* @param array $value |
|
|
|
|
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
private function perform($action, $data) |
79
|
|
|
{ |
80
|
|
|
if ($this->app->user->isGuest) { |
81
|
|
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return Client::perform($action, $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function decodeResponse($response) |
88
|
|
|
{ |
89
|
|
|
$result = json_decode(isset($response['data']) ? $response['data'] : '{}', true); |
90
|
|
|
|
91
|
|
|
return $result === null ? [] : $result; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.