Passed
Push — master ( 94b41b...17292f )
by Gombos
02:51
created

HasSettingsField::setPersistSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Glorand\Model\Settings\Traits;
4
5
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
6
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
7
use Glorand\Model\Settings\Managers\FieldSettingsManager;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\Cache;
10
use Illuminate\Support\Facades\Schema;
11
12
/**
13
 * Trait HasSettingsField
14
 * @package Glorand\Model\Settings\Traits
15
 * @property array $settings
16
 * @property string $settingsFieldName
17
 * @property boolean $persistSettings
18
 */
19
trait HasSettingsField
20
{
21
    use HasSettings;
22
23
    private $persistSettings = null;
24
25 231
    protected static function bootHasSettingsField()
26
    {
27
        static::saving(function ($model) {
28
            /** @var self $model */
29 231
            $model->fixSettingsValue();
30 231
        });
31 231
    }
32
33
    /**
34
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
35
     * @throws ModelSettingsException
36
     */
37 108
    public function settings(): SettingsManagerContract
38
    {
39 108
        return new FieldSettingsManager($this);
40
    }
41
42 231
    public function fixSettingsValue()
43
    {
44 231
        $settingsFieldName = $this->getSettingsFieldName();
45 231
        $attributes = $this->getAttributes();
46 231
        if (Arr::has($attributes, $settingsFieldName)) {
47 93
            if (is_array($this->$settingsFieldName)) {
48 6
                $this->$settingsFieldName = json_encode($this->$settingsFieldName);
49
            }
50
        }
51 231
    }
52
53
    /**
54
     * @return array
55
     * @throws ModelSettingsException
56
     */
57 108
    public function getSettingsValue(): array
58
    {
59 108
        $settingsFieldName = $this->getSettingsFieldName();
60 108
        if (!$this->hasSettingsField()) {
61 9
            throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
62
        }
63
64 99
        $value = json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
65
66 99
        return is_array($value) ? $value : [];
67
    }
68
69
    /**
70
     * @return string
71
     */
72 231
    public function getSettingsFieldName(): string
73
    {
74 231
        return $this->settingsFieldName ?? config('model_settings.settings_field_name');
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 84
    public function isPersistSettings(): bool
81
    {
82 84
        return boolval($this->persistSettings ?? config('model_settings.settings_persistent'));
83
    }
84
85
    /**
86
     * @param bool $val
87
     */
88 6
    public function setPersistSettings(bool $val = true)
89
    {
90 6
        $this->persistSettings = $val;
91 6
    }
92
93
94
    /**
95
     * @return mixed
96
     */
97 108
    private function hasSettingsField()
98
    {
99 108
        return Cache::remember(
100 108
            config('model_settings.settings_table_cache_prefix') . '::has_field',
101 108
            now()->addDays(1),
102
            function () {
103 108
                return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName());
104 108
            }
105
        );
106
    }
107
108
    abstract public function getTable();
109
110
    abstract public function getAttributes();
111
112
    abstract public function getAttributeValue($key);
113
}
114