Passed
Push — master ( e49e3e...821da0 )
by Ioannes
01:42
created
src/Cron.php 2 patches
Spacing   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -37,45 +37,45 @@  discard block
 block discarded – undo
37 37
     protected function execute(InputInterface $input, OutputInterface $output)
38 38
     {
39 39
         $logger = EnvHelper::getLogger('bx_cron');
40
-        if($logger) {
40
+        if ($logger) {
41 41
             $this->setLogger($logger);
42 42
         }
43 43
 
44 44
         $showStatus = $input->getOption('status');
45 45
         $byTime = $input->getOption('bytime');
46
-        if($showStatus) {
46
+        if ($showStatus) {
47 47
             $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME);
48 48
             $this->showStatus($output, $sort);
49 49
             return 0;
50 50
         }
51 51
 
52
-        if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
53
-            if($this->logger) {
52
+        if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
53
+            if ($this->logger) {
54 54
                 $this->logger->alert('BxCron switch off');
55 55
             }
56 56
             return 0;
57 57
         }
58 58
 
59
-        if(!$this->lock()) {
59
+        if (!$this->lock()) {
60 60
             $msg = 'The command is already running in another process.';
61 61
             $output->writeln($msg);
62
-            if($this->logger) {
62
+            if ($this->logger) {
63 63
                 $this->logger->warning($msg);
64 64
             }
65 65
             return 0;
66 66
         }
67 67
 
68
-        if($sleepInterval = EnvHelper::checkSleepInterval()) {
68
+        if ($sleepInterval = EnvHelper::checkSleepInterval()) {
69 69
             $msg = sprintf("Sleep in interval %s", $sleepInterval);
70 70
             $output->writeln($msg);
71
-            if($this->logger) {
71
+            if ($this->logger) {
72 72
                 $this->logger->warning($msg);
73 73
             }
74 74
             return 0;
75 75
         }
76 76
 
77 77
         $clean = $input->getOption('clean');
78
-        if($clean) {
78
+        if ($clean) {
79 79
             $command = $this->getApplication()->find($clean);
80 80
             $this->cleanJob($command->getName());
81 81
             $output->writeln($command->getName() . " will be executed now");
@@ -99,10 +99,10 @@  discard block
 block discarded – undo
99 99
         $lastExec = 0;
100 100
         $hasError = false;
101 101
 
102
-        foreach($jobs as $cmd => $job) {
102
+        foreach ($jobs as $cmd => $job) {
103 103
             $execTime = $job['last_exec'];
104
-            if($execTime > $lastExec) $lastExec = $execTime;
105
-            if(!empty($job['error'])) {
104
+            if ($execTime > $lastExec) $lastExec = $execTime;
105
+            if (!empty($job['error'])) {
106 106
                 $hasError = true;
107 107
             }
108 108
         }
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
             'Status',
122 122
         ];
123 123
 
124
-        if($hasError) {
124
+        if ($hasError) {
125 125
             $header[] = 'Error';
126 126
         }
127 127
 
@@ -131,15 +131,15 @@  discard block
 block discarded – undo
131 131
         ]);
132 132
 
133 133
         $cnt = 1;
134
-        foreach($jobs as $cmd => $job) {
135
-            if($cnt > 1) $table->addRow(new TableSeparator());
134
+        foreach ($jobs as $cmd => $job) {
135
+            if ($cnt > 1) $table->addRow(new TableSeparator());
136 136
             $row = [
137 137
                 $cmd,
138 138
                 $job['period'],
139 139
                 ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'),
140 140
                 $job['status'],
141 141
             ];
142
-            if($hasError) {
142
+            if ($hasError) {
143 143
                 $row[] = $job['error'];
144 144
             }
145 145
             $table->addRow($row);
@@ -168,11 +168,11 @@  discard block
 block discarded – undo
168 168
          */
169 169
         $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod();
170 170
 
171
-        if(!empty($jobs)) {
171
+        if (!empty($jobs)) {
172 172
 
173
-            foreach($jobs as $cmd => $job) {
173
+            foreach ($jobs as $cmd => $job) {
174 174
 
175
-                if($this->isActualJob($job)) {
175
+                if ($this->isActualJob($job)) {
176 176
 
177 177
                     $job['status'] = self::EXEC_STATUS_WORK;
178 178
                     $this->updaateJob($cmd, $job);
@@ -184,12 +184,12 @@  discard block
 block discarded – undo
184 184
                         $timeStart = microtime(true);
185 185
                         $returnCode = $command->run($cmdInput, $output);
186 186
 
187
-                        if(!$returnCode) {
187
+                        if (!$returnCode) {
188 188
 
189 189
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
190 190
 
191 191
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart);
192
-                            if($this->logger) {
192
+                            if ($this->logger) {
193 193
                                 $this->logger->alert($msg);
194 194
                             }
195 195
                             $output->writeln(PHP_EOL . $msg);
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
                             $job['error_code'] = $returnCode;
201 201
 
202 202
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart);
203
-                            if($this->logger) {
203
+                            if ($this->logger) {
204 204
                                 $this->logger->alert($msg);
205 205
                             }
206 206
                             $output->writeln(PHP_EOL . $msg);
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
                         $job['error'] = $e->getMessage();
213 213
 
214 214
 
215
-                        if($this->logger) {
215
+                        if ($this->logger) {
216 216
                             $this->logger->error($e, ['command' => $cmd]);
217 217
                         }
218 218
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
@@ -234,21 +234,21 @@  discard block
 block discarded – undo
234 234
 
235 235
     protected function isActualJob(&$job) {
236 236
 
237
-        if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
237
+        if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
238 238
             return false;
239 239
         }
240 240
 
241 241
         $period = intval($job['period']);
242 242
 
243
-        if($period > 0) {
244
-            if($period < $this->minAgentPeriod) {
243
+        if ($period > 0) {
244
+            if ($period < $this->minAgentPeriod) {
245 245
                 $job['orig_period'] = $period;
246 246
                 $period = $job['period'] = $this->minAgentPeriod;
247 247
             }
248
-            if(time() - $job['last_exec'] >= $period) {
248
+            if (time() - $job['last_exec'] >= $period) {
249 249
                 return true;
250 250
             }
251
-        } else if(!empty($job['times'])) {
251
+        } else if (!empty($job['times'])) {
252 252
             //TODO:
253 253
         }
254 254
 
@@ -263,9 +263,9 @@  discard block
 block discarded – undo
263 263
         $commands = $app->all();
264 264
 
265 265
         $selfCommands = [];
266
-        foreach($commands as $command) {
266
+        foreach ($commands as $command) {
267 267
             /** @var BxCommand $command */
268
-            if($command instanceof BxCommand) {
268
+            if ($command instanceof BxCommand) {
269 269
                 $name = $command->getName();
270 270
                 $selfCommands[$name] = [
271 271
                     'object' => $command,
@@ -275,12 +275,12 @@  discard block
 block discarded – undo
275 275
 
276 276
         $agents = [];
277 277
         $reader = new AnnotationReader();
278
-        foreach($selfCommands as $cmd => $selfCommand) {
278
+        foreach ($selfCommands as $cmd => $selfCommand) {
279 279
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
280 280
             $annotations = $reader->getClassAnnotations($reflectionClass);
281 281
 
282
-            foreach($annotations as $annotation) {
283
-                if($annotation instanceof Agent) {
282
+            foreach ($annotations as $annotation) {
283
+                if ($annotation instanceof Agent) {
284 284
                     $agents[$cmd] = $annotation->toArray();
285 285
                 }
286 286
             }
@@ -288,8 +288,8 @@  discard block
 block discarded – undo
288 288
 
289 289
         $crontab = $this->getCronTab();
290 290
 
291
-        foreach($crontab as $cmd => $job) {
292
-            if(is_array($job) && isset($agents[$cmd])) {
291
+        foreach ($crontab as $cmd => $job) {
292
+            if (is_array($job) && isset($agents[$cmd])) {
293 293
                 $agents[$cmd] = array_merge($job, $agents[$cmd]);
294 294
             }
295 295
         }
@@ -323,7 +323,7 @@  discard block
 block discarded – undo
323 323
         $fh = fopen($filename, 'c');
324 324
         if (flock($fh, LOCK_EX)) {
325 325
             ftruncate($fh, 0);
326
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
326
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
327 327
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
328 328
             }
329 329
         }
@@ -341,10 +341,10 @@  discard block
 block discarded – undo
341 341
         $filename = EnvHelper::getCrontabFile();
342 342
 
343 343
         $fh = fopen($filename, 'r');
344
-        if(flock($fh, LOCK_SH)) {
345
-            if($data = @fread($fh, filesize($filename))) {
344
+        if (flock($fh, LOCK_SH)) {
345
+            if ($data = @fread($fh, filesize($filename))) {
346 346
                 $decoded = json_decode($data, true);
347
-                if(is_array($decoded)) {
347
+                if (is_array($decoded)) {
348 348
                     $cronTab = $decoded;
349 349
                 }
350 350
             }
@@ -357,14 +357,14 @@  discard block
 block discarded – undo
357 357
 
358 358
     protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) {
359 359
 
360
-        if($sort == self::SORT_TIME) {
360
+        if ($sort == self::SORT_TIME) {
361 361
             $sorting = [];
362
-            foreach($crontab as $cmd => $data) {
362
+            foreach ($crontab as $cmd => $data) {
363 363
                 $sorting[$cmd] = $data['last_exec'];
364 364
             }
365 365
             arsort($sorting, SORT_NUMERIC);
366 366
             $sorted = [];
367
-            foreach($sorting as $cmd => $time) {
367
+            foreach ($sorting as $cmd => $time) {
368 368
                 $sorted[$cmd] = $crontab[$cmd];
369 369
             }
370 370
             $crontab = $sorted;
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -101,7 +101,9 @@  discard block
 block discarded – undo
101 101
 
102 102
         foreach($jobs as $cmd => $job) {
103 103
             $execTime = $job['last_exec'];
104
-            if($execTime > $lastExec) $lastExec = $execTime;
104
+            if($execTime > $lastExec) {
105
+                $lastExec = $execTime;
106
+            }
105 107
             if(!empty($job['error'])) {
106 108
                 $hasError = true;
107 109
             }
@@ -132,7 +134,9 @@  discard block
 block discarded – undo
132 134
 
133 135
         $cnt = 1;
134 136
         foreach($jobs as $cmd => $job) {
135
-            if($cnt > 1) $table->addRow(new TableSeparator());
137
+            if($cnt > 1) {
138
+                $table->addRow(new TableSeparator());
139
+            }
136 140
             $row = [
137 141
                 $cmd,
138 142
                 $job['period'],
Please login to merge, or discard this patch.