Passed
Push — master ( fa7dbb...eaf80d )
by eXeCUT
03:51 queued 11s
created

Setting::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 48
rs 9.36
cc 4
nc 4
nop 2
1
<?php
2
3
namespace execut\import\models;
4
5
use execut\crudFields\Behavior;
6
use execut\crudFields\BehaviorStub;
7
use execut\crudFields\fields\Boolean;
8
use execut\crudFields\fields\DropDown;
9
use execut\crudFields\fields\Email;
10
use execut\crudFields\fields\Group;
11
use execut\crudFields\fields\HasManyMultipleInput;
12
use execut\crudFields\fields\NumberField;
13
use execut\crudFields\fields\reloader\Reloader;
0 ignored issues
show
Bug introduced by
The type execut\crudFields\fields\reloader\Reloader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use execut\crudFields\fields\reloader\Target;
0 ignored issues
show
Bug introduced by
The type execut\crudFields\fields\reloader\Target was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use execut\crudFields\fields\reloader\type\Dependent;
0 ignored issues
show
Bug introduced by
The type execut\crudFields\fields\reloader\type\Dependent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use execut\crudFields\ModelsHelperTrait;
17
use execut\import\components\Source;
18
use kartik\detail\DetailView;
19
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
20
use Yii;
21
use yii\behaviors\TimestampBehavior;
22
use yii\db\ActiveRecord;
23
use yii\db\Expression;
24
use yii\helpers\ArrayHelper;
25
use yii\helpers\Json;
26
27
/**
28
 * This is the model class for table "import_settings".
29
 *
30
 * @property integer $id
31
 * @property string $created
32
 * @property string $updated
33
 * @property string $name
34
 * @property integer $ignored_lines
35
 * @property string $email
36
 * @property string $email_title_match
37
 * @property string $csv_delimiter
38
 * @property string $csv_enclosure
39
 * @property string $import_files_source_id
40
 *
41
 * @property \execut\import\models\File[] $importFiles
42
 * @property \execut\import\models\FilesSource $importFilesSource
43
 * @property \execut\import\models\SettingsSheet[] $importSettingsSheets
44
 */
45
class Setting extends ActiveRecord
46
{
47
    use ModelsHelperTrait, BehaviorStub;
48
    const MODEL_NAME = '{n,plural,=0{Setting} =1{Setting} other{Settings}}';
49
50
    public function rules()
51
    {
52
        return $this->getBehavior('fields')->rules();
53
    }
54
55
    public static function find()
56
    {
57
        return new queries\Setting(static::class);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public static function tableName()
64
    {
65
        return 'import_settings';
66
    }
67
68
    public function getSource() {
69
        $adapter = $this->filesSource->getAdapterForSetting($this);
0 ignored issues
show
Bug Best Practice introduced by
The property filesSource does not exist on execut\import\models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
70
        $source = new Source([
71
            'adapter' => $adapter,
72
        ]);
73
74
        return $source;
75
    }
76
77
    public function load($data, $formName = null)
78
    {
79
        $result = parent::load($data, $formName); // TODO: Change the autogenerated stub
80
        $groupsVsTypes = [
81
            FilesSource::TYPE_EMAIL => [
82
                'emailGroup',
83
                'email',
84
                'email_title_match',
85
                'email_attachment_template'
86
            ],
87
            FilesSource::TYPE_FTP => [
88
                'ftpGroup',
89
                'ftp_host',
90
                'ftp_ssl',
91
                'ftp_port',
92
                'ftp_timeout',
93
                'ftp_login',
94
                'ftp_password',
95
                'ftp_dir',
96
                'ftp_file_name',
97
            ],
98
            FilesSource::TYPE_SITE => [
99
                'siteSettingsGroup',
100
                'site_host',
101
                'site_auth_url',
102
                'site_auth_method',
103
                'site_login_field',
104
                'site_password_field',
105
                'site_other_fields',
106
                'site_login',
107
                'site_password',
108
                'site_file_url',
109
            ],
110
        ];
111
        foreach ($groupsVsTypes as $typeKey => $groupFields) {
112
            if (!$this->isKeyOfFileSource($typeKey)) {
113
                foreach ($groupFields as $groupFieldName) {
114
                    /**
115
                     * @var Group $emailGroup
116
                     */
117
                    $this->getField($groupFieldName)
0 ignored issues
show
Bug introduced by
The method getField() does not exist on execut\import\models\Setting. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                    $this->/** @scrutinizer ignore-call */ 
118
                           getField($groupFieldName)
Loading history...
118
                        ->getDetailViewField()
119
                        ->hide();
120
                }
121
            }
122
        }
123
124
        return $result;
125
    }
126
127
    public function isKeyOfFileSource($key) {
128
        if (!$this->import_files_source_id) {
129
            return false;
130
        }
131
132
        return (int) $this->import_files_source_id == FilesSource::getIdByKey($key);
133
    }
134
135
    public function behaviors()
136
    {
137
        $filesSource = new DropDown([
138
            'attribute' => 'import_files_source_id',
139
            'relation' => 'filesSource',
140
            'required' => true,
141
        ]);
142
        $standardFields = $this->getStandardFields(['visible'], [
143
            'ignored_lines' => [
144
                'class' => NumberField::class,
145
                'attribute' => 'ignored_lines',
146
                'required' => true,
147
            ],
148
            'is_check_mime_type' => [
149
                'class' => Boolean::class,
150
                'attribute' => 'is_check_mime_type',
151
            ],
152
            'import_files_source_id' => $filesSource,
153
            'import_files_encoding_id' => [
154
                'class' => DropDown::class,
155
                'attribute' => 'import_files_encoding_id',
156
                'relation' => 'filesEncoding',
157
                'required' => true,
158
            ],
159
        ]);
160
161
162
        $emailTarget = new Target($filesSource);
163
//        $emailTarget->setValues([
164
//            function () {
165
//                return FilesSource::getIdByKey(FilesSource::TYPE_EMAIL);
166
//            }
167
//        ]);
168
169
        $reloader = new Reloader(new Dependent(), [$emailTarget]);
170
        $emailGroup = new Group([
171
            'label' => 'Настройки Email',
172
            'reloaders' => [$reloader],
173
            'name' => 'emailGroup',
174
        ]);
175
176
        $standardFields = ArrayHelper::merge($standardFields, [
177
            'emailGroup' => $emailGroup,
178
            'email' => [
179
                'class' => Email::class,
180
                'attribute' => 'email',
181
                'reloaders' => [$reloader],
182
            ],
183
            'email_title_match' => [
184
                'attribute' => 'email_title_match',
185
                'reloaders' => [$reloader],
186
            ],
187
            'email_attachment_template' => [
188
                'attribute' => 'email_attachment_template',
189
                'reloaders' => [$reloader],
190
            ],
191
        ]);
192
193
        $standardFields = ArrayHelper::merge($standardFields, [
194
            'csvGroup' => [
195
                'class' => Group::class,
196
                'label' => 'Настройки Csv',
197
                'reloaders' => [$reloader],
198
                'name' => 'csvGroup',
199
            ],
200
            'csv_delimiter' => [
201
                'attribute' => 'csv_delimiter',
202
                'reloaders' => [$reloader],
203
            ],
204
            'csv_enclosure' => [
205
                'attribute' => 'csv_enclosure',
206
                'reloaders' => [$reloader],
207
            ],
208
            'ftpGroup' => [
209
                'class' => Group::class,
210
                'label' => 'Настройки FTP',
211
                'reloaders' => [$reloader],
212
                'name' => 'ftpGroup',
213
            ],
214
            'ftp_host' => [
215
                'attribute' => 'ftp_host',
216
                'reloaders' => [$reloader],
217
            ],
218
            'ftp_ssl' => [
219
                'class' => Boolean::class,
220
                'attribute' => 'ftp_ssl',
221
                'defaultValue' => false,
222
                'reloaders' => [$reloader],
223
            ],
224
            'ftp_port' => [
225
                'attribute' => 'ftp_port',
226
                'reloaders' => [$reloader],
227
            ],
228
            'ftp_timeout' => [
229
                'attribute' => 'ftp_timeout',
230
                'reloaders' => [$reloader],
231
            ],
232
            'ftp_login' => [
233
                'attribute' => 'ftp_login',
234
                'reloaders' => [$reloader],
235
            ],
236
            'ftp_password' => [
237
                'attribute' => 'ftp_password',
238
                'reloaders' => [$reloader],
239
            ],
240
            'ftp_dir' => [
241
                'attribute' => 'ftp_dir',
242
                'reloaders' => [$reloader],
243
            ],
244
            'ftp_file_name' => [
245
                'attribute' => 'ftp_file_name',
246
                'reloaders' => [$reloader],
247
            ],
248
            'siteSettingsGroup' => [
249
                'class' => Group::class,
250
                'label' => 'Настройки Сайта',
251
                'name' => 'siteSettingsGroup',
252
                'reloaders' => [$reloader],
253
            ],
254
            'site_host' => [
255
                'attribute' => 'site_host',
256
                'reloaders' => [$reloader],
257
            ],
258
            'site_auth_url' => [
259
                'attribute' => 'site_auth_url',
260
                'reloaders' => [$reloader],
261
            ],
262
            'site_auth_method' => [
263
                'attribute' => 'site_auth_method',
264
                'reloaders' => [$reloader],
265
            ],
266
            'site_login_field' => [
267
                'attribute' => 'site_login_field',
268
                'reloaders' => [$reloader],
269
            ],
270
            'site_password_field' => [
271
                'attribute' => 'site_password_field',
272
                'reloaders' => [$reloader],
273
            ],
274
            'site_other_fields' => [
275
                'attribute' => 'site_other_fields',
276
                'reloaders' => [$reloader],
277
            ],
278
            'site_login' => [
279
                'attribute' => 'site_login',
280
                'reloaders' => [$reloader],
281
            ],
282
            'site_password' => [
283
                'attribute' => 'site_password',
284
                'reloaders' => [$reloader],
285
            ],
286
            'site_file_url' => [
287
                'attribute' => 'site_file_url',
288
                'reloaders' => [$reloader],
289
            ],
290
            'sheetsGroup' => [
291
                'class' => Group::class,
292
                'label' => 'Листы',
293
            ],
294
            'settingsSheets' => [
295
                'class' => HasManyMultipleInput::class,
296
                'attribute' => 'settingsSheets',
297
                'relation' => 'settingsSheets',
298
                'column' => false,
299
            ],
300
        ]);
301
302
        return [
303
            'relationsSaver' => [
304
                'class' => SaveRelationsBehavior::class,
305
                'relations' => [
306
                    'settingsSheets'
307
                ],
308
            ],
309
            'fields' => [
310
                'class' => Behavior::class,
311
                'module' => 'import',
312
                'fields' => $standardFields,
313
                'plugins' => \yii::$app->getModule('import')->getSettingsCrudFieldsPlugins(),
314
            ],
315
            'date' => [
316
                'class' => TimestampBehavior::class,
317
                'createdAtAttribute' => 'created',
318
                'updatedAtAttribute' => 'updated',
319
                'value' => new Expression('NOW()'),
320
            ],
321
        ];
322
    }
323
324
    public static function getFilesSourcesList() {
325
        return ArrayHelper::map(FilesSource::find()->all(), 'id', 'name');
326
    }
327
328
    public function getSiteOtherFields() {
329
        if (!empty($this->site_other_fields)) {
0 ignored issues
show
Bug Best Practice introduced by
The property site_other_fields does not exist on execut\import\models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
330
            return Json::decode($this->site_other_fields);
331
        }
332
    }
333
334
    public function delete()
335
    {
336
        foreach ($this->importSettingsVsSchedulerEvents as $event) {
0 ignored issues
show
Bug Best Practice introduced by
The property importSettingsVsSchedulerEvents does not exist on execut\import\models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
337
            $event->delete();
338
        }
339
340
        foreach ($this->settingsSheets as $sheet) {
0 ignored issues
show
Bug Best Practice introduced by
The property settingsSheets does not exist on execut\import\models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
341
            $sheet->delete();
342
        }
343
344
        foreach ($this->files as $sheet) {
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist on execut\import\models\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
345
            $sheet->delete();
346
        }
347
348
        return parent::delete(); // TODO: Change the autogenerated stub
349
    }
350
351
    public function __toString()
352
    {
353
        return '#' . $this->id . ' ' . $this->name;
354
    }
355
356
    /**
357
     * @return \yii\db\ActiveQuery
358
     */
359
    public function getFiles()
360
    {
361
        return $this->hasMany(\execut\import\models\File::class, ['import_setting_id' => 'id'])->inverseOf('importSetting');
362
    }
363
364
    /**
365
     * @return \yii\db\ActiveQuery
366
     */
367
    public function getFilesEncoding()
368
    {
369
        return $this->hasOne(\execut\import\models\FilesEncoding::class, ['id' => 'import_files_encoding_id'])->inverseOf('settings');
370
    }
371
372
    /**
373
     * @return \yii\db\ActiveQuery
374
     */
375
    public function getFilesSource()
376
    {
377
        return $this->hasOne(\execut\import\models\FilesSource::class, ['id' => 'import_files_source_id'])->inverseOf('settings');
378
    }
379
380
    /**
381
     * @return \yii\db\ActiveQuery
382
     */
383
    public function getSettingsSheets()
384
    {
385
        return $this->hasMany(\execut\import\models\SettingsSheet::class, ['import_setting_id' => 'id'])->inverseOf('setting');
386
    }
387
}
388