ThemeSettingsStorage::init()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
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\storage\SettingsStorageInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\components\SettingsStorageInterface.

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...
14
use Yii;
15
use yii\base\Component;
16
use yii\base\Model;
17
use yii\caching\Cache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\components\Cache.

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...
18
19
class ThemeSettingsStorage extends Component implements SettingsStorageInterface
20
{
21
    /**
22
     * @return SettingsStorage
23
     */
24
    protected function getStorage()
25
    {
26
        return Yii::$app->get('settingsStorage');
27
    }
28
29
    /**
30
     * @var string
31
     */
32
    public $cacheKey;
33
34
    public function init()
35
    {
36
        if ($this->cacheKey === null && !Yii::$app->user->getIsGuest()) {
37
            $this->cacheKey = ['themeSettingsStorage', Yii::$app->user->id];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('themeSettingsStor..., \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...
38
        }
39
    }
40
41
    /**
42
     * @return Cache
43
     */
44
    protected function getCache()
45
    {
46
        return Yii::$app->get('cache');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function set(Model $model)
53
    {
54
        if (Yii::$app->user->getIsGuest()) {
55
            return;
56
        }
57
58
        $data = $model->toArray();
59
60
        $this->getStorage()->setBounded('theme', $data);
61
        $this->setToCache($data);
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    public function get()
68
    {
69
        if (Yii::$app->user->getIsGuest()) {
70
            return [];
71
        }
72
73
        if (($cached = $this->getFromCache()) !== false) {
74
            return $cached;
75
        }
76
77
        $storage = $this->getStorage()->getBounded('theme');
78
        $this->setToCache($storage);
79
80
        return $storage;
81
    }
82
83
    private function getFromCache()
84
    {
85
        if ($this->cacheKey === null) {
86
            return false;
87
        }
88
89
        return $this->getCache()->get($this->cacheKey);
90
    }
91
92
    private function setToCache($data)
93
    {
94
        if ($this->cacheKey === null) {
95
            return false;
96
        }
97
98
        return $this->getCache()->set($this->cacheKey, $data, 86400); // 1 day
99
    }
100
}
101