Completed
Push — master ( be843f...36b93f )
by shjchen
19:20
created

SystemSetting   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getValueAttribute() 0 8 2
A setValueAttribute() 0 8 3
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\Models;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
/**
17
 * Class SystemSetting
18
 * @package iBrand\Component\Setting\Models
19
 */
20
class SystemSetting extends Model
21
{
22
23
    /**
24
     * @var array
25
     */
26
    protected $guarded = ['id'];
27
28
    /**
29
     * SystemSetting constructor.
30
     * @param array $attributes
31
     */
32 6
    public function __construct(array $attributes = [])
33
    {
34 6
        parent::__construct($attributes);
35
36 6
        $this->setTable(config('ibrand.setting.table_name'));
37 6
    }
38
39
    /**
40
     * @param $value
41
     * @return mixed|string
42
     */
43 6
    public function getValueAttribute($value)
44
    {
45 6
        if (!is_null($value)) {
46 6
            return json_decode($value, true);
47
        }
48
49
        return '';
50
    }
51
52
    /**
53
     * @param $value
54
     * @return string
55
     */
56 6
    public function setValueAttribute($value)
57
    {
58 6
        if ($value or 0 == $value) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
59 6
            $this->attributes['value'] = json_encode($value);
60
        }
61
62 6
        return '';
63
    }
64
}
65