Skip to content

Commit

Permalink
Better maintability.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Barášek committed Apr 22, 2021
1 parent ce0ae9c commit 13b4ae5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/Emailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -119,7 +120,8 @@ public function sendMessageToAdministrators(string $subject, string $message, ?a
$url = Url::get()->getCurrentUrl();

return '<p>URL: <a href="' . $url . '" target="_blank">' . $url . '</a></p>';
} catch (\Throwable $e) {
} catch (\Throwable) {
// Silence is golden.
}
if (isset($_SERVER['argv'][0])) {
return '<p>CRON - ARGS: ' . implode(' | ', $_SERVER['argv']) . '</p>';
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -287,15 +296,17 @@ 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));
} elseif ($message instanceof Message) {
$email->setSendEarliestAt($message->getSendEarliestAt());
}

$this->entityManager->persist($email)->flush();
$this->entityManager->persist($email);
$this->entityManager->flush();

return $email;
}
Expand Down
6 changes: 6 additions & 0 deletions src/EmailerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/EmailerLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 3 additions & 2 deletions src/Entity/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
15 changes: 9 additions & 6 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}


Expand All @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/BaseTemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final public function getDefaultParameters(): array
{
try {
$locale = $this->localization->getLocale();
} catch (\Throwable $e) {
} catch (\Throwable) {
$locale = $this->localization->getDefaultLocale();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Send/QueueRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Nette\Mail\SendException;
use Tracy\Debugger;
use Tracy\ILogger;

final class QueueRunner
{
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 13b4ae5

Please sign in to comment.