Passed
Push — master ( f4169b...5ce74f )
by Loban
02:35
created

Model::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings;
10
11
use yii\di\Instance;
12
use yii\helpers\ArrayHelper;
13
14
/**
15
 * Class Model represents settings model
16
 * @package lav45\settings
17
 */
18
class Model extends \yii\base\Model
19
{
20
    /**
21
     * @var Settings|string|array
22
     */
23
    private $settings = 'settings';
24
25
    /**
26
     * @inheritdoc
27 2
     */
28
    public function init()
29 2
    {
30 2
        $this->setAttributes($this->getData(), false);
31
    }
32
33
    /**
34
     * @return Settings
35
     * @throws \yii\base\InvalidConfigException
36 2
     */
37
    public function getSettings()
38 2
    {
39 2
        if ($this->settings instanceof Settings === false) {
40
            $this->settings = Instance::ensure($this->settings, Settings::class);
41 2
        }
42
        return $this->settings;
43
    }
44
45
    /**
46
     * @param Settings|string|array $data
47
     */
48
    public function setSettings($data)
49
    {
50
        $this->settings = $data;
51
    }
52
53
    /**
54
     * @param bool $runValidation
55
     * @return bool
56 2
     */
57
    public function save($runValidation = true)
58 2
    {
59 2
        if ($runValidation === false || $this->validate()) {
60
            return $this->setData($this->getSaveAttributes($runValidation));
61
        }
62
        return false;
63
    }
64
65
    /**
66
     * @return array|string|integer
67 2
     */
68
    protected function getSettingsKey()
69 2
    {
70
        return [static::class];
71
    }
72
73
    /**
74
     * List of attributes to save
75
     * @param bool $safeOnly
76
     * @return string[]
77
     * @since 1.2.2
78 2
     */
79
    protected function getSaveAttributeList($safeOnly = false)
80 2
    {
81
        return $safeOnly ? $this->safeAttributes() : $this->attributes();
82
    }
83
84
    /**
85
     * @param bool $safeOnly
86
     * @return array
87 2
     */
88
    protected function getSaveAttributes($safeOnly = false)
89 2
    {
90
        $data = $this->getAttributes($this->getSaveAttributeList($safeOnly));
91 2
        return array_filter($data, static function ($val) {
92 2
            return $val !== '' || $val !== [] || $val !== null;
93
        });
94
    }
95
96
    /**
97
     * @return array
98 2
     */
99
    protected function getData()
100 2
    {
101
        return $this->getSettings()->get($this->getSettingsKey(), []);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getSetting...SettingsKey(), array()) also could return the type string which is incompatible with the documented return type array.
Loading history...
102
    }
103
104
    /**
105
     * @param array $data
106
     * @return bool
107 2
     */
108
    protected function setData($data)
109 2
    {
110
        return $this->getSettings()->set($this->getSettingsKey(), ArrayHelper::toArray($data));
111
    }
112
113
    /**
114
     * @return bool
115 1
     */
116
    public function flush()
117 1
    {
118 1
        $this->setAttributes(array_fill_keys($this->getSaveAttributeList(), null));
119
        return $this->getSettings()->delete($this->getSettingsKey());
0 ignored issues
show
Bug introduced by
$this->getSettingsKey() of type array<integer,string> is incompatible with the type string expected by parameter $key of lav45\settings\Settings::delete(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        return $this->getSettings()->delete(/** @scrutinizer ignore-type */ $this->getSettingsKey());
Loading history...
120
    }
121
}
122