Option::tableName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace common\models;
9
10
use common\components\Json;
11
use Yii;
12
use yii\db\ActiveRecord;
13
14
/**
15
 * This is the model class for table "{{%option}}".
16
 *
17
 * @property integer $id
18
 * @property string $name
19
 * @property string $value
20
 * @property string $label
21
 * @property string $group
22
 *
23
 * @author Agiel K. Saputra <[email protected]>
24
 * @since 0.1.0
25
 */
26
class Option extends ActiveRecord
27
{
28
    /**
29
     * @inheritdoc
30
     */
31
    public static function tableName()
32
    {
33
        return '{{%option}}';
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        return [
42
            [['name', 'value'], 'required'],
43
            ['value', 'string'],
44
            [['name', 'label', 'group'], 'string', 'max' => 64],
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function attributeLabels()
52
    {
53
        return [
54
            'id' => Yii::t('writesdown', 'ID'),
55
            'name' => Yii::t('writesdown', 'Name'),
56
            'value' => Yii::t('writesdown', 'Value'),
57
            'label' => Yii::t('writesdown', 'Label'),
58
            'group' => Yii::t('writesdown', 'Group'),
59
        ];
60
    }
61
62
    /**
63
     * Get option value.
64
     * The return value array|boolean|string depends on value.
65
     * If value is string then the return value is a string.
66
     * If value is array|object then return value is array.
67
     * If name not found in table then it will return false.
68
     *
69
     * @param string $name
70
     * @return string|array|boolean
71
     */
72 View Code Duplication
    public static function get($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        /* @var $model \common\models\Option */
75
        $model = static::findOne(['name' => $name]);
76
77
        if ($model) {
78
            if (Json::isJson($model->value)) {
79
                return Json::decode($model->value);
80
            }
81
82
            return $model->value;
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * Add new option, required name and value.
90
     * If value is array or object, it will be converted to json with Json::encode.
91
     *
92
     * @param string $name
93
     * @param string $value
94
     * @param string|array $label
95
     * @param string $group
96
     * @return bool
97
     */
98
    public static function set($name, $value, $label = null, $group = null)
99
    {
100
        if (is_array($value) || is_object($value)) {
101
            $value = Json::encode($value);
102
        }
103
104
        if (static::get($name) !== null) {
105
            return static::up($name, $value);
106
        }
107
108
        $model = new Option([
109
            'name' => $name,
110
            'value' => $value,
111
            'label' => $label,
112
            'group' => $group,
113
        ]);
114
115
        return $model->save();
116
    }
117
118
    /**
119
     * Update option with name as key.
120
     *
121
     * @param string $name
122
     * @param string|array $value
123
     * @return bool
124
     */
125
    public static function up($name, $value)
126
    {
127
        /* @var $model \common\models\Option */
128
        $model = static::findOne(['name' => $name]);
129
130
        if (is_array($value) || is_object($value)) {
131
            $model->value = Json::encode($value);
132
        } else {
133
            $model->value = $value;
134
        }
135
136
        return $model->save();
137
    }
138
139
    /**
140
     * Get menu item to render in admin sidebar left.
141
     *
142
     * @param int $position
143
     * @return array
144
     */
145
    public static function getMenus($position = 30)
146
    {
147
        $items[$position] = ['label' => Yii::t('writesdown', 'Settings'), 'icon' => 'fa fa-cogs'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
148
        $items[$position]['items'] = static::getSubmenus();
149
150
        return $items;
151
    }
152
153
    /**
154
     * The option will be grouped into group to create new submenu item.
155
     *
156
     * @return array|null
157
     */
158
    protected static function getSubmenus()
159
    {
160
        /* @var $model \common\models\Option */
161
        $models = static::find()
162
            ->groupBy('group')
163
            ->andWhere(['<>', 'group', ''])
164
            ->orderBy(['id' => SORT_ASC])
165
            ->all();
166
        $items = null;
167
168
        foreach ($models as $model) {
169
            $items[] = [
170
                'icon' => 'fa fa-circle-o',
171
                'label' => Yii::t('writesdown', ucwords($model->group)),
172
                'url' => ['/setting/group/', 'id' => strtolower($model->group)],
173
                'visible' => Yii::$app->user->can('administrator'),
174
            ];
175
        }
176
177
        return $items;
178
    }
179
}
180