Setting   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 82.81%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 10
c 5
b 0
f 0
lcom 1
cbo 4
dl 0
loc 107
ccs 53
cts 64
cp 0.8281
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B beforeSave() 0 25 4
A attributeLabels() 0 13 1
A rules() 0 17 1
B getTypes() 0 36 4
1
<?php
2
/**
3
 * @link http://phe.me
4
 * @copyright Copyright (c) 2014 Pheme
5
 * @license MIT http://opensource.org/licenses/MIT
6
 */
7
8
namespace pheme\settings\models;
9
10
use Yii;
11
use yii\helpers\Json;
12
use yii\base\DynamicModel;
13
use pheme\settings\Module;
14
use yii\base\InvalidParamException;
15
16
/**
17
 * This is the model class for table "settings".
18
 *
19
 * @author Aris Karageorgos <[email protected]>
20
 */
21
class Setting extends BaseSetting
22
{
23
    /**
24
     * @param bool $forDropDown if false - return array or validators, true - key=>value for dropDown
25
     * @return array
26
     */
27 24
    public function getTypes($forDropDown = true)
28
    {
29
        $values = [
30 24
            'string' => ['value', 'string'],
31 24
            'integer' => ['value', 'integer'],
32 24
            'boolean' => ['value', 'boolean', 'trueValue' => "1", 'falseValue' => "0", 'strict' => true],
33 24
            'float' => ['value', 'number'],
34 24
            'email' => ['value', 'email'],
35 24
            'ip' => ['value', 'ip'],
36 24
            'url' => ['value', 'url'],
37
            'object' => [
38 24
                'value',
39
                function ($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
                    $object = null;
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
                    try {
42
                        Json::decode($this->$attribute);
43
                    } catch (InvalidParamException $e) {
44
                        $this->addError($attribute, Module::t('settings', '"{attribute}" must be a valid JSON object', [
45
                            'attribute' => $attribute,
46
                        ]));
47
                    }
48
                }
49 24
            ],
50 24
        ];
51
52 24
        if (!$forDropDown) {
53 24
            return $values;
54
        }
55
56 1
        $return = [];
57 1
        foreach ($values as $key => $value) {
58 1
            $return[$key] = Module::t('settings', $key);
59 1
        }
60
61 1
        return $return;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 24
    public function rules()
68
    {
69
        return [
70 24
            [['value'], 'string'],
71 24
            [['section', 'key'], 'string', 'max' => 255],
72
            [
73 24
                ['key'],
74 24
                'unique',
75 24
                'targetAttribute' => ['section', 'key'],
76
                'message' =>
77 24
                    Module::t('settings', '{attribute} "{value}" already exists for this section.')
78 24
            ],
79 24
            ['type', 'in', 'range' => array_keys($this->getTypes(false))],
80 24
            [['type', 'created', 'modified'], 'safe'],
81 24
            [['active'], 'boolean'],
82 24
        ];
83
    }
84
85 22
    public function beforeSave($insert)
86
    {
87 22
        $validators = $this->getTypes(false);
88 22
        if (!array_key_exists($this->type, $validators)) {
89
            $this->addError('type', Module::t('settings', 'Please select correct type'));
90
            return false;
91
        }
92
93 22
        $model = DynamicModel::validateData([
94 22
            'value' => $this->value
95 22
        ], [
96 22
            $validators[$this->type],
97 22
        ]);
98
99 22
        if ($model->hasErrors()) {
100 1
            $this->addError('value', $model->getFirstError('value'));
101 1
            return false;
102
        }
103
104 22
        if ($this->hasErrors()) {
105
            return false;
106
        }
107
108 22
        return parent::beforeSave($insert);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 4
    public function attributeLabels()
115
    {
116
        return [
117 4
            'id' => Module::t('settings', 'ID'),
118 4
            'type' => Module::t('settings', 'Type'),
119 4
            'section' => Module::t('settings', 'Section'),
120 4
            'key' => Module::t('settings', 'Key'),
121 4
            'value' => Module::t('settings', 'Value'),
122 4
            'active' => Module::t('settings', 'Active'),
123 4
            'created' => Module::t('settings', 'Created'),
124 4
            'modified' => Module::t('settings', 'Modified'),
125 4
        ];
126
    }
127
}
128