EloquentSetting   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setSetting() 0 18 4
A getSetting() 0 14 3
A allToArray() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/setting.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
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 iBrand\Component\Setting\Repositories;
13
14
use iBrand\Component\Setting\Models\SystemSetting;
15
16
/**
17
 * Class EloquentSetting
18
 * @package iBrand\Component\Setting\Repositories
19
 */
20
class EloquentSetting implements SettingInterface
21
{
22
    /**
23
     * @var SystemSetting
24
     */
25
    private $model;
26
27
    /**
28
     * EloquentSetting constructor.
29
     * @param SystemSetting $model
30
     */
31 7
    public function __construct(SystemSetting $model)
32
    {
33 7
        $this->model = $model;
34 7
    }
35
36
    /**
37
     * @param array $settings
38
     * @return bool
39
     */
40 6
    public function setSetting(array $settings)
41
    {
42 6
        if (count($settings) <= 0) {
43 2
            return false;
44
        }
45
46 6
        foreach ($settings as $key => $val) {
47 6
            $var = $this->model->where('key', $key)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<iBrand\Component\...g\Models\SystemSetting>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48 6
            if ($var) {
49 1
                $var->value = $val;
50 1
                $var->save();
51
            } else {
52 6
                $var = $this->model->create(['key' => $key, 'value' => $val]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on iBrand\Component\Setting\Models\SystemSetting. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
            }
54
        }
55
56 6
        return $var;
0 ignored issues
show
Bug introduced by
The variable $var does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
57
    }
58
59
    /**
60
     * @param $key
61
     * @param null $default
62
     * @return bool|mixed|string
63
     */
64 1
    public function getSetting($key, $default = null)
65
    {
66 1
        $value = $this->model->where('key', $key)->get(['value'])->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<iBrand\Component\...g\Models\SystemSetting>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
68 1
        if (!is_null($value)) {
69 1
            return $value->value;
70
        }
71
72 1
        if (!is_null($default)) {
73 1
            return $default;
74
        }
75
76 1
        return '';
77
    }
78
79
    /**
80
     * @return array
81
     */
82 4
    public function allToArray()
83
    {
84 4
        $collection = $this->model->all();
85 4
        $keyed = $collection->mapWithKeys(function ($item) {
86 4
            return [$item->key => $item->value];
87 4
        });
88
89 4
        return $keyed->toArray();
90
    }
91
}
92