Skip to content

Commit

Permalink
Merge pull request #28 from onliner/php8-update
Browse files Browse the repository at this point in the history
Bump min PHP version to 8.0
  • Loading branch information
zloyuser authored Dec 20, 2023
2 parents 295f49a + fc6f659 commit 0772d37
Show file tree
Hide file tree
Showing 39 changed files with 290 additions and 413 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
php: ['7.2', '7.3', '7.4', '8.0']
php: ['8.0', '8.1', '8.2', '8.3']
name: PHP ${{ matrix.php }} Test on ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -18,7 +18,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl
coverage: none

- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"keywords": ["command-bus", "command", "service-bus"],
"license": "MIT",
"require": {
"php": "^7.2 || ^8.0",
"php": "^8.0",
"psr/container": "^1.0 || ^2.0",
"psr/log": "^1.1 || ^2.0",
"php-amqplib/php-amqplib": "^2.12 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.0",
"phpstan/phpstan": "^0.12.14"
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ final class Builder
/**
* @var array<string, callable>
*/
private $handlers = [];
private array $handlers = [];

/**
* @var array<string, Middleware>
*/
private $middleware = [];
private array $middleware = [];

/**
* @var array<string, Extension>
*/
private $extensions = [];
private array $extensions = [];

/**
* @param string $command
Expand Down
65 changes: 35 additions & 30 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,50 @@

namespace Onliner\CommandBus;

use Onliner\CommandBus\Message\MessageIterator;
use Onliner\CommandBus\Message\DeferredIterator;

final class Context
{
/**
* @var Dispatcher
*/
private $dispatcher;

/**
* @var MessageIterator
*/
private $deferred;
private const OPTION_LOCAL = 'local';

/**
* @var array<mixed>
* @param Dispatcher $dispatcher
* @param DeferredIterator $deferred
* @param array<string, mixed> $options
*/
private $options;
public function __construct(
private Dispatcher $dispatcher,
private DeferredIterator $deferred,
private array $options = []
) {
}

/**
* @internal
* @param object $message
* @param array<string, mixed> $options
*
* @param Dispatcher $dispatcher
* @param MessageIterator<array> $deferred
* @param array<mixed> $options
* @return void
*/
public function __construct(Dispatcher $dispatcher, MessageIterator $deferred, array $options = [])
public function dispatch(object $message, array $options = []): void
{
$this->dispatcher = $dispatcher;
$this->deferred = $deferred;
$this->options = $options;
$this->dispatcher->dispatch($message, $options);
}

/**
* @param object $message
* @param array<mixed> $options
*
* @param object $message
* @param array<string, mixed> $options
* @return void
*/
public function dispatch(object $message, array $options = []): void
public function execute(object $message, array $options = []): void
{
$this->dispatcher->dispatch($message, $options);
$this->dispatcher->dispatch($message, array_replace($options, [
self::OPTION_LOCAL => true,
]));
}

/**
* @param object $message
* @param array<mixed> $options
* @param object $message
* @param array<string, mixed> $options
*
* @return self
*/
Expand All @@ -62,7 +59,7 @@ public function defer(object $message, array $options = []): self
}

/**
* @return array<mixed>
* @return array<string, mixed>
*/
public function all(): array
{
Expand All @@ -85,7 +82,7 @@ public function has(string $option): bool
*
* @return mixed
*/
public function get(string $option, $default = null)
public function get(string $option, mixed $default = null): mixed
{
return $this->options[$option] ?? $default;
}
Expand All @@ -96,7 +93,7 @@ public function get(string $option, $default = null)
*
* @return self
*/
public function set(string $option, $value): self
public function set(string $option, mixed $value): self
{
$this->options[$option] = $value;

Expand All @@ -114,4 +111,12 @@ public function del(string $option): self

return $this;
}

/**
* @return bool
*/
public function isLocal(): bool
{
return $this->has(self::OPTION_LOCAL);
}
}
24 changes: 8 additions & 16 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,33 @@

namespace Onliner\CommandBus;

use Onliner\CommandBus\Message\MessageIterator;
use Onliner\CommandBus\Message\DeferredIterator;

final class Dispatcher
{
/**
* @var Resolver
*/
private $resolver;

/**
* @param Resolver $resolver
*/
public function __construct(Resolver $resolver)
public function __construct(private Resolver $resolver)
{
$this->resolver = $resolver;
}

/**
* @param object $message
* @param array<mixed> $options
* @param object $message
* @param array<string, mixed> $options
*
* @return void
*/
public function dispatch(object $message, array $options = []): void
{
$handler = $this->resolver->resolve($message);
$deferred = new MessageIterator();
$context = new Context($this, $deferred, $options);
$iterator = new DeferredIterator();
$context = new Context($this, $iterator, $options);

$handler($message, $context);

foreach ($deferred as $item) {
[$deferredMessage, $deferredOptions] = $item;

$this->dispatch($deferredMessage, $deferredOptions);
foreach ($iterator as $deferred) {
$this->dispatch($deferred->message, $deferred->options);
}
}
}
13 changes: 13 additions & 0 deletions src/Exception/InvalidHandlerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Onliner\CommandBus\Exception;

final class InvalidHandlerException extends CommandBusException
{
public function __construct(string $class)
{
parent::__construct(sprintf('Handler for command "%s" must be callable.', $class));
}
}
13 changes: 13 additions & 0 deletions src/Exception/InvalidMessageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Onliner\CommandBus\Exception;

final class InvalidMessageException extends CommandBusException
{
public function __construct(string $class)
{
parent::__construct(sprintf('Invalid message for class "%s".', $class));
}
}
4 changes: 2 additions & 2 deletions src/Exception/UnknownHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

final class UnknownHandlerException extends CommandBusException
{
public static function forCommand(object $command): self
public function __construct(string $class)
{
return new self(sprintf('Handler for command "%s" not found.', get_class($command)));
parent::__construct(sprintf('Handler for command "%s" not found.', $class));
}
}
18 changes: 18 additions & 0 deletions src/Message/Deferred.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Onliner\CommandBus\Message;

class Deferred
{
/**
* @param object $message
* @param array<string, mixed> $options
*/
public function __construct(
public object $message,
public array $options,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
use IteratorAggregate;

/**
* @implements IteratorAggregate<array>
* @implements IteratorAggregate<Deferred>
*/
class MessageIterator implements IteratorAggregate
class DeferredIterator implements IteratorAggregate
{
/**
* @var array<array>
* @var array<Deferred>
*/
private $messages = [];
private array $messages = [];

/**
* @param object $message
* @param array<mixed> $options
* @param object $message
* @param array<string, mixed> $options
*
* @return self<array>
* @return self
*/
public function append(object $message, array $options): self
{
$this->messages[] = [$message, $options];
$this->messages[] = new Deferred($message, $options);

return $this;
}

/**
* @return Generator<array>
* @return Generator<Deferred>
*/
public function getIterator(): Generator
{
Expand Down
14 changes: 1 addition & 13 deletions src/Middleware/LoggerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@

final class LoggerMiddleware implements Middleware
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @var string
*/
private $level;

/**
* @param LoggerInterface $logger
* @param string $level
*/
public function __construct(LoggerInterface $logger, string $level = LogLevel::ERROR)
public function __construct(private LoggerInterface $logger, private string $level = LogLevel::ERROR)
{
$this->logger = $logger;
$this->level = $level;
}

/**
Expand Down
Loading

0 comments on commit 0772d37

Please sign in to comment.