Completed
Push — dev ( 58edee...4d3d6f )
by Zach
04:00 queued 01:44
created

HasSettings::setSettingsByRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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
Documentation Bug introduced by
It seems like new \LaravelPropertyBag\...$settingsConfig, $this) of type object<LaravelPropertyBag\Settings\Settings> is incompatible with the declared type object<LaravelPropertyBa...yBag\Settings\Settings> of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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)) {
74
            $fullNamespace = $this->settingsConfig;
0 ignored issues
show
Bug introduced by
The property settingsConfig does not seem to exist. Did you mean settings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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