FileProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStorageState() 0 6 2
A setStorageState() 0 4 1
A getStorageData() 0 11 3
A setStorageData() 0 6 1
A getStorageFilePath() 0 8 2
1
<?php
2
3
namespace yiicod\pagesizepager\providers;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\FileHelper;
8
use yii\helpers\Json;
9
10
/**
11
 * Class FileProvider
12
 *
13
 * @author Orlov Aleksey <[email protected]>
14
 * @package yiicod\base\traits
15
 */
16
class FileProvider extends ProviderAbstract
17
{
18
    public $dir = '@runtime';
19
20
    /**
21
     * Get storage state value
22
     *
23
     * @param string $key
24
     * @param $default
25
     *
26
     * @return mixed
27
     */
28
    public function getStorageState(string $key, $default = null)
29
    {
30
        $data = $this->getStorageData();
31
32
        return isset($data[$key]) ? $data[$key] : $default;
33
    }
34
35
    /**
36
     * Set storage state value
37
     *
38
     * @param string $key
39
     * @param $value
40
     */
41
    public function setStorageState(string $key, array $value)
42
    {
43
        $this->setStorageData([$key => $value]);
44
    }
45
46
    /**
47
     * Get storage data
48
     *
49
     * @param bool $decode
50
     *
51
     * @return array|string
52
     */
53
    protected function getStorageData(bool $decode = true)
54
    {
55
        $data = Json::encode([]);
56
57
        $storageFilePath = $this->getStorageFilePath();
58
        if (file_exists($storageFilePath)) {
59
            $data = file_get_contents($storageFilePath);
60
        }
61
62
        return ($decode) ? Json::decode($data) : $data;
63
    }
64
65
    /**
66
     * Add data to storage
67
     *
68
     * @param array $newData
69
     */
70
    protected function setStorageData(array $newData)
71
    {
72
        $data = ArrayHelper::merge($this->getStorageData(), $newData);
73
74
        file_put_contents($this->getStorageFilePath(), Json::encode($data));
75
    }
76
77
    /**
78
     * Get path to storage file.
79
     *
80
     * @return string
81
     */
82
    protected function getStorageFilePath(): string
83
    {
84
        if (false === is_dir(Yii::getAlias($this->dir))) {
85
            FileHelper::createDirectory(Yii::getAlias($this->dir), 0755, true);
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias($this->dir) targeting yii\BaseYii::getAlias() can also be of type boolean; however, yii\helpers\BaseFileHelper::createDirectory() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
        }
87
88
        return Yii::getAlias($this->dir) . DIRECTORY_SEPARATOR . 'yii_page_size.bin';
89
    }
90
}
91