Passed
Push — master ( da797a...38c1ce )
by Gombos
03:26
created

HasSettingsField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 38
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSettingsValue() 0 8 2
A settings() 0 3 1
A getSettingsFieldName() 0 3 1
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
 */
15
trait HasSettingsField
16
{
17
    use HasSettings;
18
19
    /**
20
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
21
     * @throws ModelSettingsException
22
     */
23 9
    public function settings(): SettingsManagerContract
24
    {
25 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

25
        return new FieldSettingsManager(/** @scrutinizer ignore-type */ $this);
Loading history...
26
    }
27
28
    /**
29
     * @return array
30
     * @throws ModelSettingsException
31
     */
32 9
    public function getSettingsValue(): array
33
    {
34 9
        $settingsFieldName = $this->getSettingsFieldName();
35 9
        $attributes = $this->getAttributes();
36 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

36
        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...
37 2
            throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
38
        }
39 7
        return json_decode($this->getAttributeValue($settingsFieldName) ?? '[]', true);
40
    }
41
42
    /**
43
     * @return string
44
     */
45 9
    public function getSettingsFieldName(): string
46
    {
47 9
        return $this->settingsFieldName ?? config('model_settings.settings_field_name');
48
    }
49
50
    abstract public function getTable();
51
    abstract public function getAttributes();
52
    abstract public function getAttributeValue($key);
53
}
54