-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce EventStoreFake * Extract broker interface * Broker fake * Switch to StoresEvents interface in more places * Remove some convenience methods from interface * A little reorganization * Move `realNow` to BrokerConvenienceMethods * Update order to match how they're called
- Loading branch information
Showing
17 changed files
with
594 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Contracts; | ||
|
||
use Thunk\Verbs\Event; | ||
|
||
interface BrokersEvents | ||
{ | ||
public function fire(Event $event): ?Event; | ||
|
||
public function commit(): bool; | ||
|
||
public function isValid(Event $event): bool; | ||
|
||
public function isAllowed(Event $event): bool; | ||
|
||
public function replay(?callable $beforeEach = null, ?callable $afterEach = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Contracts; | ||
|
||
use Glhd\Bits\Bits; | ||
use Illuminate\Support\LazyCollection; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Symfony\Component\Uid\AbstractUid; | ||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\State; | ||
|
||
interface StoresEvents | ||
{ | ||
public function read( | ||
?State $state = null, | ||
Bits|UuidInterface|AbstractUid|int|string|null $after_id = null, | ||
Bits|UuidInterface|AbstractUid|int|string|null $up_to_id = null, | ||
bool $singleton = false | ||
): LazyCollection; | ||
|
||
/** @param Event[] $events */ | ||
public function write(array $events): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Thunk\Verbs\Lifecycle; | ||
|
||
use Carbon\CarbonInterface; | ||
use Glhd\Bits\Bits; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Symfony\Component\Uid\AbstractUid; | ||
use Throwable; | ||
use Thunk\Verbs\Event; | ||
use Thunk\Verbs\Exceptions\EventNotValidForCurrentState; | ||
use Thunk\Verbs\Support\Wormhole; | ||
|
||
trait BrokerConvenienceMethods | ||
{ | ||
public bool $is_replaying = false; | ||
|
||
public function toId(Bits|UuidInterface|AbstractUid|int|string|null $id): int|string|null | ||
{ | ||
return match (true) { | ||
$id instanceof Bits => $id->id(), | ||
$id instanceof UuidInterface => $id->toString(), | ||
$id instanceof AbstractUid => (string) $id, | ||
default => $id, | ||
}; | ||
} | ||
|
||
public function createMetadataUsing(?callable $callback = null): void | ||
{ | ||
app(MetadataManager::class)->createMetadataUsing($callback); | ||
} | ||
|
||
public function isAllowed(Event $event): bool | ||
{ | ||
try { | ||
$states = $event->states(); | ||
|
||
Guards::for($event, null)->authorize(); | ||
$states->each(fn ($state) => Guards::for($event, $state)->authorize()); | ||
|
||
return true; | ||
} catch (Throwable $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function isValid(Event $event): bool | ||
{ | ||
try { | ||
$states = $event->states(); | ||
|
||
Guards::for($event, null)->validate(); | ||
$states->each(fn ($state) => Guards::for($event, $state)->validate()); | ||
|
||
return true; | ||
} catch (EventNotValidForCurrentState $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function isReplaying(): bool | ||
{ | ||
return $this->is_replaying; | ||
} | ||
|
||
public function unlessReplaying(callable $callback) | ||
{ | ||
if (! $this->is_replaying) { | ||
$callback(); | ||
} | ||
} | ||
|
||
public function realNow(): CarbonInterface | ||
{ | ||
return app(Wormhole::class)->realNow(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.