Repository::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Config;
13
14
use Gitamin\Models\Setting;
15
16
class Repository
17
{
18
    /**
19
     * The eloquent model instance.
20
     *
21
     * @var \Gitamin\Models\Setting
22
     */
23
    protected $model;
24
25
    /**
26
     * Cache of the settings.
27
     *
28
     * @var array|null
29
     */
30
    protected $settings;
31
32
    /**
33
     * Create a new settings service instance.
34
     *
35
     * @param \Gitamin\Models\Setting $model
36
     */
37
    public function __construct(Setting $model)
38
    {
39
        $this->model = $model;
40
    }
41
42
    /**
43
     * Returns a setting from the database.
44
     *
45
     * @param string      $name
46
     * @param string|null $default
47
     *
48
     * @return string|null
49
     */
50
    public function get($name, $default = null)
51
    {
52
        // if we've not loaded the settings, load them now
53
        if (! $this->settings) {
54
            $this->settings = $this->model->all()->lists('value', 'name');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->model->all()->lists('value', 'name') of type object<Illuminate\Database\Eloquent\Collection> is incompatible with the declared type array|null 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...
55
        }
56
57
        // if the setting exists and is not blank, return it
58
        if (! empty($this->settings[$name])) {
59
            return $this->settings[$name];
60
        }
61
62
        return $default;
63
    }
64
65
    /**
66
     * Creates or updates a setting value.
67
     *
68
     * @param string $name
69
     * @param string $value
70
     */
71
    public function set($name, $value)
72
    {
73
        // save the change to the db
74
        $this->model->updateOrCreate(compact('name'), compact('value'));
75
76
        // if we've loaded the settings, persist this change
77
        if ($this->settings) {
78
            $this->settings[$name] = $value;
79
        }
80
    }
81
}
82