Completed
Push — master ( 619647...201cf7 )
by Klochok
04:45
created

OrientationStorage::saveStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 1
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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-2017, 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 OrientationStorage.
19
 */
20
class OrientationStorage 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 = 'orientations';
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 = ['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...
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
    private function ensureStorage()
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);
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
104
     */
105
    public function set($route, $orientation)
106
    {
107
        $this->ensureStorage();
108
109
        $this->storage[$route] = $orientation;
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] : $this->getDefaultOrientation();
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getDefaultOrientation()
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