Completed
Push — master ( fab37c...20384e )
by Alex
02:09
created

Setting   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 107
ccs 42
cts 44
cp 0.9545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypes() 0 36 4
A rules() 0 17 1
A beforeSave() 0 25 4
A attributeLabels() 0 13 1
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\helpers\Json;
11
use yii\base\DynamicModel;
12
use pheme\settings\Module;
13
use yii\base\InvalidArgumentException;
14
15
/**
16
 * This is the model class for table "settings".
17
 *
18
 * @author Aris Karageorgos <[email protected]>
19
 */
20
class Setting extends BaseSetting
21
{
22
    /**
23
     * @param bool $forDropDown if false - return array or validators, true - key=>value for dropDown
24
     * @return array
25
     */
26 36
    public function getTypes($forDropDown = true)
27
    {
28
        $values = [
29 36
            'string' => ['value', 'string'],
30
            'integer' => ['value', 'integer'],
31
            'boolean' => ['value', 'boolean', 'trueValue' => "1", 'falseValue' => "0", 'strict' => true],
32
            'float' => ['value', 'number'],
33
            'email' => ['value', 'email'],
34
            'ip' => ['value', 'ip'],
35
            'url' => ['value', 'url'],
36
            'object' => [
37 36
                'value',
38 36
                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...
39 1
                    $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...
40
                    try {
41 1
                        Json::decode($this->$attribute);
42 1
                    } catch (InvalidArgumentException $e) {
43 1
                        $this->addError($attribute, Module::t('settings', '"{attribute}" must be a valid JSON object', [
44 1
                            'attribute' => $attribute,
45
                        ]));
46
                    }
47 36
                }
48
            ],
49
        ];
50
51 36
        if (!$forDropDown) {
52 36
            return $values;
53
        }
54
55 1
        $return = [];
56 1
        foreach ($values as $key => $value) {
57 1
            $return[$key] = Module::t('settings', $key);
58
        }
59
60 1
        return $return;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 36
    public function rules()
67
    {
68
        return [
69 36
            [['value'], 'string'],
70
            [['section', 'key'], 'string', 'max' => 255],
71
            [
72
                ['key'],
73 36
                'unique',
74
                'targetAttribute' => ['section', 'key'],
75
                'message' =>
76 36
                    Module::t('settings', '{attribute} "{value}" already exists for this section.')
77
            ],
78 36
            ['type', 'in', 'range' => array_keys($this->getTypes(false))],
79
            [['type', 'created', 'modified'], 'safe'],
80
            [['active'], 'boolean'],
81
        ];
82
    }
83
84 34
    public function beforeSave($insert)
85
    {
86 34
        $validators = $this->getTypes(false);
87 34
        if (!array_key_exists($this->type, $validators)) {
88
            $this->addError('type', Module::t('settings', 'Please select correct type'));
89
            return false;
90
        }
91
92 34
        $model = DynamicModel::validateData([
93 34
            'value' => $this->value
94
        ], [
95 34
            $validators[$this->type],
96
        ]);
97
98 34
        if ($model->hasErrors()) {
99 1
            $this->addError('value', $model->getFirstError('value'));
100 1
            return false;
101
        }
102
103 34
        if ($this->hasErrors()) {
104 1
            return false;
105
        }
106
107 34
        return parent::beforeSave($insert);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 4
    public function attributeLabels()
114
    {
115
        return [
116 4
            'id' => Module::t('settings', 'ID'),
117 4
            'type' => Module::t('settings', 'Type'),
118 4
            'section' => Module::t('settings', 'Section'),
119 4
            'key' => Module::t('settings', 'Key'),
120 4
            'value' => Module::t('settings', 'Value'),
121 4
            'active' => Module::t('settings', 'Active'),
122 4
            'created' => Module::t('settings', 'Created'),
123 4
            'modified' => Module::t('settings', 'Modified'),
124
        ];
125
    }
126
}
127