Passed
Push — master ( 34f3fe...502343 )
by Ioannes
08:17
created
src/Cron.php 1 patch
Spacing   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -42,45 +42,45 @@  discard block
 block discarded – undo
42 42
         set_time_limit(EnvHelper::getCrontabTimeout());
43 43
 
44 44
         $logger = EnvHelper::getLogger('bx_cron');
45
-        if($logger) {
45
+        if ($logger) {
46 46
             $this->setLogger($logger);
47 47
         }
48 48
 
49 49
         $showStatus = $input->getOption('status');
50 50
         $byTime = $input->getOption('bytime');
51
-        if($showStatus) {
51
+        if ($showStatus) {
52 52
             $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME);
53 53
             $this->showStatus($output, $sort);
54 54
             return 0;
55 55
         }
56 56
 
57
-        if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
58
-            if($this->logger) {
57
+        if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
58
+            if ($this->logger) {
59 59
                 $this->logger->alert('BxCron switch off');
60 60
             }
61 61
             return 0;
62 62
         }
63 63
 
64
-        if(!$this->lock()) {
64
+        if (!$this->lock()) {
65 65
             $msg = 'The command is already running in another process.';
66 66
             $output->writeln($msg);
67
-            if($this->logger) {
67
+            if ($this->logger) {
68 68
                 $this->logger->warning($msg);
69 69
             }
70 70
             return 0;
71 71
         }
72 72
 
73
-        if($sleepInterval = EnvHelper::checkSleepInterval()) {
73
+        if ($sleepInterval = EnvHelper::checkSleepInterval()) {
74 74
             $msg = sprintf("Sleep in interval %s", $sleepInterval);
75 75
             $output->writeln($msg);
76
-            if($this->logger) {
76
+            if ($this->logger) {
77 77
                 $this->logger->warning($msg);
78 78
             }
79 79
             return 0;
80 80
         }
81 81
 
82 82
         $clean = $input->getOption('clean');
83
-        if($clean) {
83
+        if ($clean) {
84 84
             $command = $this->getApplication()->find($clean);
85 85
             $this->cleanJob($command->getName());
86 86
             $output->writeln($command->getName() . " will be executed now");
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
         }
89 89
 
90 90
         $cleanAll = $input->getOption('all');
91
-        if($cleanAll) {
91
+        if ($cleanAll) {
92 92
             $this->cleanJob();
93 93
             $output->writeln("All commands will be executed now");
94 94
             return 0;
@@ -113,10 +113,10 @@  discard block
 block discarded – undo
113 113
         $lastExec = 0;
114 114
         $hasError = false;
115 115
 
116
-        foreach($jobs as $cmd => $job) {
116
+        foreach ($jobs as $cmd => $job) {
117 117
             $execTime = $job['last_exec'];
118
-            if($execTime > $lastExec) $lastExec = $execTime;
119
-            if(!empty($job['error'])) {
118
+            if ($execTime > $lastExec) $lastExec = $execTime;
119
+            if (!empty($job['error'])) {
120 120
                 $hasError = true;
121 121
             }
122 122
         }
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
             'Status',
136 136
         ];
137 137
 
138
-        if($hasError) {
138
+        if ($hasError) {
139 139
             $header[] = 'Error';
140 140
         }
141 141
 
@@ -145,15 +145,15 @@  discard block
 block discarded – undo
145 145
         ]);
146 146
 
147 147
         $cnt = 1;
148
-        foreach($jobs as $cmd => $job) {
149
-            if($cnt > 1) $table->addRow(new TableSeparator());
148
+        foreach ($jobs as $cmd => $job) {
149
+            if ($cnt > 1) $table->addRow(new TableSeparator());
150 150
             $row = [
151 151
                 $cmd,
152 152
                 $job['period'],
153 153
                 ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'),
154 154
                 $job['status'],
155 155
             ];
156
-            if($hasError) {
156
+            if ($hasError) {
157 157
                 $row[] = $job['error'];
158 158
             }
159 159
             $table->addRow($row);
@@ -167,9 +167,9 @@  discard block
 block discarded – undo
167 167
 
168 168
         $crontab = [];
169 169
 
170
-        if($command) {
170
+        if ($command) {
171 171
             $crontab = $this->getCronTab();
172
-            if($crontab === false) {
172
+            if ($crontab === false) {
173 173
                 return false;
174 174
             }
175 175
             unset($crontab[$command]);
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
         $allTimeout = EnvHelper::getCrontabTimeout();
185 185
         $workTime = 0;
186 186
 
187
-        if(!empty($jobs)) {
187
+        if (!empty($jobs)) {
188 188
 
189 189
             /*
190 190
              * Минимально допустимый период выполнения одной задачи
@@ -195,10 +195,10 @@  discard block
 block discarded – undo
195 195
             $this->logger->alert($msg);
196 196
             $output->writeln($msg);
197 197
 
198
-            foreach($jobs as $cmd => $job) {
198
+            foreach ($jobs as $cmd => $job) {
199 199
 
200 200
                 $job['cmd'] = $cmd;
201
-                if($this->isActualJob($job)) {
201
+                if ($this->isActualJob($job)) {
202 202
 
203 203
                     $job['status'] = self::EXEC_STATUS_WORK;
204 204
                     $job['start_time'] = time();
@@ -213,12 +213,12 @@  discard block
 block discarded – undo
213 213
                         $returnCode = $command->run($cmdInput, $output);
214 214
                         $execTime = microtime(true) - $timeStart;
215 215
 
216
-                        if(!$returnCode) {
216
+                        if (!$returnCode) {
217 217
 
218 218
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
219 219
 
220 220
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime);
221
-                            if($this->logger) {
221
+                            if ($this->logger) {
222 222
                                 $this->logger->alert($msg);
223 223
                             }
224 224
                             $output->writeln(PHP_EOL . $msg);
@@ -229,7 +229,7 @@  discard block
 block discarded – undo
229 229
                             $job['error_code'] = $returnCode;
230 230
 
231 231
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime);
232
-                            if($this->logger) {
232
+                            if ($this->logger) {
233 233
                                 $this->logger->alert($msg);
234 234
                             }
235 235
                             $output->writeln(PHP_EOL . $msg);
@@ -241,14 +241,14 @@  discard block
 block discarded – undo
241 241
                         $job['error'] = $e->getMessage();
242 242
 
243 243
 
244
-                        if($this->logger) {
244
+                        if ($this->logger) {
245 245
                             $this->logger->error($e, ['command' => $cmd]);
246 246
                         }
247 247
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
248 248
 
249 249
                     } finally {
250 250
 
251
-                        if(!$execTime) {
251
+                        if (!$execTime) {
252 252
                             $execTime = microtime(true) - $timeStart;
253 253
                         }
254 254
                         $job['last_exec'] = time();
@@ -258,7 +258,7 @@  discard block
 block discarded – undo
258 258
                     $this->updateJob($cmd, $job);
259 259
 
260 260
                     $workTime += $execTime;
261
-                    if($workTime * 2 > $allTimeout) {
261
+                    if ($workTime * 2 > $allTimeout) {
262 262
                         break;
263 263
                     }
264 264
                     /*
@@ -274,35 +274,35 @@  discard block
 block discarded – undo
274 274
     {
275 275
         $actual = false;
276 276
 
277
-        if($job['status'] == self::EXEC_STATUS_WORK) {
278
-            if($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) {
277
+        if ($job['status'] == self::EXEC_STATUS_WORK) {
278
+            if ($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) {
279 279
                 $actual = true;
280 280
             }
281 281
         }
282 282
 
283 283
         $period = intval($job['period']);
284 284
 
285
-        if($period > 0) {
286
-            if($period < $this->minAgentPeriod) {
285
+        if ($period > 0) {
286
+            if ($period < $this->minAgentPeriod) {
287 287
                 $job['orig_period'] = $period;
288 288
                 $period = $job['period'] = $this->minAgentPeriod;
289 289
             }
290
-            if(time() - $job['last_exec'] >= $period) {
290
+            if (time() - $job['last_exec'] >= $period) {
291 291
                 $actual = true;
292 292
             }
293
-        } else if(!empty($job['times'])) {
293
+        } else if (!empty($job['times'])) {
294 294
             //TODO:
295 295
         }
296 296
 
297
-        if($actual && !empty($job['interval'])) {
297
+        if ($actual && !empty($job['interval'])) {
298 298
             $times = explode('-', $job['interval']);
299
-            if(count($times) == 2) {
299
+            if (count($times) == 2) {
300 300
                 $minTime = Time24::validateTimeString($times[0]);
301 301
                 $maxTime = Time24::validateTimeString($times[1]);
302
-                if($minTime && $maxTime) {
303
-                    if(!Time24::inInterval($minTime, $maxTime)) {
302
+                if ($minTime && $maxTime) {
303
+                    if (!Time24::inInterval($minTime, $maxTime)) {
304 304
                         $msg = sprintf("%s not in interval %s", $job['cmd'], $job['interval']);
305
-                        if($this->logger) {
305
+                        if ($this->logger) {
306 306
                             $this->logger->alert($msg);
307 307
                         }
308 308
                         return false;
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
     public function getCronJobs(): array
317 317
     {
318 318
         $crontab = $this->getCronTab();
319
-        if($crontab === false) {
319
+        if ($crontab === false) {
320 320
             return [];
321 321
         }
322 322
 
@@ -326,9 +326,9 @@  discard block
 block discarded – undo
326 326
         $commands = $app->all();
327 327
 
328 328
         $selfCommands = [];
329
-        foreach($commands as $command) {
329
+        foreach ($commands as $command) {
330 330
             /** @var BxCommand $command */
331
-            if($command instanceof BxCommand) {
331
+            if ($command instanceof BxCommand) {
332 332
                 $name = $command->getName();
333 333
                 $selfCommands[$name] = [
334 334
                     'object' => $command,
@@ -338,19 +338,19 @@  discard block
 block discarded – undo
338 338
 
339 339
         $agents = [];
340 340
         $reader = new AnnotationReader();
341
-        foreach($selfCommands as $cmd => $selfCommand) {
341
+        foreach ($selfCommands as $cmd => $selfCommand) {
342 342
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
343 343
             $annotations = $reader->getClassAnnotations($reflectionClass);
344 344
 
345
-            foreach($annotations as $annotation) {
346
-                if($annotation instanceof Agent) {
345
+            foreach ($annotations as $annotation) {
346
+                if ($annotation instanceof Agent) {
347 347
                     $agents[$cmd] = $annotation->toArray();
348 348
                 }
349 349
             }
350 350
         }
351 351
 
352
-        foreach($crontab as $cmd => $job) {
353
-            if(is_array($job) && isset($agents[$cmd])) {
352
+        foreach ($crontab as $cmd => $job) {
353
+            if (is_array($job) && isset($agents[$cmd])) {
354 354
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
355 355
             }
356 356
         }
@@ -369,7 +369,7 @@  discard block
 block discarded – undo
369 369
 
370 370
         $crontab = $this->getCronTab();
371 371
 
372
-        if($crontab === false) {
372
+        if ($crontab === false) {
373 373
             return false;
374 374
         } else {
375 375
             $crontab = array_merge($crontab, $changedAgents);
@@ -387,7 +387,7 @@  discard block
 block discarded – undo
387 387
         $fh = fopen($filename, 'c');
388 388
         if (flock($fh, LOCK_EX)) {
389 389
             ftruncate($fh, 0);
390
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
390
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
391 391
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
392 392
             }
393 393
         } else {
@@ -407,16 +407,16 @@  discard block
 block discarded – undo
407 407
         $filename = EnvHelper::getCrontabFile();
408 408
 
409 409
         $fh = fopen($filename, 'r');
410
-        if(!$fh) {
410
+        if (!$fh) {
411 411
             return false;
412 412
         }
413
-        if(flock($fh, LOCK_SH)) {
413
+        if (flock($fh, LOCK_SH)) {
414 414
             $cronTab = [];
415 415
             clearstatcache();
416 416
             $filesize = (int) filesize($filename);
417
-            if($filesize > 0 && $data = fread($fh, $filesize)) {
417
+            if ($filesize > 0 && $data = fread($fh, $filesize)) {
418 418
                 $decoded = json_decode($data, true);
419
-                if(is_array($decoded)) {
419
+                if (is_array($decoded)) {
420 420
                     $cronTab = $decoded;
421 421
                 } else {
422 422
                     throw new \Exception("Unable to parse cronTab");
@@ -433,14 +433,14 @@  discard block
 block discarded – undo
433 433
 
434 434
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
435 435
 
436
-        if($sort == self::SORT_TIME) {
436
+        if ($sort == self::SORT_TIME) {
437 437
             $sorting = [];
438
-            foreach($crontab as $cmd => $data) {
438
+            foreach ($crontab as $cmd => $data) {
439 439
                 $sorting[$cmd] = $data['last_exec'];
440 440
             }
441 441
             arsort($sorting, SORT_NUMERIC);
442 442
             $sorted = [];
443
-            foreach($sorting as $cmd => $time) {
443
+            foreach ($sorting as $cmd => $time) {
444 444
                 $sorted[$cmd] = $crontab[$cmd];
445 445
             }
446 446
             $crontab = $sorted;
Please login to merge, or discard this patch.
src/Time24.php 1 patch
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -10,15 +10,15 @@  discard block
 block discarded – undo
10 10
     public function __construct($time = '') {
11 11
 
12 12
         $time = trim($time);
13
-        if(empty($time)) {
13
+        if (empty($time)) {
14 14
             \App\BxConsole\EnvHelper::timeZoneSet();
15 15
             $time = date("H:i:s");
16 16
         }
17 17
         $timeparts = array_map('intval', explode(':', $time));
18 18
 
19
-        if(isset($timeparts[0])) $this->setHours($timeparts[0]);
20
-        if(isset($timeparts[1])) $this->setMinutes($timeparts[1]);
21
-        if(isset($timeparts[2])) $this->setSeconds($timeparts[2]);
19
+        if (isset($timeparts[0])) $this->setHours($timeparts[0]);
20
+        if (isset($timeparts[1])) $this->setMinutes($timeparts[1]);
21
+        if (isset($timeparts[2])) $this->setSeconds($timeparts[2]);
22 22
     }
23 23
 
24 24
     /**
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
         $timeparts = [];
39 39
         $timeparts[] = sprintf("%02d", $this->hours);
40 40
         $timeparts[] = sprintf("%02d", $this->minutes);
41
-        if($seconds) {
41
+        if ($seconds) {
42 42
             $timeparts[] = sprintf("%02d", $this->seconds);
43 43
         }
44 44
         return join(':', $timeparts);
@@ -74,9 +74,9 @@  discard block
 block discarded – undo
74 74
     public function setHours(int $hours): void
75 75
     {
76 76
         $hours = (int) $hours;
77
-        if($hours < 0) {
77
+        if ($hours < 0) {
78 78
             $hours = 0;
79
-        } else if($hours > 23) {
79
+        } else if ($hours > 23) {
80 80
             $hours = 23;
81 81
         }
82 82
         $this->hours = $hours;
@@ -88,9 +88,9 @@  discard block
 block discarded – undo
88 88
     public function setMinutes(int $minutes): void
89 89
     {
90 90
         $minutes = (int) $minutes;
91
-        if($minutes < 0) {
91
+        if ($minutes < 0) {
92 92
             $minutes = 0;
93
-        } else if($minutes > 59) {
93
+        } else if ($minutes > 59) {
94 94
             $minutes = 59;
95 95
         }
96 96
         $this->minutes = $minutes;
@@ -102,9 +102,9 @@  discard block
 block discarded – undo
102 102
     public function setSeconds(int $seconds): void
103 103
     {
104 104
         $seconds = (int) $seconds;
105
-        if($seconds < 0) {
105
+        if ($seconds < 0) {
106 106
             $seconds = 0;
107
-        } else if($seconds > 59) {
107
+        } else if ($seconds > 59) {
108 108
             $seconds = 59;
109 109
         }
110 110
         $this->seconds = $seconds;
@@ -113,13 +113,13 @@  discard block
 block discarded – undo
113 113
     public static function validateTimeString($str): ?string
114 114
     {
115 115
         $str = trim($str);
116
-        if(strpos($str, ':') !== false) {
116
+        if (strpos($str, ':') !== false) {
117 117
             $timeParts = explode(':', $str);
118
-            if(count($timeParts) == 2) {
118
+            if (count($timeParts) == 2) {
119 119
                 $timeParts[2] = '00';
120 120
             }
121 121
             $str = join(':', $timeParts);
122
-            if(preg_match("/^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/", $str)) {
122
+            if (preg_match("/^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/", $str)) {
123 123
                 return $str;
124 124
             }
125 125
         }
@@ -129,33 +129,33 @@  discard block
 block discarded – undo
129 129
     public static function inInterval($minStrTime, $maxStrTime, $checkStrTime = ''): bool
130 130
     {
131 131
         $minTime = new self($minStrTime);
132
-        if($maxStrTime == '00:00') {
132
+        if ($maxStrTime == '00:00') {
133 133
             $maxStrTime = '23:59';
134 134
         }
135 135
         $maxTime = new self($maxStrTime);
136 136
         $checkTime = new self($checkStrTime);
137 137
 
138
-        if($minTime->getHours() == $checkTime->getHours()) {
139
-            if($minTime->getMinutes() == $checkTime->getMinutes()) {
140
-                if($minTime->getSeconds() > $checkTime->getSeconds()) {
138
+        if ($minTime->getHours() == $checkTime->getHours()) {
139
+            if ($minTime->getMinutes() == $checkTime->getMinutes()) {
140
+                if ($minTime->getSeconds() > $checkTime->getSeconds()) {
141 141
                     return false;
142 142
                 }
143
-            } else if($minTime->getMinutes() > $checkTime->getMinutes()) {
143
+            } else if ($minTime->getMinutes() > $checkTime->getMinutes()) {
144 144
                 return false;
145 145
             }
146
-        } else if($minTime->getHours() > $checkTime->getHours()) {
146
+        } else if ($minTime->getHours() > $checkTime->getHours()) {
147 147
             return false;
148 148
         }
149 149
 
150
-        if($maxTime->getHours() == $checkTime->getHours()) {
151
-            if($maxTime->getMinutes() == $checkTime->getMinutes()) {
152
-                if($minTime->getSeconds() < $checkTime->getSeconds()) {
150
+        if ($maxTime->getHours() == $checkTime->getHours()) {
151
+            if ($maxTime->getMinutes() == $checkTime->getMinutes()) {
152
+                if ($minTime->getSeconds() < $checkTime->getSeconds()) {
153 153
                     return false;
154 154
                 }
155
-            } else if($maxTime->getMinutes() < $checkTime->getMinutes()) {
155
+            } else if ($maxTime->getMinutes() < $checkTime->getMinutes()) {
156 156
                 return false;
157 157
             }
158
-        } else if($maxTime->getHours() < $checkTime->getHours()) {
158
+        } else if ($maxTime->getHours() < $checkTime->getHours()) {
159 159
             return false;
160 160
         }
161 161
 
Please login to merge, or discard this patch.