SettingsValue::behaviors()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 114
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 62
c 1
b 1
f 0
dl 0
loc 114
rs 7.2735
cc 9
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace execut\import\models;
4
use execut\crudFields\Behavior;
5
use execut\crudFields\BehaviorStub;
6
use execut\crudFields\fields\DropDown;
7
use execut\crudFields\fields\HasOneSelect2;
8
use execut\crudFields\fields\Hidden;
9
use execut\crudFields\fields\NumberField;
10
use execut\crudFields\ModelsHelperTrait;
11
use yii\behaviors\TimestampBehavior;
12
use yii\db\Expression;
13
use yii\helpers\ArrayHelper;
14
use yii\helpers\Url;
15
use yii\web\JsExpression;
16
17
/**
18
 * This is the model class for table "import_settings_values".
19
 *
20
 * @property integer $id
21
 * @property string $created
22
 * @property string $updated
23
 * @property string $name
24
 * @property string $type
25
 * @property string $column_nbr
26
 * @property string $format
27
 * @property string $value_string
28
 * @property string $value_option
29
 * @property string $import_settings_set_id
30
 *
31
 * @property \execut\import\models\SettingsSet $settingsSet
32
 */
33
class SettingsValue extends base\SettingsValue
34
{
35
    use BehaviorStub, ModelsHelperTrait;
36
37
    /**
38
     * @return \execut\import\models\queries\SettingsValue
39
     */
40
    public static function find()
41
    {
42
        return new \execut\import\models\queries\SettingsValue(static::class);
43
    }
44
45
    public function behaviors()
46
    {
47
        return [
48
            'fields' => [
49
                'class' => Behavior::class,
50
                'module' => 'import',
51
                'fields' => [
52
                    'import_settings_set_id' => [
53
                        'class' => Hidden::class,
54
                        'attribute' => 'import_settings_set_id',
55
                    ],
56
                    'type' => [
57
                        'class' => DropDown::class,
58
                        'attribute' => 'type',
59
                        'field' => [
60
                            'options' => [
61
                                'style' => 'width: 100px',
62
                            ]
63
                        ],
64
                        'data' => function () {
65
                            return self::getAttributesValuesTypesList();
66
                        },
67
                    ],
68
                    'column_nbr' => [
69
                        'class' => NumberField::class,
70
                        'attribute' => 'column_nbr',
71
                    ],
72
                    'number_delimiter' => [
73
                        'attribute' => 'number_delimiter',
74
                        'required' => true,
75
                        'defaultValue' => ',',
76
                    ],
77
                    'value_string' => [
78
                        'attribute' => 'value_string',
79
                    ],
80
                    'value_option' => [
81
                        'class' => HasOneSelect2::class,
82
                        'attribute' => 'value_option',
83
                        'nameParam' => 'name',
84
//                        'relation' => 'dictionariesData',
85
                        'data' => function () {
86
                            if (!$this->settingsSet || !$this->settingsSet->settingsSheet || !$this->settingsSet->settingsSheet->settingsSets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->settingsSet->settingsSheet->settingsSets of type execut\import\models\SettingsSet[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
                                return [];
88
                            }
89
                            $dictionaryOptions = ['' => ''];
90
                            $types = SettingsSheet::getDictionaries();
91
//                            return [];
92
                            foreach ($this->settingsSet->settingsSheet->settingsSets as $settingsSet) {
93
                                foreach ($settingsSet->settingsValues as $value) {
94
                                    $type = explode('.', $value->type)[0];
95
                                    if (!empty($types[$type]) && !empty($value->value_option)) {
96
                                        if ($model = $types[$type]->byId($value->value_option)->one()) {
97
                                            $dictionaryOptions[$model->id] = $model->name;
98
                                        }
99
                                    }
100
                                }
101
                            }
102
103
                            return $dictionaryOptions;
104
                        },
105
                        'isNoRenderRelationLink' => true,
106
                        'widgetOptions' => [
107
                            'url' => ['get-dictionaries'],
108
                            'pluginOptions' => [
109
                                'width' => 'auto',
110
                                'ajax' => [
111
                                    'data' => new JsExpression(<<<JS
112
function(params) {
113
var currentType = $(this).parents('.multiple-input-list__item:first').find('.list-cell__type select').val();
114
return {
115
    name: params.term,
116
    type: currentType,
117
    page: params.page
118
};
119
}
120
JS
121
                                    ),
122
                                ],
123
                            ],
124
                        ],
125
//                        'data' => [],
126
//                        'options' => [
127
//                            'initValueText' => $dictionaryOptions,
128
//                            'options' => [
129
//                                'placeholder' => 'Dictionary'
130
//                            ],
131
//                            'pluginOptions' => [
132
//                                'allowClear' => true,
133
//                                'ajax' => [
134
//                                    'url' => $getDictionariesUrl,
135
//                                    'dataType' => 'json',
136
//                                    'data' => new JsExpression(<<<JS
137
//function(params) {
138
//    var currentType = $(this).parents('.multiple-input-list__item:first').find('.list-cell__type select').val();
139
//    return {
140
//        name: params.term,
141
//        type: currentType
142
//    };
143
//}
144
//JS
145
//                                    )
146
//                                ],
147
//                            ],
148
//                        ],
149
//                                        'enableError' => true,
150
                    ],
151
                ],
152
//                'plugins' => \yii::$app->getModule('import')->getSettingsSetsCrudFieldsPlugins(),
153
            ],
154
            'date' => [
155
                'class' => TimestampBehavior::class,
156
                'createdAtAttribute' => 'created',
157
                'updatedAtAttribute' => 'updated',
158
                'value' => new Expression('NOW()'),
159
            ],
160
        ];
161
    }
162
163
    public static function getAttributesValuesTypesList() {
164
        return \yii::$app->getModule('import')->getAttributesValuesTypesList();
165
    }
166
167
    public function delete()
168
    {
169
        foreach ($this->logs as $importLog) {
170
            $importLog->delete();
171
        }
172
173
        return parent::delete(); // TODO: Change the autogenerated stub
174
    }
175
176
    public function rules()
177
    {
178
        $rules = $this->getBehavior('fields')->rules();
179
180
        return ArrayHelper::merge([
181
            [['type'], 'validateValuesFields'],
182
        ], $rules);
183
    }
184
185
    public function validateValuesFields() {
186
        $variants = [
187
            'column_nbr',
188
            'value_option',
189
            'value_string'
190
        ];
191
        $notEmptyCount = 0;
192
        foreach ($variants as $variant) {
193
            if ($variant === 'column_nbr' && $this->$variant === '0' || !empty($this->$variant)) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($variant === 'column_nb... empty($this->$variant), Probably Intended Meaning: $variant === 'column_nbr...empty($this->$variant))
Loading history...
194
                $notEmptyCount++;
195
            }
196
        }
197
198
        if ($notEmptyCount != 1) {
199
            $this->addError('type', 'Enter one of fields: column nbr, text value or dictionary option for attribute value');
200
        }
201
    }
202
}
203