Passed
Push — master ( 4fa21c...f73cee )
by Gombos
03:39
created

HasSettingsField::isPersistSettings()   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 0
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
9
/**
10
 * Trait HasSettingsField
11
 * @package Glorand\Model\Settings\Traits
12
 * @property array $settings
13
 * @property string $settingsFieldName
14
 * @property boolean $persistSettings
15
 */
16
trait HasSettingsField
17
{
18
    use HasSettings;
19
20
    private $persistSettings = null;
21
22
    /**
23
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
24
     * @throws ModelSettingsException
25
     */
26 9
    public function settings(): SettingsManagerContract
27
    {
28 9
        return new FieldSettingsManager($this);
1 ignored issue
show
Bug introduced by
$this of type Glorand\Model\Settings\Traits\HasSettingsField is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Glorand\Model\Settings\M...sManager::__construct(). ( Ignorable by Annotation )

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

28
        return new FieldSettingsManager(/** @scrutinizer ignore-type */ $this);
Loading history...
29
    }
30
31
    /**
32
     * @return array
33
     * @throws ModelSettingsException
34
     */
35 9
    public function getSettingsValue(): array
36
    {
37 9
        $settingsFieldName = $this->getSettingsFieldName();
38 9
        $attributes = $this->getAttributes();
39 9
        if (!array_has($attributes, $settingsFieldName)) {
0 ignored issues
show
Deprecated Code introduced by
The function array_has() has been deprecated: Arr::has() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

39
        if (!/** @scrutinizer ignore-deprecated */ array_has($attributes, $settingsFieldName)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
40 2
            throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
41
        }
42
43 7
        return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
44
    }
45
46
    /**
47
     * @return string
48
     */
49 9
    public function getSettingsFieldName(): string
50
    {
51 9
        return $this->settingsFieldName ?? config('model_settings.settings_field_name');
52
    }
53
54
    /**
55
     * @return bool
56
     */
57 6
    public function isPersistSettings(): bool
58
    {
59 6
        return boolval($this->persistSettings ?? config('model_settings.settings_persistent'));
60
    }
61
62
    /**
63
     * @param bool $val
64
     */
65 1
    public function setPersistSettings(bool $val = true)
66
    {
67 1
        $this->persistSettings = $val;
68 1
    }
69
70
    abstract public function getTable();
71
72
    abstract public function getAttributes();
73
74
    abstract public function getAttributeValue($key);
75
}
76