HasSettings::defaultSetting()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LaravelPropertyBag\Settings;
4
5
use LaravelPropertyBag\Helpers\NameResolver;
6
use LaravelPropertyBag\Exceptions\ResourceNotFound;
7
8
trait HasSettings
9
{
10
    /**
11
     * Instance of Settings.
12
     *
13
     * @var \LaravelPropertyBag\Settings\Settings
14
     */
15
    protected $settings = null;
16
17
    /**
18
     * A resource has many settings in a property bag.
19
     *
20
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
21
     */
22
    public function propertyBag()
23
    {
24
        return $this->morphMany(PropertyBag::class, 'resource');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        return $this->/** @scrutinizer ignore-call */ morphMany(PropertyBag::class, 'resource');
Loading history...
25
    }
26
27
    /**
28
     * If passed is string, get settings class for the resource or return value
29
     * for given key. If passed is array, set the key value pair.
30
     *
31
     * @param string|array $passed
32
     *
33
     * @return \LaravelPropertyBag\Settings\Settings|mixed
34
     */
35
    public function settings($passed = null)
36
    {
37
        if (is_array($passed)) {
38
            return $this->setSettings($passed);
39
        } elseif (!is_null($passed)) {
40
            $settings = $this->getSettingsInstance();
41
42
            return $settings->get($passed);
43
        }
44
45
        return $this->getSettingsInstance();
46
    }
47
48
    /**
49
     * Get settings off this or create new instance.
50
     *
51
     * @return \LaravelPropertyBag\Settings\Settings
52
     */
53
    protected function getSettingsInstance()
54
    {
55
        if (isset($this->settings)) {
56
            return $this->settings;
57
        }
58
59
        $settingsConfig = $this->getSettingsConfig();
60
61
        return $this->settings = new Settings($settingsConfig, $this);
0 ignored issues
show
Bug introduced by
$this of type LaravelPropertyBag\Settings\HasSettings is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $resource of LaravelPropertyBag\Setti...Settings::__construct(). ( Ignorable by Annotation )

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

61
        return $this->settings = new Settings($settingsConfig, /** @scrutinizer ignore-type */ $this);
Loading history...
62
    }
63
64
    /**
65
     * Get the settings class name.
66
     *
67
     * @throws ResourceNotFound
68
     *
69
     * @return \LaravelPropertyBag\Settings\ResourceConfig
70
     */
71
    protected function getSettingsConfig()
72
    {
73
        if (isset($this->settingsConfig)) {
0 ignored issues
show
Bug introduced by
The property settingsConfig does not exist on LaravelPropertyBag\Settings\HasSettings. Did you mean settings?
Loading history...
74
            $fullNamespace = $this->settingsConfig;
75
        } else {
76
            $className = $this->getShortClassName();
77
78
            $fullNamespace = NameResolver::makeConfigFileName($className);
79
        }
80
81
        if (class_exists($fullNamespace)) {
82
            return new $fullNamespace($this);
83
        }
84
85
        throw ResourceNotFound::resourceConfigNotFound($fullNamespace);
86
    }
87
88
    /**
89
     * Get the short name of the model.
90
     *
91
     * @return string
92
     */
93
    protected function getShortClassName()
94
    {
95
        $reflection = new \ReflectionClass($this);
96
97
        return $reflection->getShortName();
98
    }
99
100
    /**
101
     * Set settings.
102
     *
103
     * @param array $attributes
104
     *
105
     * @return \LaravelPropertyBag\Settings\Settings
106
     */
107
    public function setSettings(array $attributes)
108
    {
109
        return $this->settings()->set($attributes);
110
    }
111
112
    /**
113
     * Set all allowed settings by Request.
114
     *
115
     * @return \LaravelPropertyBag\Settings\Settings
116
     */
117
    public function setSettingsByRequest()
118
    {
119
        $allAllowedSettings = array_keys($this->allSettings()->toArray());
120
121
        return $this->settings()->set(request()->only($allAllowedSettings));
122
    }
123
124
    /**
125
     * Get all settings.
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    public function allSettings()
130
    {
131
        return $this->settings()->all();
132
    }
133
134
    /**
135
     * Get all default settings or default setting for single key if given.
136
     *
137
     * @param string $key
138
     *
139
     * @return \Illuminate\Support\Collection|mixed
140
     */
141
    public function defaultSetting($key = null)
142
    {
143
        if (!is_null($key)) {
144
            return $this->settings()->getDefault($key);
145
        }
146
147
        return $this->settings()->allDefaults();
148
    }
149
150
    /**
151
     * Get all allowed settings or allowed settings for single ke if given.
152
     *
153
     * @param string $key
154
     *
155
     * @return \Illuminate\Support\Collection
156
     */
157
    public function allowedSetting($key = null)
158
    {
159
        if (!is_null($key)) {
160
            return $this->settings()->getAllowed($key);
161
        }
162
163
        return $this->settings()->allAllowed();
164
    }
165
166
    /**
167
     * Get a collection with all users with the given setting and/or value.
168
     *
169
     * @param string $key
170
     * @param mixed  $value
171
     *
172
     * @return \Illuminate\Support\Collection
173
     */
174
    public static function withSetting($key, $value = null)
175
    {
176
        return static::all()->filter(function ($row) use ($key, $value) {
177
            $setting = $row->settings($key);
178
179
            if (!is_null($value)) {
180
                return !is_null($setting) && $setting === $value;
181
            }
182
183
            return !is_null($setting);
184
        });
185
    }
186
}
187