Passed
Push — master ( a4dac5...e33ee2 )
by eXeCUT
03:28
created

File::getTypeCastFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace execut\import\models\queries;
4
5
use execut\import\models;
6
use yii\base\Exception;
7
use yii\db\ActiveQuery;
8
use yii\db\Expression;
9
use yii\db\mysql\Schema;
10
use yii\web\UploadedFile;
11
12
/**
13
 * This is the model class for table "import_files".
14
 *
15
 * @property integer $id
16
 * @property string $created
17
 * @property string $updated
18
 * @property string $name
19
 * @property resource $content
20
 * @property string $md5
21
 * @property string $import_files_source_id
22
 * @property string $use_id
23
 * @property UploadedFile $contentFile
24
 *
25
 * @property \execut\import\models\FilesSource $importFilesSource
26
 * @property \execut\import\models\User $use
27
 */
28
class File extends ActiveQuery
29
{
30
    public function isNew() {
31
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isNew()->select('id'));
32
    }
33
34
    public function isDelete() {
35
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isDelete()->select('id'));
36
    }
37
38
    public function isLoading() {
39
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isLoading()->select('id'));
40
    }
41
42
    public function isReloading() {
43
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isReloading()->select('id'));
0 ignored issues
show
Bug introduced by eXeCUT
The method isReloading() does not exist on execut\import\models\queries\FilesStatuse. 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

43
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->/** @scrutinizer ignore-call */ isReloading()->select('id'));
Loading history...
44
    }
45
46
    public function isForClean() {
47
        $keys = [models\FilesStatuse::DELETE];
48
49
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->byKey($keys)->select('id'));
50
    }
51
52
    public function isForImport() {
53
        $keys = [models\FilesStatuse::RELOAD, models\FilesStatuse::NEW];
54
55
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->byKey($keys)->select('id'));
56
    }
57
58
    public function byMd5($md5) {
59
        return $this->andWhere([
60
            'md5' => $md5,
61
        ]);
62
    }
63
64
    public function isStop() {
65
        return $this->byImportFilesStatuse_Key([models\FilesStatuse::STOP]);
66
    }
67
68
    public function isCancelLoading() {
69
        return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isNotLoading()->select('id'));
70
    }
71
72
    public function byImportFilesStatuse_Key($key) {
73
        return $this->andWhere([
74
            'import_files_statuse_id' => models\FilesStatuse::find()->byKey($key)->select('id'),
75
        ]);
76
    }
77
78
    public function byImportFilesStatuseId($id) {
79
        return $this->andWhere([
80
            'import_files_statuse_id' => $id,
81
        ]);
82
    }
83
84
    public function withErrorsPercent() {
85
        if ($this->select === null) {
86
            $this->select = ['*'];
87
        }
88
89
        $typeCast = $this->getTypeCastFunction();
90
        $this->select['errorsPercent'] = new Expression('CASE WHEN (rows_success is not null OR rows_errors is not null) AND (rows_success + rows_errors) > 0 THEN rows_errors' . $typeCast . '  / (rows_success + rows_errors) ELSE 0 END');
91
92
        return $this;
93
    }
94
95
    public function withProgress() {
96
        if ($this->select === null) {
97
            $this->select = ['*'];
98
        }
99
100
        $typeCast = $this->getTypeCastFunction();
101
        $this->select['progress'] = new Expression('CASE WHEN rows_count > 0 THEN (rows_success + rows_errors)' . $typeCast . ' / rows_count ELSE 0 END');
102
103
        return $this;
104
    }
105
106
    public function isOnlyFresh() {
107
        $modelClass = $this->modelClass;
108
        if ($modelClass::getDb()->schema instanceof Schema) {
109
            $subQuery = 'SELECT id FROM ' . $this->getPrimaryTableName() . ' GROUP BY import_setting_id ORDER BY import_setting_id, created DESC';
110
        } else {
111
            $subQuery = 'SELECT DISTINCT ON (import_setting_id) id FROM ' . $this->getPrimaryTableName() . ' ORDER BY import_setting_id, created DESC';
112
        }
113
114
        return $this->andWhere($this->getPrimaryTableName() . '.id IN (' . $subQuery . ')');
115
    }
116
117
    public function byEventsCount($count) {
118
        if ($count === '1') {
119
            $this->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) > 0');
120
        } else if ($count === '0') {
121
            $this->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) = 0');
122
        }
123
124
        return $this;
125
    }
126
127
    public function withEventsCount() {
128
        $vsSql = '(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id)';
129
        $this->select['eventsCount'] = $vsSql;
130
131
        return $this;
132
    }
133
134
    public function byImportSettingId($id) {
135
        return $this->andWhere([
136
            'import_setting_id' => $id,
137
        ]);
138
    }
139
140
    public function byId($id) {
141
        return $this->andWhere([
142
            'id' => $id,
143
        ]);
144
    }
145
146
    public function byHostName($name) {
0 ignored issues
show
Unused Code introduced by eXeCUT
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

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

146
    public function byHostName(/** @scrutinizer ignore-unused */ $name) {

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

Loading history...
147
        return $this;
148
    }
149
150
    protected function getPhpProcessesIds() {
151
        exec('pidof php', $output);
152
        if (empty($output[0])) {
153
            return [];
154
        }
155
156
        return explode(' ', $output[0]);
157
    }
158
159
    public function isWithoutProcess() {
160
        $processesIds = $this->getPhpProcessesIds();
161
        return $this->andWhere([
162
            'NOT IN',
163
            'process_id',
164
            $processesIds
165
        ]);
166
    }
167
168
    public function isInProgress() {
169
        return $this->andWhere([
170
            'import_files_statuse_id' => models\FilesStatuse::find()->byKey([
171
                models\FilesStatuse::DELETING,
172
                models\FilesStatuse::LOADING,
173
                models\FilesStatuse::STOP,
174
            ])->select('id')
175
        ]);
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    protected function getTypeCastFunction()
182
    {
183
        $modelClass = $this->modelClass;
184
        if ($modelClass::getDb()->schema instanceof Schema) {
185
            $typeCast = '';
186
        } else {
187
            $typeCast = '::float';
188
        }
189
190
        return $typeCast;
191
    }
192
}
193