Completed
Push — master ( 7a0906...3ff6f8 )
by Ryan
02:30
created

SettingRepository::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 12
Bugs 1 Features 0
Metric Value
c 12
b 1
f 0
dl 0
loc 8
cc 1
eloc 4
nc 1
nop 2
rs 9.4285
1
<?php namespace Anomaly\SettingsModule\Setting;
2
3
use Anomaly\SettingsModule\Setting\Contract\SettingInterface;
4
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection;
6
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
7
use Anomaly\Streams\Platform\Entry\EntryRepository;
8
use Illuminate\Config\Repository;
9
10
/**
11
 * Class SettingRepositoryInterface
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 * @package       Anomaly\SettingsModule\SettingInterface
17
 */
18
class SettingRepository extends EntryRepository implements SettingRepositoryInterface
19
{
20
21
    /**
22
     * The setting model.
23
     *
24
     * @var SettingModel
25
     */
26
    protected $model;
27
28
    /**
29
     * The settings collection.
30
     *
31
     * @var SettingCollection
32
     */
33
    protected $settings;
34
35
    /**
36
     * Create a new SettingRepositoryInterface instance.
37
     *
38
     * @param SettingModel        $model
39
     * @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...
40
     * @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...
41
     */
42
    public function __construct(SettingModel $model)
43
    {
44
        $this->model = $model;
45
46
        $this->settings = $this->model->all();
47
    }
48
49
    /**
50
     * Return if the key exists or not.
51
     *
52
     * @param $key
53
     * @return bool
54
     */
55
    public function has($key)
56
    {
57
        return $this->settings->has($key);
58
    }
59
60
    /**
61
     * Get a setting.
62
     *
63
     * @param      $key
64
     * @param null $default
65
     * @return null|SettingInterface|SettingModel
66
     */
67
    public function get($key, $default = null)
68
    {
69
        $setting = $this->settings->get($key);
70
71
        return ($setting) ? $setting : $default;
72
    }
73
74
    /**
75
     * Set a settings value.
76
     *
77
     * @param $key
78
     * @param $value
79
     * @return bool
80
     */
81
    public function set($key, $value)
82
    {
83
        $setting = $this->findByKeyOrNew($key);
84
85
        $setting->setValue($value);
86
87
        return $this->save($setting);
88
    }
89
90
    /**
91
     * Get a setting value.
92
     *
93
     * @param      $key
94
     * @param null $default
95
     * @return mixed|null
96
     */
97
    public function value($key, $default = null)
98
    {
99
        if ($setting = $this->get($key)) {
100
            return $setting->getValue();
101
        }
102
103
        return $default;
104
    }
105
106
    /**
107
     * Return the field type
108
     * presenter for a setting.
109
     *
110
     * @param $key
111
     * @return FieldTypePresenter|null
112
     */
113
    public function presenter($key)
114
    {
115
        if ($setting = $this->get($key)) {
116
            return $setting->getFieldTypePresenter('value');
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * Find a setting by it's key
124
     * or return a new instance.
125
     *
126
     * @param $key
127
     * @return SettingInterface
128
     */
129
    public function findByKeyOrNew($key)
130
    {
131
        if (!$setting = $this->model->where('key', $key)->first()) {
132
133
            $setting = $this->model->newInstance();
134
135
            $setting->setKey($key);
136
        }
137
138
        return $setting;
139
    }
140
141
    /**
142
     * Find all settings with namespace.
143
     *
144
     * @param $namespace
145
     * @return SettingCollection
146
     */
147
    public function findAllByNamespace($namespace)
148
    {
149
        return $this->model->where('key', 'LIKE', $namespace . '%')->get();
150
    }
151
}
152