Completed
Push — master ( e14e07...c33614 )
by Loban
54:50
created

Model::getSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
13
/**
14
 * Class Model represents settings model
15
 * @package lav45\settings
16
 */
17
class Model extends \yii\base\Model
18
{
19
    /**
20
     * @var Settings|string|array
21
     */
22
    private $settings = 'settings';
23
24
    /**
25
     * @inheritdoc
26
     */
27 2
    public function init()
28
    {
29 2
        $this->setAttributes($this->getData(), false);
30 2
    }
31
32
    /**
33
     * @return Settings
34
     * @throws \yii\base\InvalidConfigException
35
     */
36 2
    public function getSettings()
37
    {
38 2
        if ($this->settings instanceof Settings === false) {
39 2
            $this->settings = Instance::ensure($this->settings, Settings::class);
40
        }
41 2
        return $this->settings;
42
    }
43
44
    /**
45
     * @param Settings|string|array $data
46
     */
47
    public function setSettings($data)
48
    {
49
        $this->settings = $data;
50
    }
51
52
    /**
53
     * @param bool $runValidation
54
     * @return bool
55
     */
56 2
    public function save($runValidation = true)
57
    {
58 2
        if ($runValidation === false || $this->validate()) {
59 2
            return $this->setData($this->getSaveAttributes($runValidation));
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * @return array|string|integer
66
     */
67 2
    protected function getSettingsKey()
68
    {
69 2
        return [static::class];
70
    }
71
72
    /**
73
     * List of attributes to save
74
     * @param bool $safeOnly
75
     * @return string[]
76
     * @since 1.2.2
77
     */
78 2
    protected function getSaveAttributeList($safeOnly = false)
79
    {
80 2
        return $safeOnly ? $this->safeAttributes() : $this->attributes();
81
    }
82
83
    /**
84
     * @param bool $safeOnly
85
     * @return array
86
     */
87 2
    protected function getSaveAttributes($safeOnly = false)
88
    {
89 2
        $data = $this->getAttributes($this->getSaveAttributeList($safeOnly));
90
        return array_filter($data, function ($val) {
91 2
            return $val !== '' || $val !== [] || $val !== null;
92 2
        });
93
    }
94
95
    /**
96
     * @return array
97
     */
98 2
    protected function getData()
99
    {
100 2
        return $this->getSettings()->get($this->getSettingsKey(), []);
101
    }
102
103
    /**
104
     * @param array $data
105
     * @return bool
106
     */
107 2
    protected function setData($data)
108
    {
109 2
        return $this->getSettings()->set($this->getSettingsKey(), $data);
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 1
    public function flush()
116
    {
117 1
        $this->setAttributes(array_fill_keys($this->getSaveAttributeList(), null));
118 1
        return $this->getSettings()->delete($this->getSettingsKey());
119
    }
120
}
121