@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | try { |
81 | 81 | $this->getData->execute(['key' => $key]); |
82 | 82 | $result = $this->getData->fetchObject(); |
83 | - if(!is_object($result)) { |
|
83 | + if (!is_object($result)) { |
|
84 | 84 | throw new RuntimeException('Row not found'); |
85 | 85 | } |
86 | 86 | return $result; |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | public function lockAndIterateServices(DateTimeInterface $now, callable $fn): int { |
98 | 98 | $count = 0; |
99 | 99 | $services = $this->fetchServices($now); |
100 | - foreach($services as $service) { |
|
100 | + foreach ($services as $service) { |
|
101 | 101 | $fn($service); |
102 | 102 | } |
103 | 103 | return $count; |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | $this->selectServices->execute(['dt' => $now->format(self::SQLITE_DATETIME_FORMAT)]); |
112 | 112 | try { |
113 | 113 | $services = $this->selectServices->fetchAll(PDO::FETCH_ASSOC); |
114 | - foreach($services as $service) { |
|
114 | + foreach ($services as $service) { |
|
115 | 115 | yield new Service($service['service_key']); |
116 | 116 | } |
117 | 117 | } finally { |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | private function migrate(int $version, string $statement): void { |
127 | 127 | $currentVersion = (int) $this->pdo->query('PRAGMA user_version')->fetchColumn(0); |
128 | - if($currentVersion < $version) { |
|
128 | + if ($currentVersion < $version) { |
|
129 | 129 | $this->pdo->exec($statement); |
130 | 130 | $this->pdo->exec("PRAGMA user_version={$version}"); |
131 | 131 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | */ |
17 | 17 | public static function create($init = null): DateTime { |
18 | 18 | try { |
19 | - if($init instanceof DateTimeInterface) { |
|
19 | + if ($init instanceof DateTimeInterface) { |
|
20 | 20 | return new DateTime($init->format('c')); |
21 | 21 | } |
22 | 22 | return new DateTime($init); |
@@ -31,10 +31,10 @@ discard block |
||
31 | 31 | */ |
32 | 32 | public static function createImmutable($init = null): DateTimeImmutable { |
33 | 33 | try { |
34 | - if($init instanceof DateTimeImmutable) { |
|
34 | + if ($init instanceof DateTimeImmutable) { |
|
35 | 35 | return $init; |
36 | 36 | } |
37 | - if($init instanceof DateTimeInterface) { |
|
37 | + if ($init instanceof DateTimeInterface) { |
|
38 | 38 | return new DateTimeImmutable($init->format('c')); |
39 | 39 | } |
40 | 40 | return new DateTimeImmutable($init); |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | $this->tableName = $tableName; |
54 | 54 | $this->options = $options; |
55 | 55 | $this->pdo = $pdo; |
56 | - if($options['use-locking'] ?? true) { |
|
56 | + if ($options['use-locking'] ?? true) { |
|
57 | 57 | $this->lock = $pdo->prepare('SELECT GET_LOCK(:name, 0)'); |
58 | 58 | $this->unlock = $pdo->prepare('SELECT RELEASE_LOCK(:name)'); |
59 | 59 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @return bool |
65 | 65 | */ |
66 | 66 | public function has(string $key) { |
67 | - if($this->hasService === null) { |
|
67 | + if ($this->hasService === null) { |
|
68 | 68 | $this->hasService = $this->pdo->prepare("SELECT COUNT(*) FROM `{$this->tableName}` WHERE service_key=:key;"); |
69 | 69 | } |
70 | 70 | $this->hasService->execute(['key' => $key]); |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | * @return MySQLAttributeRepository|void |
78 | 78 | */ |
79 | 79 | public function register(string $key) { |
80 | - $this->handleException(function () use ($key) { |
|
81 | - if($this->services === null) { |
|
82 | - if($this->getServiceKeys === null) { |
|
80 | + $this->handleException(function() use ($key) { |
|
81 | + if ($this->services === null) { |
|
82 | + if ($this->getServiceKeys === null) { |
|
83 | 83 | $this->getServiceKeys = $this->pdo->prepare("SELECT `service_key` FROM `{$this->tableName}`;"); |
84 | 84 | } |
85 | 85 | try { |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | $this->getServiceKeys->closeCursor(); |
90 | 90 | } |
91 | 91 | } |
92 | - if(!in_array($key, $this->services, true)) { |
|
93 | - if($this->registerRow === null) { |
|
92 | + if (!in_array($key, $this->services, true)) { |
|
93 | + if ($this->registerRow === null) { |
|
94 | 94 | $this->registerRow = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (`service_key`) VALUES (:key)"); |
95 | 95 | } |
96 | 96 | $this->registerRow->execute(['key' => $key]); |
@@ -103,15 +103,15 @@ discard block |
||
103 | 103 | * @return object |
104 | 104 | */ |
105 | 105 | public function getRowByKey(string $key) { |
106 | - if($this->getData === null) { |
|
106 | + if ($this->getData === null) { |
|
107 | 107 | $this->getData = $this->pdo->prepare("SELECT service_key, service_last_try, service_last_run, service_next_run FROM `{$this->tableName}` WHERE service_key=:key;"); |
108 | 108 | } |
109 | 109 | try { |
110 | - $this->handleException(function () use ($key) { |
|
110 | + $this->handleException(function() use ($key) { |
|
111 | 111 | $this->getData->execute(['key' => $key]); |
112 | 112 | }); |
113 | 113 | $result = $this->getData->fetchObject(); |
114 | - if(!is_object($result)) { |
|
114 | + if (!is_object($result)) { |
|
115 | 115 | throw new RuntimeException('Row not found'); |
116 | 116 | } |
117 | 117 | return $result; |
@@ -126,10 +126,10 @@ discard block |
||
126 | 126 | * @return void |
127 | 127 | */ |
128 | 128 | public function setLastTryDate(string $key, DateTimeInterface $datetime): void { |
129 | - if($this->updateTryDate === null) { |
|
129 | + if ($this->updateTryDate === null) { |
|
130 | 130 | $this->updateTryDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_last_try) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_last_try=:dt"); |
131 | 131 | } |
132 | - $this->handleException(function () use ($key, $datetime) { |
|
132 | + $this->handleException(function() use ($key, $datetime) { |
|
133 | 133 | $this->updateTryDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
134 | 134 | }); |
135 | 135 | } |
@@ -140,10 +140,10 @@ discard block |
||
140 | 140 | * @return void |
141 | 141 | */ |
142 | 142 | public function setLastRunDate(string $key, DateTimeInterface $datetime): void { |
143 | - if($this->updateRunDate === null) { |
|
143 | + if ($this->updateRunDate === null) { |
|
144 | 144 | $this->updateRunDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_last_run) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_last_run=:dt"); |
145 | 145 | } |
146 | - $this->handleException(function () use ($key, $datetime) { |
|
146 | + $this->handleException(function() use ($key, $datetime) { |
|
147 | 147 | $this->updateRunDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
148 | 148 | }); |
149 | 149 | } |
@@ -154,10 +154,10 @@ discard block |
||
154 | 154 | * @return void |
155 | 155 | */ |
156 | 156 | public function setNextRunDate(string $key, DateTimeInterface $datetime): void { |
157 | - if($this->updateNextDate === null) { |
|
157 | + if ($this->updateNextDate === null) { |
|
158 | 158 | $this->updateNextDate = $this->pdo->prepare("INSERT INTO `{$this->tableName}` (service_key, service_next_run) VALUES (:key, :dt) ON DUPLICATE KEY UPDATE service_next_run=:dt"); |
159 | 159 | } |
160 | - $this->handleException(function () use ($key, $datetime) { |
|
160 | + $this->handleException(function() use ($key, $datetime) { |
|
161 | 161 | $this->updateNextDate->execute(['key' => $key, 'dt' => $datetime->format('Y-m-d H:i:s')]); |
162 | 162 | }); |
163 | 163 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | public function lockAndIterateServices(?DateTimeInterface $now, callable $fn): int { |
171 | 171 | $data = (object) ['count' => 0]; |
172 | 172 | $services = $this->fetchServices($now); |
173 | - foreach($services as $service) { |
|
173 | + foreach ($services as $service) { |
|
174 | 174 | try { |
175 | 175 | $this->lock($service->getKey()); |
176 | 176 | $fn($service); |
@@ -187,15 +187,15 @@ discard block |
||
187 | 187 | * @return Service[] |
188 | 188 | */ |
189 | 189 | private function fetchServices(DateTimeInterface $now) { |
190 | - if($this->selectOverdueServices === null) { |
|
190 | + if ($this->selectOverdueServices === null) { |
|
191 | 191 | $this->selectOverdueServices = $this->pdo->prepare("SELECT service_key, service_last_try, service_last_run, service_next_run FROM `{$this->tableName}` WHERE IFNULL(service_next_run, DATE('2000-01-01')) <= :dt ORDER BY service_last_try;"); |
192 | 192 | } |
193 | - return $this->handleException(function () use ($now) { |
|
193 | + return $this->handleException(function() use ($now) { |
|
194 | 194 | $this->selectOverdueServices->execute(['dt' => $now->format('Y-m-d H:i:d')]); |
195 | 195 | try { |
196 | 196 | $services = $this->selectOverdueServices->fetchAll(PDO::FETCH_ASSOC); |
197 | 197 | $result = []; |
198 | - foreach($services as $service) { |
|
198 | + foreach ($services as $service) { |
|
199 | 199 | $result[] = new Service($service['service_key']); |
200 | 200 | } |
201 | 201 | return $result; |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | $lockName = sprintf('%s%s', $this->options['lock-prefix'] ?? '', $key); |
216 | 216 | $this->lock->execute(['name' => $lockName]); |
217 | 217 | $lockObtained = $this->lock->fetchColumn(0); |
218 | - if(!$lockObtained) { |
|
218 | + if (!$lockObtained) { |
|
219 | 219 | throw new RuntimeException(sprintf('Could not obtain lock "%s"', $lockName)); |
220 | 220 | } |
221 | 221 | } finally { |
@@ -238,13 +238,13 @@ discard block |
||
238 | 238 | try { |
239 | 239 | return $fn(); |
240 | 240 | } catch (PDOException $e) { |
241 | - if($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
241 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_MSSING) { |
|
242 | 242 | // Field is missing, let's have a look what is going on... |
243 | 243 | $this->pdo->exec("CREATE TABLE IF NOT EXISTS `{$this->tableName}` (`service_key` VARCHAR(255) NOT NULL DEFAULT '', `service_last_try` DATETIME NULL DEFAULT NULL, `service_last_run` DATETIME NULL DEFAULT NULL, `service_next_run` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`service_key`));"); |
244 | 244 | return $this->retry($fn); |
245 | 245 | } |
246 | 246 | |
247 | - if($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
247 | + if ($e->getCode() === self::MYSQL_ERR_TABLE_COLUMN_MSSING) { |
|
248 | 248 | // Field is missing, let's have a look what is going on... |
249 | 249 | $this->checkIfOldTableVersion(); |
250 | 250 | return $this->retry($fn); |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | * @return mixed |
260 | 260 | */ |
261 | 261 | private function retry(Closure $fn) { |
262 | - return $this->handleException(function () use ($fn) { |
|
262 | + return $this->handleException(function() use ($fn) { |
|
263 | 263 | return $fn(); |
264 | 264 | }); |
265 | 265 | } |
@@ -269,10 +269,10 @@ discard block |
||
269 | 269 | private function checkIfOldTableVersion() { |
270 | 270 | $serviceNextRunField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_next_run';")->fetchAll(PDO::FETCH_ASSOC); |
271 | 271 | |
272 | - if(!count($serviceNextRunField)) { |
|
272 | + if (!count($serviceNextRunField)) { |
|
273 | 273 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` ADD COLUMN `service_next_run` DATETIME NULL DEFAULT NULL AFTER `service_last_run`;"); |
274 | 274 | $serviceTimeoutField = $this->pdo->query("SHOW COLUMNS FROM `{$this->tableName}` LIKE 'service_timeout';")->fetchAll(PDO::FETCH_ASSOC); |
275 | - if(count($serviceTimeoutField)) { |
|
275 | + if (count($serviceTimeoutField)) { |
|
276 | 276 | $this->pdo->exec("UPDATE `{$this->tableName}` SET service_next_run = DATE_ADD(service_last_run, INTERVAL service_timeout SECOND)"); |
277 | 277 | $this->pdo->exec("ALTER TABLE `{$this->tableName}` DROP COLUMN `service_timeout`;"); |
278 | 278 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * @return $this |
58 | 58 | */ |
59 | 59 | public function on(string $event, callable $fn) { |
60 | - if(!array_key_exists($event, $this->listeners)) { |
|
60 | + if (!array_key_exists($event, $this->listeners)) { |
|
61 | 61 | $this->listeners[$event] = []; |
62 | 62 | } |
63 | 63 | $this->listeners[$event][] = $fn; |
@@ -70,8 +70,8 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public function run(?DateTimeInterface $now = null): int { |
72 | 72 | $dt = $now ?? date_create_immutable(); |
73 | - return $this->attributeRepository->lockAndIterateServices($dt, function (Service $service) { |
|
74 | - if(!array_key_exists($service->getKey(), $this->services)) { |
|
73 | + return $this->attributeRepository->lockAndIterateServices($dt, function(Service $service) { |
|
74 | + if (!array_key_exists($service->getKey(), $this->services)) { |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | $eventParams = ['serviceName' => $service->getKey()]; |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | $this->fireEvent('service-start', $eventParams); |
80 | 80 | $serviceData = $this->services[$service->getKey()]; |
81 | 81 | $this->attributeRepository->setLastTryDate($service->getKey(), date_create_immutable()); |
82 | - if($this->methodInvoker !== null) { |
|
82 | + if ($this->methodInvoker !== null) { |
|
83 | 83 | $result = $this->methodInvoker->invoke($serviceData->fn, $eventParams); |
84 | 84 | } else { |
85 | 85 | $result = call_user_func($serviceData->fn, $service); |
@@ -87,13 +87,13 @@ discard block |
||
87 | 87 | $this->attributeRepository->setLastRunDate($service->getKey(), date_create_immutable()); |
88 | 88 | $nextRunDate = IntervalParser::getNext($serviceData->interval); |
89 | 89 | $this->attributeRepository->setNextRunDate($serviceData->key, $nextRunDate); |
90 | - if($result !== false) { |
|
90 | + if ($result !== false) { |
|
91 | 91 | $this->fireEvent('service-success', $eventParams); |
92 | 92 | } |
93 | 93 | } catch (Throwable $e) { |
94 | 94 | $eventParams['exception'] = $e; |
95 | 95 | $this->fireEvent('service-failure', $eventParams); |
96 | - if($this->logger !== null) { |
|
96 | + if ($this->logger !== null) { |
|
97 | 97 | $this->logger->critical("{$service}: {$e->getMessage()}", ['exception' => $e]); |
98 | 98 | } else { |
99 | 99 | throw new RuntimeException("{$service}: {$e->getMessage()}", (int) $e->getCode(), $e); |
@@ -107,10 +107,10 @@ discard block |
||
107 | 107 | * @param array $params |
108 | 108 | */ |
109 | 109 | private function fireEvent(string $event, array $params) { |
110 | - if(array_key_exists($event, $this->listeners)) { |
|
110 | + if (array_key_exists($event, $this->listeners)) { |
|
111 | 111 | try { |
112 | - foreach($this->listeners[$event] as $listener) { |
|
113 | - if($this->methodInvoker !== null) { |
|
112 | + foreach ($this->listeners[$event] as $listener) { |
|
113 | + if ($this->methodInvoker !== null) { |
|
114 | 114 | $this->methodInvoker->invoke($listener, $params); |
115 | 115 | } else { |
116 | 116 | call_user_func($listener, $params['serviceName'] ?? null); |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | } |
119 | 119 | } catch (Exception $e) { |
120 | 120 | // Supress exceptions emitted by events |
121 | - if($this->logger !== null) { |
|
121 | + if ($this->logger !== null) { |
|
122 | 122 | $this->logger->critical($e->getMessage(), array('exception' => $e)); |
123 | 123 | } |
124 | 124 | } |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | * @return DateTimeImmutable |
15 | 15 | */ |
16 | 16 | public static function getNext($interval, ?DateTimeInterface $now = null): DateTimeImmutable { |
17 | - if($now === null) { |
|
17 | + if ($now === null) { |
|
18 | 18 | try { |
19 | 19 | $now = new DateTimeImmutable(); |
20 | 20 | } catch (Throwable $e) { |
@@ -24,10 +24,10 @@ discard block |
||
24 | 24 | $now = DateTimeHelper::createImmutable($now); |
25 | 25 | } |
26 | 26 | $result = null; |
27 | - foreach(self::parse($interval, $now) as $date) { |
|
28 | - if($result === null) { |
|
27 | + foreach (self::parse($interval, $now) as $date) { |
|
28 | + if ($result === null) { |
|
29 | 29 | $result = $date; |
30 | - } elseif($date < $result) { |
|
30 | + } elseif ($date < $result) { |
|
31 | 31 | $result = $date; |
32 | 32 | } |
33 | 33 | } |
@@ -40,11 +40,11 @@ discard block |
||
40 | 40 | * @return Generator|DateTimeImmutable[] |
41 | 41 | */ |
42 | 42 | private static function parse($interval, DateTimeImmutable $now) { |
43 | - if(is_array($interval)) { |
|
44 | - foreach($interval as $inner) { |
|
43 | + if (is_array($interval)) { |
|
44 | + foreach ($interval as $inner) { |
|
45 | 45 | yield from self::parse($inner, $now); |
46 | 46 | } |
47 | - } elseif(preg_match('/^\\d+$/', $interval)) { |
|
47 | + } elseif (preg_match('/^\\d+$/', $interval)) { |
|
48 | 48 | yield self::parseInt($interval, $now); |
49 | 49 | } else { |
50 | 50 | yield self::parseString($interval, $now); |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * @return DateTimeImmutable |
67 | 67 | */ |
68 | 68 | private static function parseString(string $interval, DateTimeImmutable $now): DateTimeImmutable { |
69 | - if(preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
69 | + if (preg_match('/^(\\d{1,2}|\\*):(\\d{1,2}|\\*)(?::(\\d{1,2}|\\*))?$/', $interval, $matches)) { |
|
70 | 70 | $matches[] = 0; |
71 | 71 | [$hours, $minutes, $seconds] = array_slice($matches, 1); |
72 | 72 | $today = DateTimeHelper::createImmutable($now)->setTime((int) $hours, (int) $minutes, (int) $seconds); |
@@ -87,15 +87,15 @@ discard block |
||
87 | 87 | */ |
88 | 88 | private static function nearst(array $possibleDates, DateTimeImmutable $now) { |
89 | 89 | $current = null; |
90 | - foreach($possibleDates as $possibleDate) { |
|
91 | - if($now > $possibleDate) { // The current date is in the past |
|
90 | + foreach ($possibleDates as $possibleDate) { |
|
91 | + if ($now > $possibleDate) { // The current date is in the past |
|
92 | 92 | continue; |
93 | 93 | } |
94 | - if($current === null || $possibleDate < $current) { |
|
94 | + if ($current === null || $possibleDate < $current) { |
|
95 | 95 | $current = $possibleDate; |
96 | 96 | } |
97 | 97 | } |
98 | - if($current !== null) { |
|
98 | + if ($current !== null) { |
|
99 | 99 | return $current; |
100 | 100 | } |
101 | 101 | throw new RuntimeException('No alternative lays in the future'); |