Passed
Push — master ( 470f50...6685b9 )
by Ioannes
01:53
created
src/Cron.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -27,30 +27,30 @@  discard block
 block discarded – undo
27 27
     protected function execute(InputInterface $input, OutputInterface $output)
28 28
     {
29 29
         $logger = EnvHelper::getLogger('bx_cron');
30
-        if($logger) {
30
+        if ($logger) {
31 31
             $this->setLogger($logger);
32 32
         }
33 33
 
34
-        if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
35
-            if($this->logger) {
34
+        if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) {
35
+            if ($this->logger) {
36 36
                 $this->logger->alert('BxCron switch off');
37 37
             }
38 38
             return 0;
39 39
         }
40 40
 
41
-        if(!$this->lock()) {
41
+        if (!$this->lock()) {
42 42
             $msg = 'The command is already running in another process.';
43 43
             $output->writeln($msg);
44
-            if($this->logger) {
44
+            if ($this->logger) {
45 45
                 $this->logger->warning($msg);
46 46
             }
47 47
             return 0;
48 48
         }
49 49
 
50
-        if($sleepInterval = EnvHelper::checkSleepInterval()) {
50
+        if ($sleepInterval = EnvHelper::checkSleepInterval()) {
51 51
             $msg = sprintf("Sleep in interval %s", $sleepInterval);
52 52
             $output->writeln($msg);
53
-            if($this->logger) {
53
+            if ($this->logger) {
54 54
                 $this->logger->warning($msg);
55 55
             }
56 56
             return 0;
@@ -64,11 +64,11 @@  discard block
 block discarded – undo
64 64
          */
65 65
         $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod();
66 66
 
67
-        if(!empty($jobs)) {
67
+        if (!empty($jobs)) {
68 68
 
69
-            foreach($jobs as $cmd => $job) {
69
+            foreach ($jobs as $cmd => $job) {
70 70
 
71
-                if($this->isActualJob($job)) {
71
+                if ($this->isActualJob($job)) {
72 72
 
73 73
                     $job['status'] = self::EXEC_STATUS_WORK;
74 74
                     $this->updaateJob($cmd, $job);
@@ -80,12 +80,12 @@  discard block
 block discarded – undo
80 80
                         $timeStart = microtime(true);
81 81
                         $returnCode = $command->run($cmdInput, $output);
82 82
 
83
-                        if(!$returnCode) {
83
+                        if (!$returnCode) {
84 84
 
85 85
                             $job['status'] = self::EXEC_STATUS_SUCCESS;
86 86
 
87 87
                             $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart);
88
-                            if($this->logger) {
88
+                            if ($this->logger) {
89 89
                                 $this->logger->alert($msg);
90 90
                             }
91 91
                             $output->writeln(PHP_EOL . $msg);
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
                             $job['error_code'] = $returnCode;
97 97
 
98 98
                             $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart);
99
-                            if($this->logger) {
99
+                            if ($this->logger) {
100 100
                                 $this->logger->alert($msg);
101 101
                             }
102 102
                             $output->writeln(PHP_EOL . $msg);
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
                         $job['error'] = $e->getMessage();
109 109
 
110 110
 
111
-                        if($this->logger) {
111
+                        if ($this->logger) {
112 112
                             $this->logger->error($e, ['command' => $cmd]);
113 113
                         }
114 114
                         $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage());
@@ -134,21 +134,21 @@  discard block
 block discarded – undo
134 134
 
135 135
     protected function isActualJob(&$job) {
136 136
 
137
-        if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
137
+        if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) {
138 138
             return false;
139 139
         }
140 140
 
141 141
         $period = intval($job['period']);
142 142
 
143
-        if($period > 0) {
144
-            if($period < $this->minAgentPeriod) {
143
+        if ($period > 0) {
144
+            if ($period < $this->minAgentPeriod) {
145 145
                 $job['orig_period'] = $period;
146 146
                 $period = $job['period'] = $this->minAgentPeriod;
147 147
             }
148
-            if(time() - $job['last_exec'] >= $period) {
148
+            if (time() - $job['last_exec'] >= $period) {
149 149
                 return true;
150 150
             }
151
-        } else if(!empty($job['times'])) {
151
+        } else if (!empty($job['times'])) {
152 152
             //TODO:
153 153
         }
154 154
 
@@ -163,9 +163,9 @@  discard block
 block discarded – undo
163 163
         $commands = $app->all();
164 164
 
165 165
         $selfCommands = [];
166
-        foreach($commands as $command) {
166
+        foreach ($commands as $command) {
167 167
             /** @var BxCommand $command */
168
-            if($command instanceof BxCommand) {
168
+            if ($command instanceof BxCommand) {
169 169
                 $name = $command->getName();
170 170
                 $selfCommands[$name] = [
171 171
                     'object' => $command,
@@ -175,12 +175,12 @@  discard block
 block discarded – undo
175 175
 
176 176
         $agents = [];
177 177
         $reader = new AnnotationReader();
178
-        foreach($selfCommands as $cmd => $selfCommand) {
178
+        foreach ($selfCommands as $cmd => $selfCommand) {
179 179
             $reflectionClass = new \ReflectionClass($selfCommand['object']);
180 180
             $annotations = $reader->getClassAnnotations($reflectionClass);
181 181
 
182
-            foreach($annotations as $annotation) {
183
-                if($annotation instanceof Agent) {
182
+            foreach ($annotations as $annotation) {
183
+                if ($annotation instanceof Agent) {
184 184
                     $agents[$cmd] = $annotation->toArray();
185 185
                 }
186 186
             }
@@ -188,9 +188,9 @@  discard block
 block discarded – undo
188 188
 
189 189
         $crontab = $this->getCronTab();
190 190
 
191
-        if(is_array($crontab)) {
192
-            foreach($crontab as $cmd => $job) {
193
-                if(is_array($job) && isset($agents[$cmd])) {
191
+        if (is_array($crontab)) {
192
+            foreach ($crontab as $cmd => $job) {
193
+                if (is_array($job) && isset($agents[$cmd])) {
194 194
                     $agents[$cmd] = array_merge($job, $agents[$cmd]);
195 195
                 }
196 196
             }
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
         $fh = fopen($filename, 'c');
224 224
         if (flock($fh, LOCK_EX)) {
225 225
             ftruncate($fh, 0);
226
-            if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
226
+            if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) {
227 227
                 throw new \Exception('Unable to write BX_CRONTAB : ' . $filename);
228 228
             }
229 229
         }
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
         $filename = EnvHelper::getCrontabFile();
242 242
 
243 243
         $fh = fopen($filename, 'r');
244
-        if(flock($fh, LOCK_SH)) {
244
+        if (flock($fh, LOCK_SH)) {
245 245
             $data = @fread($fh, filesize($filename));
246 246
             $cronTab = json_decode($data, true);
247 247
         }
Please login to merge, or discard this patch.
src/EnvHelper.php 1 patch
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -18,10 +18,10 @@  discard block
 block discarded – undo
18 18
     public static function loadEnv() {
19 19
 
20 20
         $envFile = realpath(__DIR__ . '/../../../../.env');
21
-        if(!is_file($envFile)) {
21
+        if (!is_file($envFile)) {
22 22
             $envFile = realpath(__DIR__ . '/../../../../../.env');
23 23
         }
24
-        if(is_file($envFile)) {
24
+        if (is_file($envFile)) {
25 25
             try {
26 26
                 $env = new \Symfony\Component\Dotenv\Dotenv();
27 27
                 $env->load($envFile);
@@ -38,15 +38,15 @@  discard block
 block discarded – undo
38 38
 
39 39
         $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../');
40 40
 
41
-        if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) {
41
+        if (isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) {
42 42
             $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT'];
43 43
             return $_SERVER['DOCUMENT_ROOT'];
44 44
         }
45 45
 
46 46
         $composerFile = realpath(__DIR__ . '/../../../../composer.json');
47
-        if(is_file($composerFile)) {
47
+        if (is_file($composerFile)) {
48 48
             $composerConfig = json_decode(file_get_contents($composerFile), true);
49
-            if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) {
49
+            if (isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) {
50 50
                 $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root'];
51 51
                 return $_SERVER['DOCUMENT_ROOT'];
52 52
             }
@@ -61,10 +61,10 @@  discard block
 block discarded – undo
61 61
      */
62 62
     public static function getLogger($channel) {
63 63
 
64
-        if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) {
64
+        if (isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) {
65 65
             $logClass = $_ENV['APP_LOG_CLASS'];
66 66
             $log = new $logClass($channel);
67
-            if($log instanceof LoggerInterface) {
67
+            if ($log instanceof LoggerInterface) {
68 68
                 return $log;
69 69
             }
70 70
         }
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
      */
78 78
     public static function getCrontabFile() {
79 79
 
80
-        if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) {
80
+        if (isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) {
81 81
             return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json';
82 82
         }
83 83
 
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
 
87 87
     public static function getCrontabTimeout() {
88 88
 
89
-        if(isset($_ENV['BX_CRONTAB_FOLDER']) && is_numeric($_ENV['BX_CRONTAB_FOLDER'])) {
89
+        if (isset($_ENV['BX_CRONTAB_FOLDER']) && is_numeric($_ENV['BX_CRONTAB_FOLDER'])) {
90 90
             return (int) $_ENV['BX_CRONTAB_FOLDER'];
91 91
         }
92 92
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 
96 96
     public static function getBxCrontabPeriod() {
97 97
 
98
-        if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) {
98
+        if (isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) {
99 99
             return (int) $_ENV['BX_CRONTAB_PERIOD'];
100 100
         }
101 101
 
@@ -104,15 +104,15 @@  discard block
 block discarded – undo
104 104
 
105 105
     public static function getSwitch($name, $state) {
106 106
 
107
-        if(isset($_ENV[$name])) {
107
+        if (isset($_ENV[$name])) {
108 108
 
109 109
             $val = strtolower(trim($_ENV[$name]));
110
-            if($state == self::SWITCH_STATE_ON) {
111
-                if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
110
+            if ($state == self::SWITCH_STATE_ON) {
111
+                if ($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
112 112
                     return true;
113 113
                 }
114
-            } else if($state == self::SWITCH_STATE_OFF) {
115
-                if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
114
+            } else if ($state == self::SWITCH_STATE_OFF) {
115
+                if ($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
116 116
                     return true;
117 117
                 }
118 118
             }
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
 
126 126
         $timeZone = self::BX_CRONTAB_TIMEZONE;
127 127
 
128
-        if(isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) {
128
+        if (isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) {
129 129
             $timeZone = trim($_ENV['BX_CRONTAB_TIMEZONE']);
130 130
         }
131 131
 
@@ -134,17 +134,17 @@  discard block
 block discarded – undo
134 134
 
135 135
     public static function checkSleepInterval() {
136 136
 
137
-        if(isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) {
137
+        if (isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) {
138 138
             $intervals = explode(',', $_ENV['BX_CRONTAB_SLEEP_TIME']);
139
-            foreach($intervals as $interval) {
139
+            foreach ($intervals as $interval) {
140 140
                 $times = explode('-', $interval);
141
-                if(count($times) != 2) {
141
+                if (count($times) != 2) {
142 142
                     continue;
143 143
                 }
144 144
                 $minTime = Time24::validateTimeString($times[0]);
145 145
                 $maxTime = Time24::validateTimeString($times[1]);
146
-                if($minTime && $maxTime) {
147
-                    if(Time24::inInterval($minTime, $maxTime)) {
146
+                if ($minTime && $maxTime) {
147
+                    if (Time24::inInterval($minTime, $maxTime)) {
148 148
                         return $interval;
149 149
                     }
150 150
                 }
Please login to merge, or discard this patch.
src/Time24.php 2 patches
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($hours)
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($minutes)
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($seconds)
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) {
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 = '') {
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.
Braces   +9 added lines, -3 removed lines patch added patch discarded remove patch
@@ -16,9 +16,15 @@
 block discarded – undo
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])) {
20
+            $this->setHours($timeparts[0]);
21
+        }
22
+        if(isset($timeparts[1])) {
23
+            $this->setMinutes($timeparts[1]);
24
+        }
25
+        if(isset($timeparts[2])) {
26
+            $this->setSeconds($timeparts[2]);
27
+        }
22 28
     }
23 29
 
24 30
     /**
Please login to merge, or discard this patch.