SettingRepository::findByKeyOrNew()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php namespace Anomaly\SettingsModule\Setting;
2
3
use Anomaly\SettingsModule\Setting\Command\GetDefaultValue;
4
use Anomaly\SettingsModule\Setting\Contract\SettingInterface;
5
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
6
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection;
7
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
8
use Anomaly\Streams\Platform\Entry\EntryRepository;
9
use Illuminate\Config\Repository;
10
use Illuminate\Foundation\Bus\DispatchesJobs;
11
12
/**
13
 * Class SettingRepositoryInterface
14
 *
15
 * @link          http://pyrocms.com/
16
 * @author        PyroCMS, Inc. <[email protected]>
17
 * @author        Ryan Thompson <[email protected]>
18
 */
19
class SettingRepository extends EntryRepository implements SettingRepositoryInterface
20
{
21
22
    use DispatchesJobs;
23
24
    /**
25
     * The setting model.
26
     *
27
     * @var SettingModel
28
     */
29
    protected $model;
30
31
    /**
32
     * The settings collection.
33
     *
34
     * @var SettingCollection
35
     */
36
    protected $settings;
37
38
    /**
39
     * Create a new SettingRepositoryInterface instance.
40
     *
41
     * @param SettingModel        $model
42
     * @param Repository          $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
     * @param FieldTypeCollection $fieldTypes
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldTypes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     */
45
    public function __construct(SettingModel $model)
46
    {
47
        $this->model = $model;
48
49
        $this->settings = $this->model->all();
50
    }
51
52
    /**
53
     * Return if the key exists or not.
54
     *
55
     * @param $key
56
     * @return bool
57
     */
58
    public function has($key)
59
    {
60
        return $this->settings->has($key);
61
    }
62
63
    /**
64
     * Get a setting.
65
     *
66
     * @param                                     $key
67
     * @param  null                               $default
68
     * @return null|SettingInterface|SettingModel
69
     */
70
    public function get($key, $default = null)
71
    {
72
        $setting = $this->settings->get($key);
73
74
        return ($setting) ? $setting : $default;
75
    }
76
77
    /**
78
     * Set a settings value.
79
     *
80
     * @param $key
81
     * @param $value
82
     * @return bool
83
     */
84
    public function set($key, $value)
85
    {
86
        $setting = $this->findByKeyOrNew($key);
87
88
        $setting->setValue($value);
89
90
        return $this->save($setting);
91
    }
92
93
    /**
94
     * Get a setting value.
95
     *
96
     * @param             $key
97
     * @param  null       $default
98
     * @return mixed|null
99
     */
100
    public function value($key, $default = null)
101
    {
102
        $setting = $this->get($key);
103
104
        if ($setting) {
105
            return $setting->getValue();
106
        }
107
108
        if (!$setting && !$default) {
109
            return $this->dispatch(new GetDefaultValue($key));
110
        }
111
112
        return $default;
113
    }
114
115
    /**
116
     * Return the field type
117
     * presenter for a setting.
118
     *
119
     * @param $key
120
     * @return FieldTypePresenter|null
121
     */
122
    public function presenter($key)
123
    {
124
        if ($setting = $this->get($key)) {
125
            return $setting->getFieldTypePresenter('value');
0 ignored issues
show
Bug introduced by
The method getFieldTypePresenter does only exist in Anomaly\SettingsModule\Setting\SettingModel, but not in Anomaly\SettingsModule\S...ntract\SettingInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
126
        }
127
128
        return null;
129
    }
130
131
    /**
132
     * Find a setting by it's key
133
     * or return a new instance.
134
     *
135
     * @param $key
136
     * @return SettingInterface
137
     */
138
    public function findByKeyOrNew($key)
139
    {
140
        if (!$setting = $this->model->where('key', $key)->first()) {
141
142
            $setting = $this->model->newInstance();
143
144
            $setting->setKey($key);
145
        }
146
147
        return $setting;
148
    }
149
150
    /**
151
     * Find all settings with namespace.
152
     *
153
     * @param $namespace
154
     * @return SettingCollection
155
     */
156
    public function findAllByNamespace($namespace)
157
    {
158
        return $this->model->where('key', 'LIKE', $namespace . '%')->get();
159
    }
160
}
161