Completed
Push — master ( 2e8b61...fce84e )
by Andrii
04:18
created

SettingsStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Bug introduced by
There is no parameter named $key. 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...
75
     * @param array $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. 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...
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