Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent 360ef1e commit dd38ac6
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 163 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@
"require": {
"php": ">=8.1",
"ext-json": "*",
"spiral/core": "^3.14.10",
"spiral/queue": "^3.14.10",
"spiral/logger": "^3.14.10",
"spiral/mailer": "^3.14.10",
"spiral/views": "^3.14.10",
"spiral/core": "^3.15",
"spiral/queue": "^3.15",
"spiral/logger": "^3.15",
"spiral/mailer": "^3.15",
"spiral/views": "^3.15",
"symfony/mailer": "^5.1 || ^6.0 || ^7.0",
"psr/event-dispatcher": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
"mockery/mockery": "^1.5",
"spiral/boot": "^3.14.10",
"spiral/stempler-bridge": "^3.14.10",
"spiral/boot": "^3.15",
"spiral/stempler-bridge": "^3.15",
"spiral/testing": "^2.8",
"vimeo/psalm": "^5.9"
},
Expand Down
1 change: 0 additions & 1 deletion src/Bootloader/BuilderBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class BuilderBootloader extends Bootloader
protected const DEPENDENCIES = [
StemplerBootloader::class,
];

protected const SINGLETONS = [
RendererInterface::class => ViewRenderer::class,
];
Expand Down
19 changes: 8 additions & 11 deletions src/Bootloader/MailerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Config\ConfiguratorInterface;
Expand Down Expand Up @@ -36,7 +35,6 @@ class MailerBootloader extends Bootloader
QueueBootloader::class,
BuilderBootloader::class,
];

protected const SINGLETONS = [
MailJob::class => MailJob::class,
SymfonyMailer::class => [self::class, 'mailer'],
Expand All @@ -47,9 +45,8 @@ class MailerBootloader extends Bootloader
];

public function __construct(
private readonly ConfiguratorInterface $config
) {
}
private readonly ConfiguratorInterface $config,
) {}

public function init(EnvironmentInterface $env): void
{
Expand All @@ -65,10 +62,10 @@ public function boot(BinderInterface $binder, ContainerInterface $container): vo
{
$binder->bindSingleton(
MailerInterface::class,
static fn (MailerConfig $config, QueueConnectionProviderInterface $provider): MailQueue => new MailQueue(
static fn(MailerConfig $config, QueueConnectionProviderInterface $provider): MailQueue => new MailQueue(
$config,
$provider->getConnection($config->getQueueConnection())
)
$provider->getConnection($config->getQueueConnection()),
),
);

$registry = $container->get(QueueRegistry::class);
Expand All @@ -85,7 +82,7 @@ public function mailer(TransportInterface $transport, ?EventDispatcherInterface
{
return new Mailer(
transport: $transport,
dispatcher: $dispatcher
dispatcher: $dispatcher,
);
}

Expand All @@ -96,11 +93,11 @@ protected function initTransportResolver(
$defaultTransports = \iterator_to_array(Transport::getDefaultFactories(
$dispatcher,
null,
$logs?->getLogger('mailer')
$logs?->getLogger('mailer'),
));

return new TransportResolver(
new Transport($defaultTransports)
new Transport($defaultTransports),
);
}
}
2 changes: 1 addition & 1 deletion src/Config/MailerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
'from' => '',
'queue' => null,
'queueConnection' => null,
]
],
) {
parent::__construct($config);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Event/MessageNotSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ final class MessageNotSent
{
public function __construct(
public readonly Email $message,
public readonly \Throwable $exception
) {
}
public readonly \Throwable $exception,
) {}
}
5 changes: 2 additions & 3 deletions src/Event/MessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
final class MessageSent
{
public function __construct(
public readonly Email $message
) {
}
public readonly Email $message,
) {}
}
17 changes: 8 additions & 9 deletions src/Listener/LoggerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
final class LoggerListener
{
public function __construct(
private readonly LoggerInterface $logger
) {
}
private readonly LoggerInterface $logger,
) {}

public function onMessageSent(MessageSent $event): void
{
$this->logger->debug(
\sprintf(
'Sent `%s` to "%s"',
$event->message->getSubject(),
\implode('", "', $this->getRecipients($event->message))
\implode('", "', $this->getRecipients($event->message)),
),
['emails' => $this->getRecipients($event->message)]
['emails' => $this->getRecipients($event->message)],
);
}

Expand All @@ -36,17 +35,17 @@ public function onMessageNotSent(MessageNotSent $event): void
'Failed to send `%s` to "%s": %s',
$event->message->getSubject(),
\implode('", "', $this->getRecipients($event->message)),
$event->exception->getMessage()
$event->exception->getMessage(),
),
['emails' => $this->getRecipients($event->message)]
['emails' => $this->getRecipients($event->message)],
);
}

private function getRecipients(Email $message): array
{
return \array_map(
static fn (Address $address): string => $address->toString(),
\array_merge($message->getTo(), $message->getCc(), $message->getBcc())
static fn(Address $address): string => $address->toString(),
\array_merge($message->getTo(), $message->getCc(), $message->getBcc()),
);
}
}
5 changes: 2 additions & 3 deletions src/MailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public function __construct(
private readonly MailerConfig $config,
private readonly SymfonyMailer $mailer,
private readonly RendererInterface $renderer,
private readonly ?EventDispatcherInterface $dispatcher = null
) {
}
private readonly ?EventDispatcherInterface $dispatcher = null,
) {}

/**
* @throws TransportExceptionInterface
Expand Down
7 changes: 3 additions & 4 deletions src/MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ final class MailQueue implements MailerInterface

public function __construct(
private readonly MailerConfig $config,
private readonly QueueInterface $queue
) {
}
private readonly QueueInterface $queue,
) {}

public function send(MessageInterface ...$message): void
{
Expand All @@ -28,7 +27,7 @@ public function send(MessageInterface ...$message): void
$this->queue->push(
self::JOB_NAME,
MessageSerializer::pack($msg),
$options->withDelay($msg->getOptions()['delay'] ?? null)
$options->withDelay($msg->getOptions()['delay'] ?? null),
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/MessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

final class MessageSerializer
{
public function serialize(string $jobType, array $payload): string
{
return \json_encode($payload, JSON_THROW_ON_ERROR);
}

public static function pack(MessageInterface $message): array
{
return [
Expand Down Expand Up @@ -45,4 +40,9 @@ public static function unpack(array $payload): MessageInterface

return $message;
}

public function serialize(string $jobType, array $payload): string
{
return \json_encode($payload, JSON_THROW_ON_ERROR);
}
}
51 changes: 25 additions & 26 deletions src/Renderer/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@
final class ViewRenderer implements RendererInterface
{
public function __construct(
private readonly ViewsInterface $views
) {
private readonly ViewsInterface $views,
) {}

/**
* Copy-pasted form https://stackoverflow.com/a/20806227
* Make sure the subject is ASCII-clean
*
* @param string $subject Subject to encode
* @return string Encoded subject
*/
public static function escapeSubject(string $subject): string
{
if (!preg_match('/[^\x20-\x7e]/', $subject)) {
// ascii-only subject, return as-is
return $subject;
}

// Subject is non-ascii, needs encoding
$encoded = \base64_encode($subject);
$prefix = '=?UTF-8?B?';
$suffix = '?=';

return $prefix . \str_replace("=\r\n", $suffix . "\r\n " . $prefix, $encoded) . $suffix;
}

public function render(MessageInterface $message): Email
Expand All @@ -26,7 +47,7 @@ public function render(MessageInterface $message): Email
throw new MailerException(
\sprintf('Invalid email template `%s`: %s', $message->getSubject(), $e->getMessage()),
$e->getCode(),
$e
$e,
);
}

Expand All @@ -47,32 +68,10 @@ public function render(MessageInterface $message): Email
throw new MailerException(
\sprintf('Unable to render email `%s`: %s', $message->getSubject(), $e->getMessage()),
$e->getCode(),
$e
$e,
);
}

return $msg;
}

/**
* Copy-pasted form https://stackoverflow.com/a/20806227
* Make sure the subject is ASCII-clean
*
* @param string $subject Subject to encode
* @return string Encoded subject
*/
public static function escapeSubject(string $subject): string
{
if (!preg_match('/[^\x20-\x7e]/', $subject)) {
// ascii-only subject, return as-is
return $subject;
}

// Subject is non-ascii, needs encoding
$encoded = \base64_encode($subject);
$prefix = '=?UTF-8?B?';
$suffix = '?=';

return $prefix . \str_replace("=\r\n", $suffix . "\r\n " . $prefix, $encoded) . $suffix;
}
}
3 changes: 1 addition & 2 deletions src/TransportResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class TransportResolver implements TransportResolverInterface, TransportRe

public function __construct(
private readonly Transport $transport,
) {
}
) {}

public function registerTransport(TransportFactoryInterface $factory): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/App/config/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
return [
'dsn' => 'smtp://user:pass@smtp.example.com',
'queue' => 'local',
'from' => 'test@spiral.framework'
'from' => 'test@spiral.framework',
];
22 changes: 11 additions & 11 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function testConfig(): void
'queueConnection' => 'foo',
]);

$this->assertSame('mailer-dsn', $cfg->getDSN());
$this->assertSame('admin@spiral.dev', $cfg->getFromAddress());
$this->assertSame('emails', $cfg->getQueue());
$this->assertSame('foo', $cfg->getQueueConnection());
self::assertSame('mailer-dsn', $cfg->getDSN());
self::assertSame('admin@spiral.dev', $cfg->getFromAddress());
self::assertSame('emails', $cfg->getQueue());
self::assertSame('foo', $cfg->getQueueConnection());
}

public function testDefaultConfig(): void
Expand All @@ -36,10 +36,10 @@ public function testDefaultConfig(): void
'queueConnection' => $env->get('MAILER_QUEUE_CONNECTION'),
]);

$this->assertSame('', $config->getDSN());
$this->assertSame('Spiral <sendit@local.host>', $config->getFromAddress());
$this->assertSame('local', $config->getQueue());
$this->assertNull($config->getQueueConnection());
self::assertSame('', $config->getDSN());
self::assertSame('Spiral <sendit@local.host>', $config->getFromAddress());
self::assertSame('local', $config->getQueue());
self::assertNull($config->getQueueConnection());
}

public function testDefaultConfigWithQueue(): void
Expand All @@ -50,7 +50,7 @@ public function testDefaultConfigWithQueue(): void
'queue' => $env->get('MAILER_QUEUE', 'local'),
]);

$this->assertSame('emails', $config->getQueue());
self::assertSame('emails', $config->getQueue());
}

public function testQueueWithNull(): void
Expand All @@ -59,12 +59,12 @@ public function testQueueWithNull(): void
'queue' => null,
]);

$this->assertNull($config->getQueue());
self::assertNull($config->getQueue());
}

public function testGetsQueueConnectionWithoutKey(): void
{
$cfg = new MailerConfig();
$this->assertNull($cfg->getQueueConnection());
self::assertNull($cfg->getQueueConnection());
}
}
Loading

0 comments on commit dd38ac6

Please sign in to comment.