UiOptionsStorage::getOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\components;
12
13
use hiqdev\thememanager\models\OrientationInterface;
14
use Yii;
15
use yii\base\Component;
16
17
/**
18
 * Class UiOptionsStorage.
19
 */
20
class UiOptionsStorage extends Component implements OrientationInterface
21
{
22
    /**
23
     * @var string the cache key that will be used to cache storage values
24
     */
25
    public $cacheKey;
26
27
    /**
28
     * @var string
29
     */
30
    public $settingsStorageKey = 'uiOptions';
31
32
    /**
33
     * @var array
34
     */
35
    protected $storage;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function init()
41
    {
42
        if (!isset($this->cacheKey)) {
43
            $this->cacheKey = ['uiOptionsStorage', Yii::$app->user->id];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('uiOptionsStorage', \Yii::$app->user->id) of type array<integer,?,{"0":"string","1":"?"}> is incompatible with the declared type string of property $cacheKey.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        }
45
    }
46
47
    /**
48
     * @return \yii\caching\Cache
49
     */
50
    protected function getCache()
51
    {
52
        return Yii::$app->getCache();
53
    }
54
55
    /**
56
     * @return SettingsStorageInterface
57
     */
58
    protected function getSettingsStorage()
59
    {
60
        return Yii::$app->get('settingsStorage');
61
    }
62
63
    /**
64
     * Ensures that [[storage]] contains actual orientations storage.
65
     *
66
     * @return array
67
     */
68 View Code Duplication
    private function ensureStorage()
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...
69
    {
70
        if (!isset($this->storage)) {
71
            $this->storage = $this->getCache()->get($this->cacheKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getCache()->get($this->cacheKey) of type * is incompatible with the declared type array of property $storage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
73
            if ($this->storage === false) {
74
                $this->storage = $this->getSettingsStorage()->getBounded($this->settingsStorageKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getSettingsStorag...is->settingsStorageKey) of type * is incompatible with the declared type array of property $storage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
                $this->cache();
76
            }
77
        }
78
79
        return $this->storage;
80
    }
81
82
    /**
83
     * Saves current [[storage]].
84
     */
85
    protected function saveStorage()
86
    {
87
        $this->getSettingsStorage()->setBounded($this->settingsStorageKey, $this->storage);
88
        $this->cache();
89
    }
90
91
    /**
92
     * Caches current [[storage]].
93
     */
94
    protected function cache()
95
    {
96
        $this->getCache()->set($this->cacheKey, $this->storage, 86400); // 1 day
97
    }
98
99
    /**
100
     * Sets orientation for the $route.
101
     *
102
     * @param string $route
103
     * @param string $orientation
0 ignored issues
show
Bug introduced by
There is no parameter named $orientation. 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...
104
     */
105
    public function set($route, $options = [])
106
    {
107
        $this->ensureStorage();
108
109
        $this->storage[$route] = $options;
110
        $this->saveStorage();
111
    }
112
113
    /**
114
     * Gets orientation for the $route.
115
     *
116
     * @param $route
117
     * @return string
118
     */
119
    public function get($route)
120
    {
121
        $this->ensureStorage();
122
123
        return isset($this->storage[$route]) ? $this->storage[$route] : null;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 View Code Duplication
    public function getDefaultOption()
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...
130
    {
131
        $settings = Yii::$app->themeManager->getSettings();
132
133
        if (property_exists($settings, 'filterOrientation') && in_array($settings->filterOrientation, [self::ORIENTATION_HORIZONTAL, self::ORIENTATION_VERTICAL], true)) {
134
            return $settings->filterOrientation;
135
        } else {
136
            return self::ORIENTATION_HORIZONTAL;
137
        }
138
    }
139
140
    public function getOption($option)
141
    {
142
        return $this->get(Yii::$app->request->pathInfo)[$option];
143
    }
144
145
    public function getAllUiOptions()
146
    {
147
        return $this->getSettingsStorage()->getBounded($this->settingsStorageKey);
148
    }
149
}
150