@@ -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'); |