Skip to content

Commit

Permalink
Adds support for Spiral Framework 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed May 27, 2022
1 parent 3306cb7 commit a13fe0c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ application.

Make sure that your server is configured with following PHP version and extensions:

- PHP 8.0+
- Spiral framework 2.9+
- PHP 8.1+
- Spiral framework 3.0+

## Installation

Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.1",
"doctrine/inflector": "^2.0",
"spiral/boot": "^2.9",
"spiral/config": "^2.9",
"spiral/core": "^2.9",
"spiral/queue": "^2.9",
"spiral/sendit": "^2.9",
"spiral/snapshots": "^2.9",
"spiral/boot": "^3.0",
"spiral/config": "^3.0",
"spiral/core": "^3.0",
"spiral/queue": "^3.0",
"spiral/sendit": "^3.0",
"spiral/snapshots": "^3.0",
"symfony/messenger": "^6.0",
"symfony/notifier": "^6.0"
},
"require-dev": {
"spiral/testing": "^1.2",
"spiral/framework": "^3.0",
"spiral/testing": "^2.0",
"symfony/firebase-notifier": "^6.0",
"vimeo/psalm": "^4.9"
},
Expand Down
7 changes: 4 additions & 3 deletions src/Bootloader/NotificationsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class NotificationsBootloader extends Bootloader
Notifier::class => [self::class, 'initNotifier'],
];

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

public function boot(EnvironmentInterface $env): void
public function init(EnvironmentInterface $env): void
{
$this->config->setDefaults(
NotificationsConfig::CONFIG,
Expand Down
6 changes: 3 additions & 3 deletions src/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ final class ChannelManager
private array $channels = [];

public function __construct(
private FactoryInterface $factory,
private NotificationsConfig $config,
private MailerConfig $mailerConfig,
private readonly FactoryInterface $factory,
private readonly NotificationsConfig $config,
private readonly MailerConfig $mailerConfig,
) {
}

Expand Down
18 changes: 11 additions & 7 deletions src/Config/NotificationsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class NotificationsConfig extends InjectableConfig
{
public const CONFIG = 'notifications';

protected $config = [
protected array $config = [
'queueConnection' => null,
'channels' => [],
'transports' => [],
Expand Down Expand Up @@ -43,26 +43,28 @@ public function getChannelPolicies(): array
public function getChannel(string $name): array
{
if (! isset($this->config['channels'][$name])) {
throw new TransportException(sprintf('Channel with given name `%s` is not found.', $name));
throw new TransportException(
\sprintf('Channel with given name `%s` is not found.', $name)
);
}

$channel = $this->config['channels'][$name];

if (! \is_array($channel)) {
throw new InvalidArgumentException(
sprintf('Config for channel `%s` must be an array.', $name)
\sprintf('Config for channel `%s` must be an array.', $name)
);
}

if (! isset($channel['type'])) {
throw new InvalidArgumentException(
sprintf('Config for channel `%s` should contain `type` key.', $name)
\sprintf('Config for channel `%s` should contain `type` key.', $name)
);
}

if (! isset($channel['transport'])) {
throw new InvalidArgumentException(
sprintf('Config for channel `%s` should contain `transport` key.', $name)
\sprintf('Config for channel `%s` should contain `transport` key.', $name)
);
}

Expand All @@ -86,14 +88,16 @@ public function getTransport(array $names): array

foreach ($names as $name) {
if (! isset($this->config['transports'][$name])) {
throw new TransportException(sprintf('Transport with given name `%s` is not found.', $name));
throw new TransportException(
\sprintf('Transport with given name `%s` is not found.', $name)
);
}

$transport = $this->config['transports'][$name];

if (! \is_string($transport)) {
throw new InvalidArgumentException(
sprintf('Config for transport `%s` must be a DSN string', $name)
\sprintf('Config for transport `%s` must be a DSN string', $name)
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ final class Notifier implements NotifierInterface
private array $adminRecipients = [];

public function __construct(
private ChannelManager $channelManager,
private NotificationsConfig $config,
private QueueConnectionProviderInterface $queue,
private ?ChannelPolicyInterface $policy = null
private readonly ChannelManager $channelManager,
private readonly NotificationsConfig $config,
private readonly QueueConnectionProviderInterface $queue,
private readonly ?ChannelPolicyInterface $policy = null
) {
}

Expand Down Expand Up @@ -89,21 +89,21 @@ private function getChannels(Notification $notification, RecipientInterface $rec
$channels = $notification->getChannels($recipient);

if (! $channels) {
$errorPrefix = sprintf(
$errorPrefix = \sprintf(
'Unable to determine which channels to use to send the "%s" notification',
\get_class($notification)
);

$error = 'you should either pass channels in the constructor, override its "getChannels()" method';
if (null === $this->policy) {
throw new LogicException(
sprintf('%s; %s, or configure a "%s".', $errorPrefix, $error, ChannelPolicy::class)
\sprintf('%s; %s, or configure a "%s".', $errorPrefix, $error, ChannelPolicy::class)
);
}

if (! $channels = $this->policy->getChannels($notification->getImportance())) {
throw new LogicException(
sprintf(
\sprintf(
'%s; the "%s" returns no channels for importance "%s"; %s.',
$errorPrefix,
ChannelPolicy::class,
Expand Down
6 changes: 3 additions & 3 deletions src/SendNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class SendNotificationJob implements HandlerInterface
{
public function __construct(
private NotifierInterface $notifier
private readonly NotifierInterface $notifier
) {
}

Expand All @@ -32,7 +32,7 @@ public function handle(string $name, string $id, array $payload): void

if (! $payload['notification'] instanceof Notification) {
throw new InvalidArgumentException(
sprintf(
\sprintf(
'Payload `notification` key value type should be instance of `%s`',
Notification::class
)
Expand All @@ -46,7 +46,7 @@ public function handle(string $name, string $id, array $payload): void

if (! $payload['recipient'] instanceof RecipientInterface) {
throw new InvalidArgumentException(
sprintf(
\sprintf(
'Payload `recipient` key value type should be instance of `%s`',
RecipientInterface::class
)
Expand Down

0 comments on commit a13fe0c

Please sign in to comment.