diff --git a/src/Infrastructure/Application/Listener/Subscriber.php b/src/Infrastructure/Application/Listener/Subscriber.php new file mode 100644 index 00000000..e42af860 --- /dev/null +++ b/src/Infrastructure/Application/Listener/Subscriber.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Infrastructure\Application\Listener; + +use Doctrine\DBAL\Connection; +use Streak\Domain; +use Streak\Domain\Event; +use Streak\Domain\Event\Listener; + +class Subscriber implements Event\Listener +{ + use Event\Listener\Filtering; + use Event\Listener\Identifying; + use Event\Listener\Listening; + use Query\Handling; + + public function __construct(Subscriber\Id $id) + { + $this->identifyBy($id); + } + + public function listenerId(): Subscriber\Id + { + return $this->id; + } + + public function on(Envelope $event): bool + { + + } +} diff --git a/src/Infrastructure/Application/Listener/Subscriber/Id.php b/src/Infrastructure/Application/Listener/Subscriber/Id.php new file mode 100644 index 00000000..6878eeb0 --- /dev/null +++ b/src/Infrastructure/Application/Listener/Subscriber/Id.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Infrastructure\Application\Listener\Subscriber; + +use Streak\Domain\Event\Listener; + +/** + * @see \Printify\Tests\Invoices\Application\Projectors\InvoicesList\Projector\IdTest + */ +final class Id implements Listener\Id +{ + private const ID = '00000000-0000-0000-0000-000000000000'; + + public function equals(object $id): bool + { + if (!$id instanceof self) { + return false; + } + + return true; + } + + public function toString(): string + { + return self::ID; + } + + public static function fromString(string $id): self + { + return new self(); + } +} diff --git a/src/Infrastructure/Domain/UnitOfWork/EventStoreUnitOfWork.php b/src/Infrastructure/Domain/UnitOfWork/EventStoreUnitOfWork.php index 37df8a39..068794b7 100644 --- a/src/Infrastructure/Domain/UnitOfWork/EventStoreUnitOfWork.php +++ b/src/Infrastructure/Domain/UnitOfWork/EventStoreUnitOfWork.php @@ -104,25 +104,30 @@ public function commit(): \Generator $this->committing = true; try { + $events = []; + $producers = []; /** @var Event\Producer $producer */ while ($producer = array_shift($this->uncommited)) { - try { - $this->store->add(...$producer->events()); // maybe gather all events and send them in one single EventStore:add() call? - - if ($producer instanceof Domain\Versionable) { - $producer->commit(); - } - - yield $producer; - } catch (ConcurrentWriteDetected $e) { - // version must be wrong so nothing good if we retry it later on... - throw $e; - } catch (\Exception $e) { - // something unexpected occurred, so lets leave uow in state from just before it happened - we may like to retry it later... - array_unshift($this->uncommited, $producer); - - throw $e; + $producers[] = $producer; + $events = [...$events, ...$producer->events()]; + } + + try { + $this->store->add(...$events); + } catch (ConcurrentWriteDetected $e) { + // version must be wrong so nothing good if we retry it later on... + throw $e; + } catch (\Exception $e) { + // something unexpected occurred, so lets leave uow in state from just before it happened - we may like to retry it later... + array_unshift($this->uncommited, ...$producers); + throw $e; + } + + foreach ($producers as $producer) { + if ($producer instanceof Domain\Versionable) { + $producer->commit(); } + yield $producer; } $this->clear();