Passed
Push — master ( dc2440...49787f )
by Ioannes
07:37
created
src/Cron.php 1 patch
Spacing   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -40,45 +40,45 @@  discard block
 block discarded – undo
40 40
         set_time_limit(EnvHelper::getCrontabTimeout());
41 41
 
42 42
         $logger = EnvHelper::getLogger('bx_cron');
43
-        if($logger) {
43
+        if ($logger) {
44 44
             $this->setLogger($logger);
45 45
         }
46 46
 
47 47
         $showStatus = $input->getOption('status');
48 48
         $byTime = $input->getOption('bytime');
49
-        if($showStatus) {
49
+        if ($showStatus) {
50 50
             $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME);
51 51
             $this->showStatus($output, $sort);
52 52
             return 0;
53 53
         }
54 54
 
55
-        if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
56
-            if($this->logger) {
55
+        if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
56
+            if ($this->logger) {
57 57
                 $this->logger->alert('BxCron switch off');
58 58
             }
59 59
             return 0;
60 60
         }
61 61
 
62
-        if(!$this->lock()) {
62
+        if (!$this->lock()) {
63 63
             $msg = 'The command is already running in another process.';
64 64
             $output->writeln($msg);
65
-            if($this->logger) {
65
+            if ($this->logger) {
66 66
                 $this->logger->warning($msg);
67 67
             }
68 68
             return 0;
69 69
         }
70 70
 
71
-        if($sleepInterval = EnvHelper::checkSleepInterval()) {
71
+        if ($sleepInterval = EnvHelper::checkSleepInterval()) {
72 72
             $msg = sprintf("Sleep in interval %s", $sleepInterval);
73 73
             $output->writeln($msg);
74
-            if($this->logger) {
74
+            if ($this->logger) {
75 75
                 $this->logger->warning($msg);
76 76
             }
77 77
             return 0;
78 78
         }
79 79
 
80 80
         $clean = $input->getOption('clean');
81
-        if($clean) {
81
+        if ($clean) {
82 82
             $command = $this->getApplication()->find($clean);
83 83
             $this->cleanJob($command->getName());
84 84
             $output->writeln($command->getName() . " will be executed now");
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
         }
87 87
 
88 88
         $cleanAll = $input->getOption('all');
89
-        if($cleanAll) {
89
+        if ($cleanAll) {
90 90
             $this->cleanJob();
91 91
             $output->writeln("All commands will be executed now");
92 92
             return 0;
@@ -109,10 +109,10 @@  discard block
 block discarded – undo
109 109
         $lastExec = 0;
110 110
         $hasError = false;
111 111
 
112
-        foreach($jobs as $cmd => $job) {
112
+        foreach ($jobs as $cmd => $job) {
113 113
             $execTime = $job['last_exec'];
114
-            if($execTime > $lastExec) $lastExec = $execTime;
115
-            if(!empty($job['error'])) {
114
+            if ($execTime > $lastExec) $lastExec = $execTime;
115
+            if (!empty($job['error'])) {
116 116
                 $hasError = true;
117 117
             }
118 118
         }
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
             'Status',
132 132
         ];
133 133
 
134
-        if($hasError) {
134
+        if ($hasError) {
135 135
             $header[] = 'Error';
136 136
         }
137 137
 
@@ -141,15 +141,15 @@  discard block
 block discarded – undo
141 141
         ]);
142 142
 
143 143
         $cnt = 1;
144
-        foreach($jobs as $cmd => $job) {
145
-            if($cnt > 1) $table->addRow(new TableSeparator());
144
+        foreach ($jobs as $cmd => $job) {
145
+            if ($cnt > 1) $table->addRow(new TableSeparator());
146 146
             $row = [
147 147
                 $cmd,
148 148
                 $job['period'],
149 149
                 ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'),
150 150
                 $job['status'],
151 151
             ];
152
-            if($hasError) {
152
+            if ($hasError) {
153 153
                 $row[] = $job['error'];
154 154
             }
155 155
             $table->addRow($row);
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 
164 164
         $crontab = [];
165 165
 
166
-        if($command) {
166
+        if ($command) {
167 167
             $crontab = $this->getCronTab();
168 168
             unset($crontab[$command]);
169 169
         }
@@ -181,11 +181,11 @@  discard block
 block discarded – undo
181 181
          */
182 182
         $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod();
183 183
 
184
-        if(!empty($jobs)) {
184
+        if (!empty($jobs)) {
185 185
 
186
-            foreach($jobs as $cmd => $job) {
186
+            foreach ($jobs as $cmd => $job) {
187 187
 
188
-                if($this->isActualJob($job)) {
188
+                if ($this->isActualJob($job)) {
189 189
 
190 190
                     $job['status'] = self::EXEC_STATUS_WORK;
191 191
                     $this->updaateJob($cmd, $job);
@@ -197,12 +197,12 @@  discard block
 block discarded – undo
197 197
                         $timeStart = microtime(true);
198 198
                         $returnCode = $command->run($cmdInput, $output);
199 199
 
200
-                        if(!$returnCode) {
200
+                        if (!$returnCode) {
201 201
 
202 202
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
203 203
 
204 204
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart);
205
-                            if($this->logger) {
205
+                            if ($this->logger) {
206 206
                                 $this->logger->alert($msg);
207 207
                             }
208 208
                             $output->writeln(PHP_EOL . $msg);
@@ -213,7 +213,7 @@  discard block
 block discarded – undo
213 213
                             $job['error_code'] = $returnCode;
214 214
 
215 215
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart);
216
-                            if($this->logger) {
216
+                            if ($this->logger) {
217 217
                                 $this->logger->alert($msg);
218 218
                             }
219 219
                             $output->writeln(PHP_EOL . $msg);
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
                         $job['error'] = $e->getMessage();
226 226
 
227 227
 
228
-                        if($this->logger) {
228
+                        if ($this->logger) {
229 229
                             $this->logger->error($e, ['command' => $cmd]);
230 230
                         }
231 231
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
@@ -247,21 +247,21 @@  discard block
 block discarded – undo
247 247
 
248 248
     protected function isActualJob(&$job) {
249 249
 
250
-        if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
250
+        if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
251 251
             return false;
252 252
         }
253 253
 
254 254
         $period = intval($job['period']);
255 255
 
256
-        if($period > 0) {
257
-            if($period < $this->minAgentPeriod) {
256
+        if ($period > 0) {
257
+            if ($period < $this->minAgentPeriod) {
258 258
                 $job['orig_period'] = $period;
259 259
                 $period = $job['period'] = $this->minAgentPeriod;
260 260
             }
261
-            if(time() - $job['last_exec'] >= $period) {
261
+            if (time() - $job['last_exec'] >= $period) {
262 262
                 return true;
263 263
             }
264
-        } else if(!empty($job['times'])) {
264
+        } else if (!empty($job['times'])) {
265 265
             //TODO:
266 266
         }
267 267
 
@@ -276,9 +276,9 @@  discard block
 block discarded – undo
276 276
         $commands = $app->all();
277 277
 
278 278
         $selfCommands = [];
279
-        foreach($commands as $command) {
279
+        foreach ($commands as $command) {
280 280
             /** @var BxCommand $command */
281
-            if($command instanceof BxCommand) {
281
+            if ($command instanceof BxCommand) {
282 282
                 $name = $command->getName();
283 283
                 $selfCommands[$name] = [
284 284
                     'object' => $command,
@@ -288,12 +288,12 @@  discard block
 block discarded – undo
288 288
 
289 289
         $agents = [];
290 290
         $reader = new AnnotationReader();
291
-        foreach($selfCommands as $cmd => $selfCommand) {
291
+        foreach ($selfCommands as $cmd => $selfCommand) {
292 292
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
293 293
             $annotations = $reader->getClassAnnotations($reflectionClass);
294 294
 
295
-            foreach($annotations as $annotation) {
296
-                if($annotation instanceof Agent) {
295
+            foreach ($annotations as $annotation) {
296
+                if ($annotation instanceof Agent) {
297 297
                     $agents[$cmd] = $annotation->toArray();
298 298
                 }
299 299
             }
@@ -301,8 +301,8 @@  discard block
 block discarded – undo
301 301
 
302 302
         $crontab = $this->getCronTab();
303 303
 
304
-        foreach($crontab as $cmd => $job) {
305
-            if(is_array($job) && isset($agents[$cmd])) {
304
+        foreach ($crontab as $cmd => $job) {
305
+            if (is_array($job) && isset($agents[$cmd])) {
306 306
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
307 307
             }
308 308
         }
@@ -336,7 +336,7 @@  discard block
 block discarded – undo
336 336
         $fh = fopen($filename, 'c');
337 337
         if (flock($fh, LOCK_EX)) {
338 338
             ftruncate($fh, 0);
339
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
339
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
340 340
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
341 341
             }
342 342
         }
@@ -354,10 +354,10 @@  discard block
 block discarded – undo
354 354
         $filename = EnvHelper::getCrontabFile();
355 355
 
356 356
         $fh = fopen($filename, 'r');
357
-        if(flock($fh, LOCK_SH)) {
358
-            if($data = @fread($fh, filesize($filename))) {
357
+        if (flock($fh, LOCK_SH)) {
358
+            if ($data = @fread($fh, filesize($filename))) {
359 359
                 $decoded = json_decode($data, true);
360
-                if(is_array($decoded)) {
360
+                if (is_array($decoded)) {
361 361
                     $cronTab = $decoded;
362 362
                 }
363 363
             }
@@ -370,14 +370,14 @@  discard block
 block discarded – undo
370 370
 
371 371
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
372 372
 
373
-        if($sort == self::SORT_TIME) {
373
+        if ($sort == self::SORT_TIME) {
374 374
             $sorting = [];
375
-            foreach($crontab as $cmd => $data) {
375
+            foreach ($crontab as $cmd => $data) {
376 376
                 $sorting[$cmd] = $data['last_exec'];
377 377
             }
378 378
             arsort($sorting, SORT_NUMERIC);
379 379
             $sorted = [];
380
-            foreach($sorting as $cmd => $time) {
380
+            foreach ($sorting as $cmd => $time) {
381 381
                 $sorted[$cmd] = $crontab[$cmd];
382 382
             }
383 383
             $crontab = $sorted;
Please login to merge, or discard this patch.