Skip to content

Commit

Permalink
Merge pull request #33 from hirethunk/remove-phase
Browse files Browse the repository at this point in the history
Remove phases from events
  • Loading branch information
inxilpro authored Dec 6, 2023
2 parents 755e301 + d360895 commit 011b881
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 43 deletions.
7 changes: 2 additions & 5 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use ReflectionMethod;
use ReflectionParameter;
use Thunk\Verbs\Exceptions\EventNotValidForCurrentState;
use Thunk\Verbs\Lifecycle\Phase;
use Thunk\Verbs\Support\EventSerializer;
use Thunk\Verbs\Support\EventStateRegistry;
use Thunk\Verbs\Support\PendingEvent;
Expand All @@ -20,8 +19,6 @@ abstract class Event
{
public int $id;

public Phase $phase;

/** @return PendingEvent<static> */
public static function make(...$args): PendingEvent
{
Expand All @@ -44,7 +41,7 @@ public static function make(...$args): PendingEvent

$event = app(EventSerializer::class)->deserialize(static::class, $args);

$event->id = Snowflake::make()->id();
$event->id ??= Snowflake::make()->id();

return PendingEvent::make($event);
}
Expand All @@ -69,7 +66,7 @@ public function states(): StateCollection
* @param class-string<T>|null $state_type
* @return T|null
*/
public function state(string $state_type = null): ?State
public function state(?string $state_type = null): ?State
{
$states = $this->states();

Expand Down
3 changes: 0 additions & 3 deletions src/Lifecycle/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public function fire(Event $event): Event

$states->each(fn ($state) => Guards::for($event, $state)->check());

$event->phase = Phase::Apply;
$states->each(fn ($state) => $this->dispatcher->apply($event, $state));

$event->phase = Phase::Fired;
$this->dispatcher->fired($event, $states);

app(Queue::class)->queue($event);
Expand All @@ -51,7 +49,6 @@ public function commit(): bool
}

foreach ($events as $event) {
$event->phase = Phase::Handle;
$this->dispatcher->handle($event);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Lifecycle/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
class EventStore
{
public function read(
State $state = null,
Bits|UuidInterface|AbstractUid|int|string $after_id = null,
Bits|UuidInterface|AbstractUid|int|string $up_to_id = null
?State $state = null,
Bits|UuidInterface|AbstractUid|int|string|null $after_id = null,
Bits|UuidInterface|AbstractUid|int|string|null $up_to_id = null
): LazyCollection {
if ($state) {
return VerbStateEvent::query()
Expand Down
6 changes: 1 addition & 5 deletions src/Lifecycle/Guards.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Guards
{
public static function for(Event $event, State $state = null): static
public static function for(Event $event, ?State $state = null): static
{
return new static($event, $state);
}
Expand All @@ -29,8 +29,6 @@ public function check(): static

public function authorize(): static
{
$this->event->phase = Phase::Authorize;

if ($this->passesAuthorization()) {
return $this;
}
Expand All @@ -44,8 +42,6 @@ public function authorize(): static

public function validate(): static
{
$this->event->phase = Phase::Validate;

$exception = new EventNotValidForCurrentState();

try {
Expand Down
6 changes: 3 additions & 3 deletions src/Lifecycle/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,21 @@ public function fired(Container $container, Event $event, StateCollection $state
}
}

public function handle(Container $container, Event $event, State $state = null): void
public function handle(Container $container, Event $event, ?State $state = null): void
{
if ($this->runsInPhase(Phase::Handle)) {
$container->call($this->callback, $this->guessParameters($event, $state));
}
}

public function replay(Container $container, Event $event, State $state = null): void
public function replay(Container $container, Event $event, ?State $state = null): void
{
if ($this->runsInPhase(Phase::Replay)) {
$container->call($this->callback, $this->guessParameters($event, $state));
}
}

protected function guessParameters(Event $event, State $state = null, StateCollection $states = null): array
protected function guessParameters(Event $event, ?State $state = null, ?StateCollection $states = null): array
{
$parameters = [
'e' => $event,
Expand Down
2 changes: 0 additions & 2 deletions src/Models/VerbEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Thunk\Verbs\Event;
use Thunk\Verbs\Lifecycle\Phase;
use Thunk\Verbs\State;
use Thunk\Verbs\Support\EventSerializer;

Expand All @@ -24,7 +23,6 @@ class VerbEvent extends Model
public function event(): Event
{
$this->event ??= app(EventSerializer::class)->deserialize($this->type, $this->data);
$this->event->phase = Phase::Replay;

return $this->event;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Normalization/BitsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

class BitsNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, ?string $format = null): bool
{
return is_a($type, Bits::class, true);
}

/** @param class-string<Bits> $type */
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): Bits
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Bits
{
return $type::coerce($data);
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, ?string $format = null): bool
{
return $data instanceof Bits;
}

public function normalize(mixed $object, string $format = null, array $context = []): string
public function normalize(mixed $object, ?string $format = null, array $context = []): string
{
if (! $object instanceof Bits) {
throw new InvalidArgumentException(class_basename($this).' can only normalize Bits objects.');
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Normalization/CarbonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

class CarbonNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, ?string $format = null): bool
{
return is_a($type, CarbonInterface::class, true);
}

/** @param class-string<CarbonInterface> $type */
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): CarbonInterface
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): CarbonInterface
{
return $type === CarbonInterface::class
? Date::parse($data)
: $type::parse($data);
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, ?string $format = null): bool
{
return $data instanceof CarbonInterface;
}

public function normalize(mixed $object, string $format = null, array $context = []): string
public function normalize(mixed $object, ?string $format = null, array $context = []): string
{
if (! $object instanceof CarbonInterface) {
throw new InvalidArgumentException(class_basename($this).' can only normalize Carbon objects.');
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Normalization/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class CollectionNormalizer implements DenormalizerInterface, NormalizerInterface
{
use AcceptsNormalizerAndDenormalizer;

public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, ?string $format = null): bool
{
return is_a($type, Collection::class, true);
}

/** @param class-string<Collection> $type */
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): Collection
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Collection
{
$fqcn = data_get($data, 'fqcn', Collection::class);
$items = data_get($data, 'items', []);
Expand All @@ -36,12 +36,12 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
return $fqcn::make($items)->map(fn ($value) => $this->serializer->denormalize($value, $subtype, $format, $context));
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, ?string $format = null): bool
{
return $data instanceof Collection;
}

public function normalize(mixed $object, string $format = null, array $context = []): array
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
if (! $object instanceof Collection) {
throw new InvalidArgumentException(class_basename($this).' can only normalize Collection objects.');
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Normalization/SelfSerializingNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class SelfSerializingNormalizer implements DenormalizerInterface, NormalizerInte
{
use AcceptsNormalizerAndDenormalizer;

public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, ?string $format = null): bool
{
return is_a($type, SerializedByVerbs::class, true);
}

/** @param class-string<SerializedByVerbs> $type */
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): SerializedByVerbs
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): SerializedByVerbs
{
if (is_string($data)) {
$data = json_decode($data, true);
Expand All @@ -27,12 +27,12 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
return $type::deserializeForVerbs($data, $this->serializer);
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, ?string $format = null): bool
{
return $data instanceof SerializedByVerbs;
}

public function normalize(mixed $object, string $format = null, array $context = []): array|string
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string
{
if (! $object instanceof SerializedByVerbs) {
throw new InvalidArgumentException(class_basename($this).' can only normalize classes that implement SerializedByVerbs.');
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Normalization/StateNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

class StateNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, ?string $format = null): bool
{
return is_a($type, State::class, true) && is_numeric($data);
}

/** @param class-string<State> $type */
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): State
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): State
{
return app(StateManager::class)->load((int) $data, $type);
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, ?string $format = null): bool
{
return $data instanceof State;
}

public function normalize(mixed $object, string $format = null, array $context = []): string
public function normalize(mixed $object, ?string $format = null, array $context = []): string
{
if (! $object instanceof State) {
throw new InvalidArgumentException(class_basename($this).' can only normalize State objects.');
Expand Down
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
->values()
->all();

expect()->extend('toThrow', function (string|Throwable $expected, string $message = null) {
expect()->extend('toThrow', function (string|Throwable $expected, ?string $message = null) {
if ($expected instanceof Throwable) {
$message = $expected->getMessage();
$expected = $expected::class;
Expand All @@ -38,7 +38,7 @@
return false;
});

expect()->extend('toBeMoney', function (Money|string|int $amount = null, string $currency = null) {
expect()->extend('toBeMoney', function (Money|string|int|null $amount = null, ?string $currency = null) {
$this->toBeInstanceOf(Money::class);

if (isset($amount, $currency)) {
Expand Down

0 comments on commit 011b881

Please sign in to comment.