From 13b4ae5990f4c6b8c0e1fcd108ae022fbac3f7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 22 Apr 2021 16:25:20 +0200 Subject: [PATCH] Better maintability. --- src/Emailer.php | 25 ++++++++++++++++++------- src/EmailerExtension.php | 6 ++++++ src/EmailerLogger.php | 2 +- src/Entity/Log.php | 5 +++-- src/Helper.php | 15 +++++++++------ src/Renderer/BaseTemplateRenderer.php | 2 +- src/Send/QueueRunner.php | 3 ++- 7 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Emailer.php b/src/Emailer.php index f41d7ab..2744ef8 100644 --- a/src/Emailer.php +++ b/src/Emailer.php @@ -83,7 +83,8 @@ public function send(NetteMessage $mail): void public function sendNow(NetteMessage $message): void { - if (($email = $this->insertMessageToQueue($message)) !== null) { + $email = $this->insertMessageToQueue($message); + if ($email !== null) { if ($email->getStatus() === Email::STATUS_SENT) { return; } @@ -119,7 +120,8 @@ public function sendMessageToAdministrators(string $subject, string $message, ?a $url = Url::get()->getCurrentUrl(); return '

URL: ' . $url . '

'; - } catch (\Throwable $e) { + } catch (\Throwable) { + // Silence is golden. } if (isset($_SERVER['argv'][0])) { return '

CRON - ARGS: ' . implode(' | ', $_SERVER['argv']) . '

'; @@ -174,8 +176,11 @@ public function getEmailServiceByType( $locale = $this->localization->getDefaultLocale(); } - if (($templatePath = $email->getTemplate($locale)) === null) { - throw new EmailerException('Email template for mail "' . $type . '" and locale "' . $locale . '" does not exist.'); + $templatePath = $email->getTemplate($locale); + if ($templatePath === null) { + throw new EmailerException( + 'Email template for mail "' . $type . '" and locale "' . $locale . '" does not exist.', + ); } $subject = $parameters['subject'] ?? $message->getSubject(); @@ -189,7 +194,11 @@ public function getEmailServiceByType( $from = $parameters['from'] ?? $this->configuration->getDefaultFrom() - ?? throw new \InvalidArgumentException('Parameter "from" does not exist. Did you defined default configuration?'); + ?? null; + + if ($from === null) { + throw new \InvalidArgumentException('Parameter "from" does not exist. Did you defined default configuration?'); + } $message->setFrom($this->fixer->fix($from)); if (isset($parameters['to']) === true) { @@ -287,7 +296,8 @@ private function insertMessageToQueue(NetteMessage $message, string $sendEarlies $email = new Email($this->messageEntity->toEntity($message)); try { $email->setLocale($this->localization->getLocale()); - } catch (\Throwable $e) { + } catch (\Throwable) { + // Locale should be unknown } if ($sendEarliestAt !== 'now') { $email->setSendEarliestAt(DateTime::from($sendEarliestAt)); @@ -295,7 +305,8 @@ private function insertMessageToQueue(NetteMessage $message, string $sendEarlies $email->setSendEarliestAt($message->getSendEarliestAt()); } - $this->entityManager->persist($email)->flush(); + $this->entityManager->persist($email); + $this->entityManager->flush(); return $email; } diff --git a/src/EmailerExtension.php b/src/EmailerExtension.php index 02789b3..282db36 100644 --- a/src/EmailerExtension.php +++ b/src/EmailerExtension.php @@ -14,6 +14,8 @@ use Nette\Schema\Expect; use Nette\Schema\Schema; use Nette\Utils\FileSystem; +use Tracy\Debugger; +use Tracy\ILogger; final class EmailerExtension extends CompilerExtension { @@ -53,6 +55,10 @@ public function beforeCompile(): void $dicMailConfiguration = $netteMailerArguments; $builder->removeDefinition('mail.mailer'); } catch (MissingServiceException $e) { + Debugger::log( + new \LogicException('Mailer is broken: ' . $e->getMessage(), $e->getCode(), $e), + ILogger::EXCEPTION, + ); } /** @var mixed[] $config */ diff --git a/src/EmailerLogger.php b/src/EmailerLogger.php index c360d71..d567753 100644 --- a/src/EmailerLogger.php +++ b/src/EmailerLogger.php @@ -22,7 +22,7 @@ public function __construct( */ public function log(string $level, string $message, ?Email $email = null): void { - $this->entityManager->persist($log = new Log($level, $message, $email)); + $this->entityManager->persist(new Log($level, $message, $email)); $this->entityManager->flush(); } } diff --git a/src/Entity/Log.php b/src/Entity/Log.php index 747f130..10f3eae 100644 --- a/src/Entity/Log.php +++ b/src/Entity/Log.php @@ -48,8 +48,9 @@ class Log public function __construct(string $level, string $message, ?Email $email = null) { - if (\in_array($level = strtoupper($level), [self::LEVEL_WARNING, self::LEVEL_ERROR, self::LEVEL_INFO], true) === false) { - trigger_error('Log: Level "' . $level . '" is not supported.'); + $level = strtoupper($level); + if (\in_array($level, [self::LEVEL_WARNING, self::LEVEL_ERROR, self::LEVEL_INFO], true) === false) { + trigger_error(__METHOD__ . ': Level "' . $level . '" is not supported.'); $level = self::LEVEL_ERROR; } diff --git a/src/Helper.php b/src/Helper.php index 199021f..73f605d 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -16,11 +16,11 @@ public function __construct() public static function formatDurationFrom(int $fromMicroTime, ?int $nowMicroTime = null): string { - if (($microTime = ($nowMicroTime ?: (int) microtime(true)) - $fromMicroTime) >= 1) { - return number_format($microTime, 3, '.', ' ') . ' s'; - } + $microTime = ($nowMicroTime ?: (int) microtime(true)) - $fromMicroTime; - return number_format($microTime * 1_000, 2, '.', ' ') . ' ms'; + return $microTime >= 1 + ? number_format($microTime, 3, '.', ' ') . ' s' + : number_format($microTime * 1_000, 2, '.', ' ') . ' ms'; } @@ -39,8 +39,11 @@ public static function userIp(): string if (isset($_SERVER['REMOTE_ADDR'])) { if (\in_array($_SERVER['REMOTE_ADDR'], ['::1', '0.0.0.0', 'localhost'], true)) { $ip = '127.0.0.1'; - } elseif (($ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) === false) { - $ip = '127.0.0.1'; + } else { + $ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); + if ($ip === false) { + $ip = '127.0.0.1'; + } } } else { $ip = '127.0.0.1'; diff --git a/src/Renderer/BaseTemplateRenderer.php b/src/Renderer/BaseTemplateRenderer.php index 84e7610..6920521 100644 --- a/src/Renderer/BaseTemplateRenderer.php +++ b/src/Renderer/BaseTemplateRenderer.php @@ -51,7 +51,7 @@ final public function getDefaultParameters(): array { try { $locale = $this->localization->getLocale(); - } catch (\Throwable $e) { + } catch (\Throwable) { $locale = $this->localization->getDefaultLocale(); } diff --git a/src/Send/QueueRunner.php b/src/Send/QueueRunner.php index 3fc7a49..c4f2d07 100644 --- a/src/Send/QueueRunner.php +++ b/src/Send/QueueRunner.php @@ -13,6 +13,7 @@ use Doctrine\ORM\Tools\Pagination\Paginator; use Nette\Mail\SendException; use Tracy\Debugger; +use Tracy\ILogger; final class QueueRunner { @@ -81,7 +82,7 @@ public function run(): int $result++; } catch (\Throwable $e) { echo 'E'; - Debugger::log($e); + Debugger::log($e, ILogger::CRITICAL); $this->logger->log(Log::LEVEL_ERROR, 'Failed to send: ' . $e->getMessage() . ', details on Tracy logger.', $email);