Total Complexity | 54 |
Total Lines | 301 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like PushRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PushRecord, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class PushRecord extends ActiveRecord |
||
55 | { |
||
56 | const STATUS_STOPPED = 'stopped'; |
||
57 | const STATUS_WAITING = 'waiting'; |
||
58 | const STATUS_STARTED = 'started'; |
||
59 | const STATUS_DONE = 'done'; |
||
60 | const STATUS_FAILED = 'failed'; |
||
61 | const STATUS_RESTARTED = 'restarted'; |
||
62 | const STATUS_BURIED = 'buried'; |
||
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | * @return PushQuery the active query used by this AR class. |
||
67 | */ |
||
68 | public static function find() |
||
69 | { |
||
70 | return Yii::createObject(PushQuery::class, [get_called_class()]); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public static function getDb() |
||
77 | { |
||
78 | return Env::ensure()->db; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | public static function tableName() |
||
85 | { |
||
86 | return Env::ensure()->pushTableName; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return PushQuery |
||
91 | */ |
||
92 | public function getParent() |
||
93 | { |
||
94 | return $this->hasOne(static::class, ['id' => 'parent_id']); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return PushQuery |
||
99 | */ |
||
100 | public function getChildren() |
||
101 | { |
||
102 | return $this->hasMany(static::class, ['parent_id' => 'id']); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return ExecQuery |
||
107 | */ |
||
108 | public function getExecs() |
||
109 | { |
||
110 | return $this->hasMany(ExecRecord::class, ['push_id' => 'id']); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return ExecQuery |
||
115 | */ |
||
116 | public function getFirstExec() |
||
117 | { |
||
118 | return $this->hasOne(ExecRecord::class, ['id' => 'first_exec_id']); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return ExecQuery |
||
123 | */ |
||
124 | public function getLastExec() |
||
125 | { |
||
126 | return $this->hasOne(ExecRecord::class, ['id' => 'last_exec_id']); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return ExecQuery |
||
131 | */ |
||
132 | public function getExecTotal() |
||
133 | { |
||
134 | return $this->hasOne(ExecRecord::class, ['push_id' => 'id']) |
||
135 | ->select([ |
||
136 | 'exec.push_id', |
||
137 | 'attempts' => 'COUNT(*)', |
||
138 | 'errors' => 'COUNT(exec.error)', |
||
139 | ]) |
||
140 | ->groupBy('exec.push_id') |
||
141 | ->asArray(); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return int number of attempts |
||
146 | */ |
||
147 | public function getAttemptCount() |
||
148 | { |
||
149 | return ArrayHelper::getValue($this->execTotal, 'attempts', 0); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return int waiting time from push till first execute |
||
154 | */ |
||
155 | public function getWaitTime() |
||
156 | { |
||
157 | if ($this->firstExec) { |
||
158 | return $this->firstExec->started_at - $this->pushed_at - $this->delay; |
||
159 | } |
||
160 | return time() - $this->pushed_at - $this->delay; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getStatus() |
||
167 | { |
||
168 | if ($this->isStopped()) { |
||
169 | return self::STATUS_STOPPED; |
||
170 | } |
||
171 | if (!$this->lastExec) { |
||
172 | return self::STATUS_WAITING; |
||
173 | } |
||
174 | if (!$this->lastExec->isDone() && $this->lastExec->attempt == 1) { |
||
175 | return self::STATUS_STARTED; |
||
176 | } |
||
177 | if ($this->lastExec->isDone() && !$this->lastExec->isFailed()) { |
||
178 | return self::STATUS_DONE; |
||
179 | } |
||
180 | if ($this->lastExec->isDone() && $this->lastExec->retry) { |
||
181 | return self::STATUS_FAILED; |
||
182 | } |
||
183 | if (!$this->lastExec->isDone()) { |
||
184 | return self::STATUS_RESTARTED; |
||
185 | } |
||
186 | if ($this->lastExec->isDone() && !$this->lastExec->retry) { |
||
|
|||
187 | return self::STATUS_BURIED; |
||
188 | } |
||
189 | return null; |
||
190 | } |
||
191 | |||
192 | public function getStatusLabel($label) |
||
193 | { |
||
194 | $labels = [ |
||
195 | self::STATUS_STOPPED => Module::t('main', 'Stopped'), |
||
196 | self::STATUS_BURIED => Module::t('main', 'Buried'), |
||
197 | self::STATUS_DONE => Module::t('main', 'Done'), |
||
198 | self::STATUS_FAILED => Module::t('main', 'Failed'), |
||
199 | self::STATUS_RESTARTED => Module::t('main', 'Restarted'), |
||
200 | self::STATUS_STARTED => Module::t('main', 'Started'), |
||
201 | self::STATUS_WAITING => Module::t('main', 'Waiting'), |
||
202 | ]; |
||
203 | if (!isset($labels[$label])) { |
||
204 | throw new InvalidArgumentException('label not found'); |
||
205 | } |
||
206 | return $labels[$label]; |
||
207 | } |
||
208 | /** |
||
209 | * @return Queue|null |
||
210 | */ |
||
211 | public function getSender() |
||
212 | { |
||
213 | return Yii::$app->get($this->sender_name, false); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function isSenderValid() |
||
220 | { |
||
221 | return $this->getSender() instanceof Queue; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param JobInterface|mixed $job |
||
226 | */ |
||
227 | public function setJob($job) |
||
228 | { |
||
229 | $this->job_class = get_class($job); |
||
230 | $data = []; |
||
231 | foreach (get_object_vars($job) as $name => $value) { |
||
232 | $data[$name] = $this->serializeData($value); |
||
233 | } |
||
234 | $this->job_data = Json::encode($data); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return array of job properties |
||
239 | */ |
||
240 | public function getJobParams() |
||
241 | { |
||
242 | if (is_resource($this->job_data)) { |
||
243 | $this->job_data = stream_get_contents($this->job_data); |
||
244 | } |
||
245 | $params = []; |
||
246 | foreach (Json::decode($this->job_data) as $name => $value) { |
||
247 | $params[$name] = $this->unserializeData($value); |
||
248 | } |
||
249 | return $params; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function isJobValid() |
||
256 | { |
||
257 | return is_subclass_of($this->job_class, JobInterface::class); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return JobInterface|mixed |
||
262 | */ |
||
263 | public function createJob() |
||
264 | { |
||
265 | return Yii::createObject(['class' => $this->job_class] + $this->getJobParams()); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function canPushAgain() |
||
272 | { |
||
273 | return $this->isSenderValid() && $this->isJobValid(); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return bool marked as stopped |
||
278 | */ |
||
279 | public function isStopped() |
||
280 | { |
||
281 | return !!$this->stopped_at; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return bool ability to mark as stopped |
||
286 | */ |
||
287 | public function canStop() |
||
288 | { |
||
289 | if ($this->isStopped()) { |
||
290 | return false; |
||
291 | } |
||
292 | if ($this->lastExec && $this->lastExec->isDone() && !$this->lastExec->retry) { |
||
293 | return false; |
||
294 | } |
||
295 | return true; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Marks as stopped |
||
300 | */ |
||
301 | public function stop() |
||
302 | { |
||
303 | $this->stopped_at = time(); |
||
304 | $this->save(false); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param mixed $data |
||
309 | * @return mixed |
||
310 | */ |
||
311 | private function serializeData($data) |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param mixed $data |
||
333 | * @return mixed |
||
334 | */ |
||
335 | private function unserializeData($data) |
||
336 | { |
||
337 | if (!is_array($data)) { |
||
338 | return $data; |
||
339 | } |
||
340 | |||
341 | if (!isset($data['=class='])) { |
||
342 | $result = []; |
||
343 | foreach ($data as $key => $value) { |
||
344 | $result[$key] = $this->unserializeData($value); |
||
355 | } |
||
356 | } |
||
357 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.