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

OrientationStorage::ensureStorage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\components;
13
14
use Yii;
15
use yii\base\Component;
16
17
/**
18
 * Class OrientationStorage
19
 *
20
 * @package hipanel\components
21
 */
22
class OrientationStorage extends Component
23
{
24
    const ORIENTATION_HORIZONTAL = 'horizontal';
25
    const ORIENTATION_VERTICAL = 'vertical';
26
27
    public $defaultOrientation = self::ORIENTATION_HORIZONTAL;
28
29
    /**
30
     * @var string the cache key that will be used to cache storage values
31
     */
32
    public $cacheKey;
33
34
    /**
35
     * @var string
36
     */
37
    public $settingsStorageKey = 'orientations';
38
39
    /**
40
     * @var array
41
     */
42
    protected $storage;
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function init()
48
    {
49
        if (!isset($this->cacheKey)) {
50
            $this->cacheKey = ['orientationStorage', Yii::$app->user->id];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('orientationStorage', \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...
51
        }
52
    }
53
54
    /**
55
     * @return \yii\caching\Cache
56
     */
57
    protected function getCache()
58
    {
59
        return Yii::$app->getCache();
60
    }
61
62
    /**
63
     * @return SettingsStorageInterface
64
     */
65
    protected function getSettingsStorage()
66
    {
67
        return Yii::$app->get('settingsStorage');
68
    }
69
70
    /**
71
     * Ensures that [[storage]] contains actual orientations storage
72
     *
73
     * @return array
74
     */
75
    private function ensureStorage()
76
    {
77
        if (!isset($this->storage)) {
78
            $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...
79
80
            if ($this->storage === false) {
81
                $this->storage = $this->getSettingsStorage()->getBounded($this->settingsStorageKey);
82
                $this->cache();
83
            }
84
        }
85
86
        return $this->storage;
87
    }
88
89
    /**
90
     * Saves current [[storage]]
91
     */
92
    protected function saveStorage()
93
    {
94
        $this->getSettingsStorage()->setBounded($this->settingsStorageKey, $this->storage);
95
        $this->cache();
96
    }
97
98
    /**
99
     * Caches current [[storage]]
100
     */
101
    protected function cache()
102
    {
103
        $this->getCache()->set($this->cacheKey, $this->storage, 86400); // 1 day
104
    }
105
106
    /**
107
     * Sets orientation for the $route
108
     *
109
     * @param string $route
110
     * @param string $orientation
111
     */
112
    public function set($route, $orientation)
113
    {
114
        $this->ensureStorage();
115
116
        $this->storage[$route] = $orientation;
117
        $this->saveStorage();
118
    }
119
120
    /**
121
     * Gets orientation for the $route
122
     *
123
     * @param $route
124
     * @return string
125
     */
126
    public function get($route)
127
    {
128
        $this->ensureStorage();
129
130
        return isset($this->storage[$route]) ? $this->storage[$route] : $this->defaultOrientation;
131
    }
132
}
133