Skip to content

Commit

Permalink
Framework assertions and collections
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Jun 10, 2020
1 parent 42a4036 commit f010b3e
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 129 deletions.
473 changes: 415 additions & 58 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions psalm.xml
Expand Up @@ -39,6 +39,7 @@
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />
<MixedClone errorLevel="suppress" />

<RedundantCondition errorLevel="info" />

Expand Down
8 changes: 4 additions & 4 deletions src/Channel/Channel.php
Expand Up @@ -13,8 +13,8 @@
use Daikon\MessageBus\MessageBusInterface;
use Daikon\MessageBus\Channel\Subscription\SubscriptionInterface;
use Daikon\MessageBus\Channel\Subscription\SubscriptionMap;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Error\SubscriptionUnknown;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\SubscriptionUnknown;
use Daikon\Metadata\MetadataInterface;
use Daikon\Metadata\MetadataEnricherInterface;
use Daikon\Metadata\MetadataEnricherList;
Expand All @@ -39,7 +39,7 @@ public function __construct(
$this->subscriptions = $subscriptions;
$this->guard = $guard ?? fn(): bool => true;
$metadataEnrichers = $metadataEnrichers ?? new MetadataEnricherList;
$this->metadataEnrichers = $metadataEnrichers->prependEnricher(self::METADATA_KEY, $this->key);
$this->metadataEnrichers = $metadataEnrichers->enrichWith(self::METADATA_KEY, $this->key);
}

public function publish(EnvelopeInterface $envelope, MessageBusInterface $messageBus): void
Expand Down Expand Up @@ -85,7 +85,7 @@ function (MetadataInterface $metadata, MetadataEnricherInterface $metadataEnrich

private function accepts(EnvelopeInterface $envelope): bool
{
return (bool)call_user_func($this->guard, $envelope);
return (bool)($this->guard)($envelope);
}

private function verify(EnvelopeInterface $envelope): void
Expand Down
13 changes: 4 additions & 9 deletions src/Channel/ChannelMap.php
Expand Up @@ -8,23 +8,18 @@

namespace Daikon\MessageBus\Channel;

use Daikon\DataStructure\TypedMapInterface;
use Daikon\DataStructure\TypedMapTrait;
use InvalidArgumentException;
use Daikon\DataStructure\TypedMap;
use Daikon\Interop\Assertion;

final class ChannelMap implements TypedMapInterface
final class ChannelMap extends TypedMap
{
use TypedMapTrait;

public function __construct(iterable $channels = [])
{
$mappedChannels = [];
/** @var ChannelInterface $channel */
foreach ($channels as $channel) {
$channelKey = $channel->getKey();
if (isset($mappedChannels[$channelKey])) {
throw new InvalidArgumentException("Channel key '$channelKey' is already defined.");
}
Assertion::keyNotExists($mappedChannels, $channelKey, "Channel key '$channelKey' is already defined.");
$mappedChannels[$channelKey] = $channel;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Channel/Subscription/LazySubscription.php
Expand Up @@ -16,10 +16,10 @@ final class LazySubscription implements SubscriptionInterface
{
private string $key;

private SubscriptionInterface $compositeSubscription;

private Closure $factoryCallback;

private SubscriptionInterface $compositeSubscription;

public function __construct(
string $key,
Closure $transport,
Expand All @@ -28,7 +28,7 @@ public function __construct(
Closure $metadataEnrichers = null
) {
$this->key = $key;
$this->factoryCallback = fn(): SubscriptionInterface =>
$this->factoryCallback = fn(): Subscription =>
new Subscription(
$this->key,
$transport(),
Expand Down Expand Up @@ -61,7 +61,7 @@ private function getSubscription(): SubscriptionInterface
{
/** @psalm-suppress TypeDoesNotContainType */
if (!isset($this->compositeSubscription)) {
$this->compositeSubscription = call_user_func($this->factoryCallback);
$this->compositeSubscription = ($this->factoryCallback)();
unset($this->factoryCallback);
}
return $this->compositeSubscription;
Expand Down
Expand Up @@ -8,13 +8,10 @@

namespace Daikon\MessageBus\Channel\Subscription\MessageHandler;

use Daikon\DataStructure\TypedListInterface;
use Daikon\DataStructure\TypedListTrait;
use Daikon\DataStructure\TypedList;

final class MessageHandlerList implements TypedListInterface
final class MessageHandlerList extends TypedList
{
use TypedListTrait;

public function __construct(iterable $messageHandlers = [])
{
$this->init($messageHandlers, [MessageHandlerInterface::class]);
Expand Down
6 changes: 3 additions & 3 deletions src/Channel/Subscription/Subscription.php
Expand Up @@ -12,7 +12,7 @@
use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerList;
use Daikon\MessageBus\Channel\Subscription\Transport\TransportInterface;
use Daikon\MessageBus\EnvelopeInterface;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\MessageBusInterface;
use Daikon\Metadata\MetadataInterface;
use Daikon\Metadata\MetadataEnricherInterface;
Expand Down Expand Up @@ -42,7 +42,7 @@ public function __construct(
$this->messageHandlers = $messageHandlers;
$this->guard = $guard ?? fn(): bool => true;
$metadataEnrichers = $metadataEnrichers ?? new MetadataEnricherList;
$this->metadataEnrichers = $metadataEnrichers->prependEnricher(self::METADATA_KEY, $this->key);
$this->metadataEnrichers = $metadataEnrichers->enrichWith(self::METADATA_KEY, $this->key);
}

public function publish(EnvelopeInterface $envelope, MessageBusInterface $messageBus): void
Expand Down Expand Up @@ -79,7 +79,7 @@ function (MetadataInterface $metadata, MetadataEnricherInterface $metadataEnrich

private function accepts(EnvelopeInterface $envelope): bool
{
return (bool)call_user_func($this->guard, $envelope);
return (bool)($this->guard)($envelope);
}

private function verify(EnvelopeInterface $envelope): void
Expand Down
17 changes: 8 additions & 9 deletions src/Channel/Subscription/SubscriptionMap.php
Expand Up @@ -8,23 +8,22 @@

namespace Daikon\MessageBus\Channel\Subscription;

use Daikon\DataStructure\TypedMapInterface;
use Daikon\DataStructure\TypedMapTrait;
use InvalidArgumentException;
use Daikon\DataStructure\TypedMap;
use Daikon\Interop\Assertion;

final class SubscriptionMap implements TypedMapInterface
final class SubscriptionMap extends TypedMap
{
use TypedMapTrait;

public function __construct(iterable $subscriptions = [])
{
$mappedSubscriptions = [];
/** @var SubscriptionInterface $subscription */
foreach ($subscriptions as $subscription) {
$subscriptionKey = $subscription->getKey();
if (isset($mappedSubscriptions[$subscriptionKey])) {
throw new InvalidArgumentException("Subscription key '$subscriptionKey' is already defined.");
}
Assertion::keyNotExists(
$mappedSubscriptions,
$subscriptionKey,
"Subscription key '$subscriptionKey' is already defined."
);
$mappedSubscriptions[$subscriptionKey] = $subscription;
}

Expand Down
17 changes: 8 additions & 9 deletions src/Channel/Subscription/Transport/TransportMap.php
Expand Up @@ -8,23 +8,22 @@

namespace Daikon\MessageBus\Channel\Subscription\Transport;

use Daikon\DataStructure\TypedMapInterface;
use Daikon\DataStructure\TypedMapTrait;
use InvalidArgumentException;
use Daikon\DataStructure\TypedMap;
use Daikon\Interop\Assertion;

final class TransportMap implements TypedMapInterface
final class TransportMap extends TypedMap
{
use TypedMapTrait;

public function __construct(iterable $transports = [])
{
$mappedTransports = [];
/** @var TransportInterface $transport */
foreach ($transports as $transport) {
$transportKey = $transport->getKey();
if (isset($mappedTransports[$transportKey])) {
throw new InvalidArgumentException("Transport key '$transportKey' is already defined.");
}
Assertion::keyNotExists(
$mappedTransports,
$transportKey,
"Transport key '$transportKey' is already defined."
);
$mappedTransports[$transportKey] = $transport;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Envelope.php
Expand Up @@ -9,7 +9,7 @@
namespace Daikon\MessageBus;

use DateTimeImmutable;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\MessageInterface;
use Daikon\Metadata\Metadata;
use Daikon\Metadata\MetadataInterface;
Expand Down Expand Up @@ -96,10 +96,10 @@ public static function fromNative($state): self

$messageType = $state['@message_type'] ?? null;
if (is_null($messageType) || !is_subclass_of($messageType, MessageInterface::class)) {
throw new EnvelopeNotAcceptable(sprintf(
"Message type '%s' given must be an instance of MessageInterface",
$messageType ?? 'null'
), EnvelopeNotAcceptable::UNPARSEABLE);
throw new EnvelopeNotAcceptable(
sprintf("Message type '%s' given must be an instance of MessageInterface.", $messageType ?? 'null'),
EnvelopeNotAcceptable::UNPARSEABLE
);
}

$metadataType = $state['@metadata_type'] ?? null;
Expand Down
Expand Up @@ -6,10 +6,10 @@
* file that was distributed with this source code.
*/

namespace Daikon\MessageBus\Error;
namespace Daikon\MessageBus\Exception;

use Exception;
use Daikon\Interop\RuntimeException;

final class SubscriptionUnknown extends Exception implements ErrorInterface
final class ChannelUnknown extends RuntimeException implements MessageBusException
{
}
Expand Up @@ -6,11 +6,11 @@
* file that was distributed with this source code.
*/

namespace Daikon\MessageBus\Error;
namespace Daikon\MessageBus\Exception;

use Exception;
use Daikon\Interop\RuntimeException;

final class EnvelopeNotAcceptable extends Exception implements ErrorInterface
final class EnvelopeNotAcceptable extends RuntimeException implements MessageBusException
{
public const SUBSCRIPTION_KEY_MISSING = 6000;
public const SUBSCRIPTION_KEY_UNEXPECTED = 6001;
Expand Down
Expand Up @@ -6,10 +6,10 @@
* file that was distributed with this source code.
*/

namespace Daikon\MessageBus\Error;
namespace Daikon\MessageBus\Exception;

use Exception;
use Daikon\Interop\DaikonException;

final class ChannelUnknown extends Exception implements ErrorInterface
interface MessageBusException extends DaikonException
{
}
Expand Up @@ -6,8 +6,10 @@
* file that was distributed with this source code.
*/

namespace Daikon\MessageBus\Error;
namespace Daikon\MessageBus\Exception;

interface ErrorInterface
use Daikon\Interop\RuntimeException;

final class SubscriptionUnknown extends RuntimeException implements MessageBusException
{
}
7 changes: 3 additions & 4 deletions src/MessageBus.php
Expand Up @@ -10,8 +10,8 @@

use Daikon\MessageBus\Channel\ChannelInterface;
use Daikon\MessageBus\Channel\ChannelMap;
use Daikon\MessageBus\Error\ChannelUnknown;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\ChannelUnknown;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\Metadata\MetadataInterface;
use Daikon\Metadata\Metadata;
use Daikon\Metadata\MetadataEnricherInterface;
Expand Down Expand Up @@ -41,9 +41,8 @@ public function publish(MessageInterface $message, string $channelKey, MetadataI
throw new ChannelUnknown("Channel '$channelKey' has not been registered on message bus.");
}
$metadata = $this->enrichMetadata($metadata ?? Metadata::makeEmpty());
$envelopeType = $this->envelopeType;
/** @var EnvelopeInterface $envelope */
$envelope = $envelopeType::wrap($message, $metadata);
$envelope = $this->envelopeType::wrap($message, $metadata);
/** @var ChannelInterface $channel */
$channel = $this->channelMap->get($channelKey);
$channel->publish($envelope, $this);
Expand Down
2 changes: 1 addition & 1 deletion tests/Channel/ChannelMapTest.php
Expand Up @@ -8,9 +8,9 @@

namespace Daikon\Tests\MessageBus;

use Daikon\Interop\InvalidArgumentException;
use Daikon\MessageBus\Channel\ChannelInterface;
use Daikon\MessageBus\Channel\ChannelMap;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class ChannelMapTest extends TestCase
Expand Down
4 changes: 2 additions & 2 deletions tests/Channel/ChannelTest.php
Expand Up @@ -14,8 +14,8 @@
use Daikon\MessageBus\Channel\Subscription\SubscriptionMap;
use Daikon\MessageBus\Envelope;
use Daikon\MessageBus\EnvelopeInterface;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Error\SubscriptionUnknown;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\SubscriptionUnknown;
use Daikon\MessageBus\MessageBusInterface;
use Daikon\MessageBus\MessageInterface;
use Daikon\Metadata\Metadata;
Expand Down
2 changes: 1 addition & 1 deletion tests/Channel/Subscription/SubscriptionMapTest.php
Expand Up @@ -8,9 +8,9 @@

namespace Daikon\Tests\MessageBus;

use Daikon\Interop\InvalidArgumentException;
use Daikon\MessageBus\Channel\Subscription\SubscriptionInterface;
use Daikon\MessageBus\Channel\Subscription\SubscriptionMap;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class SubscriptionMapTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/Channel/Subscription/SubscriptionTest.php
Expand Up @@ -16,7 +16,7 @@
use Daikon\MessageBus\Channel\Subscription\Transport\TransportInterface;
use Daikon\MessageBus\Envelope;
use Daikon\MessageBus\EnvelopeInterface;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\MessageBusInterface;
use Daikon\MessageBus\MessageInterface;
use Daikon\Metadata\Metadata;
Expand Down
2 changes: 1 addition & 1 deletion tests/Channel/Subscription/Transport/TransportMapTest.php
Expand Up @@ -8,9 +8,9 @@

namespace Daikon\Tests\MessageBus;

use Daikon\Interop\InvalidArgumentException;
use Daikon\MessageBus\Channel\Subscription\Transport\TransportInterface;
use Daikon\MessageBus\Channel\Subscription\Transport\TransportMap;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class TransportMapTest extends TestCase
Expand Down
4 changes: 2 additions & 2 deletions tests/MessageBusTest.php
Expand Up @@ -12,8 +12,8 @@
use Daikon\MessageBus\Channel\ChannelMap;
use Daikon\MessageBus\Envelope;
use Daikon\MessageBus\EnvelopeInterface;
use Daikon\MessageBus\Error\ChannelUnknown;
use Daikon\MessageBus\Error\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\ChannelUnknown;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\MessageBus;
use Daikon\MessageBus\MessageInterface;
use Daikon\Metadata\Metadata;
Expand Down

0 comments on commit f010b3e

Please sign in to comment.