Completed
Push — master ( 01e3d1...48bdd7 )
by Dmitry
04:56
created

SettingsStorage::setGlobal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 8
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace hipanel\components;
4
5
use hiqdev\hiart\Connection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\components\Connection.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use yii\base\Component;
7
use yii\di\Instance;
8
9
class SettingsStorage extends Component implements SettingsStorageInterface
10
{
11
    /**
12
     * @var Connection
13
     */
14
    private $connection;
15
16
    public function init()
17
    {
18
        $this->connection = Instance::ensure('hiart', Connection::class);
19
    }
20
21
    /**
22
     * @inheritdoc
23
     */
24 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...
25
    {
26
        $this->perform('clientSetSettingsStorage', array_merge([
27
            'key' => $key,
28
            'data' => json_encode($value),
29
            'oauthClientBound' => false
30
        ]));
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 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...
37
    {
38
        $this->perform('clientSetSettingsStorage', array_merge([
39
            'key' => $key,
40
            'data' => json_encode($value),
41
            'oauthClientBound' => true
42
        ]));
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 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...
49
    {
50
        $response = $this->perform('clientGetSettingsStorage', array_merge([
51
            'key' => $key,
52
            'oauthClientBound' => false
53
        ]));
54
55
        return json_decode($response['data'], true);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 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...
62
    {
63
        $response = $this->perform('clientGetSettingsStorage', array_merge([
64
            'key' => $key,
65
            'oauthClientBound' => true
66
        ]));
67
68
        return json_decode($response['data'], true);
69
    }
70
71
    /**
72
     * Performs request to the API
73
     *
74
     * @param string $key
75
     * @param array $value
76
     * @return array
77
     */
78
    private function perform($key, $value)
79
    {
80
        return $this->connection->createCommand()->perform($key, $value);
81
    }
82
}
83