Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move subscribers to outside event bus (making it subscription itself) and others #148

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Infrastructure/Application/Listener/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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
{

}
}
43 changes: 43 additions & 0 deletions src/Infrastructure/Application/Listener/Subscriber/Id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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();
}
}
37 changes: 21 additions & 16 deletions src/Infrastructure/Domain/UnitOfWork/EventStoreUnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down