Passed
Push — master ( 2ef181...6dab9e )
by Ioannes
01:42
created
src/Cron.php 1 patch
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
     protected function execute(InputInterface $input, OutputInterface $output)
37 37
     {
38 38
         $logger = EnvHelper::getLogger('bx_cron');
39
-        if($logger) {
39
+        if ($logger) {
40 40
             $this->setLogger($logger);
41 41
         }
42 42
 
@@ -50,20 +50,20 @@  discard block
 block discarded – undo
50 50
 
51 51
         $workedJobs = [];
52 52
 
53
-        if(!empty($jobs)) {
53
+        if (!empty($jobs)) {
54 54
 
55 55
             $lockStore = new FlockStore(pathinfo(EnvHelper::getCrontabFile(), PATHINFO_DIRNAME));
56 56
             $lockFactory = new LockFactory($lockStore);
57
-            if($this->logger) {
57
+            if ($this->logger) {
58 58
                 $lockFactory->setLogger($this->logger);
59 59
             }
60 60
 
61
-            foreach($jobs as $cmd => $job) {
61
+            foreach ($jobs as $cmd => $job) {
62 62
 
63
-                if($this->isActualJob($job)) {
63
+                if ($this->isActualJob($job)) {
64 64
 
65 65
                     $lock = $lockFactory->createLock($this->getLockName($cmd), self::EXEC_TIMEOUT);
66
-                    if($lock->acquire()) {
66
+                    if ($lock->acquire()) {
67 67
 
68 68
                         $workedJobs[$cmd] = $job;
69 69
 
@@ -74,11 +74,11 @@  discard block
 block discarded – undo
74 74
                             $timeStart = microtime(true);
75 75
                             $returnCode = $command->run($cmdInput, $output);
76 76
 
77
-                            if(!$returnCode) {
77
+                            if (!$returnCode) {
78 78
 
79 79
                                 $workedJobs[$cmd]['status'] = self::EXEC_STATUS_SUCCESS;
80 80
                                 $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart);
81
-                                if($this->logger) {
81
+                                if ($this->logger) {
82 82
                                     $this->logger->alert($msg);
83 83
                                 }
84 84
                                 $output->writeln(PHP_EOL . $msg);
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
                                 $workedJobs[$cmd]['status'] = self::EXEC_STATUS_ERROR;
89 89
                                 $workedJobs[$cmd]['error_code'] = $returnCode;
90 90
                                 $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart);
91
-                                if($this->logger) {
91
+                                if ($this->logger) {
92 92
                                     $this->logger->alert($msg);
93 93
                                 }
94 94
                                 $output->writeln(PHP_EOL . $msg);
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
 
99 99
                             $workedJobs[$cmd]['status'] = self::EXEC_STATUS_ERROR;
100 100
                             $workedJobs[$cmd]['error'] = $e->getMessage();
101
-                            if($this->logger) {
101
+                            if ($this->logger) {
102 102
                                 $this->logger->error($e, ['command' => $cmd]);
103 103
                             }
104 104
                             $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
@@ -117,7 +117,7 @@  discard block
 block discarded – undo
117 117
                         break;
118 118
 
119 119
                     } else {
120
-                        if($this->logger) {
120
+                        if ($this->logger) {
121 121
                             $this->logger->warning($cmd . " is locked");
122 122
                         }
123 123
                     }
@@ -137,15 +137,15 @@  discard block
 block discarded – undo
137 137
 
138 138
         $period = intval($job['period']);
139 139
 
140
-        if($period > 0) {
141
-            if($period < $this->minAgentPeriod) {
140
+        if ($period > 0) {
141
+            if ($period < $this->minAgentPeriod) {
142 142
                 $job['orig_period'] = $period;
143 143
                 $period = $job['period'] = $this->minAgentPeriod;
144 144
             }
145
-            if(time() - $job['last_exec'] >= $period) {
145
+            if (time() - $job['last_exec'] >= $period) {
146 146
                 return true;
147 147
             }
148
-        } else if(!empty($job['times'])) {
148
+        } else if (!empty($job['times'])) {
149 149
             //TODO:
150 150
         }
151 151
 
@@ -160,9 +160,9 @@  discard block
 block discarded – undo
160 160
         $commands = $app->all();
161 161
 
162 162
         $selfCommands = [];
163
-        foreach($commands as $command) {
163
+        foreach ($commands as $command) {
164 164
             /** @var BxCommand $command */
165
-            if($command instanceof BxCommand) {
165
+            if ($command instanceof BxCommand) {
166 166
                 $name = $command->getName();
167 167
                 $selfCommands[$name] = [
168 168
                     'object' => $command,
@@ -172,12 +172,12 @@  discard block
 block discarded – undo
172 172
 
173 173
         $agents = [];
174 174
         $reader = new AnnotationReader();
175
-        foreach($selfCommands as $cmd => $selfCommand) {
175
+        foreach ($selfCommands as $cmd => $selfCommand) {
176 176
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
177 177
             $annotations = $reader->getClassAnnotations($reflectionClass);
178 178
 
179
-            foreach($annotations as $annotation) {
180
-                if($annotation instanceof Agent) {
179
+            foreach ($annotations as $annotation) {
180
+                if ($annotation instanceof Agent) {
181 181
                     $agents[$cmd] = $annotation->toArray();
182 182
                 }
183 183
             }
@@ -185,9 +185,9 @@  discard block
 block discarded – undo
185 185
 
186 186
         $crontab = $this->getCronTab();
187 187
 
188
-        if(is_array($crontab)) {
189
-            foreach($crontab as $cmd => $job) {
190
-                if(is_array($job) && isset($agents[$cmd])) {
188
+        if (is_array($crontab)) {
189
+            foreach ($crontab as $cmd => $job) {
190
+                if (is_array($job) && isset($agents[$cmd])) {
191 191
                     $agents[$cmd] = array_merge($job, $agents[$cmd]);
192 192
                 }
193 193
             }
@@ -215,7 +215,7 @@  discard block
 block discarded – undo
215 215
         $fh = fopen($filename, 'c');
216 216
         if (flock($fh, LOCK_EX)) {
217 217
             ftruncate($fh, 0);
218
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
218
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
219 219
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
220 220
             }
221 221
         }
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
         $filename = EnvHelper::getCrontabFile();
234 234
 
235 235
         $fh = fopen($filename, 'r');
236
-        if(flock($fh, LOCK_SH)) {
236
+        if (flock($fh, LOCK_SH)) {
237 237
             $data = @fread($fh, filesize($filename));
238 238
             $cronTab = json_decode($data, true);
239 239
         }
Please login to merge, or discard this patch.