Skip to content

Commit ec8b37d

Browse files
Fix some problems with state factory hooks (#75)
* Add afterMaking and afterCreating hooks to StateFactories * Fix hooks * Fix styling --------- Co-authored-by: DanielCoulbourne <DanielCoulbourne@users.noreply.github.com>
1 parent 854c01e commit ec8b37d

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/StateFactory.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public static function new(string $state_class, array $data = []): static
3131
{
3232
$factory = new static($state_class);
3333

34-
$factory->configure();
35-
3634
return $data ? $factory->state($data) : $factory;
3735
}
3836

37+
protected string $initial_event = VerbsStateInitialized::class;
38+
3939
/** @param class-string<TStateType> $state_class */
4040
public function __construct(
4141
protected string $state_class,
@@ -44,7 +44,6 @@ public function __construct(
4444
protected int|string|null $id = null,
4545
protected bool $singleton = false,
4646
protected ?Generator $faker = null,
47-
protected ?string $initial_event = null,
4847
protected Collection $makeCallbacks = new Collection(),
4948
protected Collection $createCallbacks = new Collection(),
5049
) {
@@ -137,23 +136,29 @@ public function create(array $data = [], Bits|UuidInterface|AbstractUid|int|stri
137136
/** @return TStateType */
138137
protected function createState(): State
139138
{
140-
$this->makeCallbacks->each(fn (Closure $callback) => $callback($this));
139+
$this->makeCallbacks->each(
140+
fn (Closure $callback) => $callback->bindTo($this)()
141+
);
141142

142-
$initialized = $this->initial_event
143-
? $this->initial_event::fire(
144-
...$this->getRawData(),
145-
id: $this->id ?? Id::make(),
146-
)
147-
: VerbsStateInitialized::fire(
143+
$this->configure();
144+
145+
$initialized = $this->initial_event === VerbsStateInitialized::class
146+
? VerbsStateInitialized::fire(
148147
state_id: $this->id ?? Id::make(),
149148
state_class: $this->state_class,
150149
state_data: $this->getRawData(),
151150
singleton: $this->singleton,
151+
)
152+
: $this->initial_event::fire(
153+
...$this->getRawData(),
154+
id: $this->id ?? Id::make(),
152155
);
153156

154-
$this->createCallbacks->each(fn (Closure $callback) => $callback($initialized));
157+
$state = $initialized->state($this->state_class);
158+
159+
$this->createCallbacks->each(fn (Closure $callback) => $callback($state));
155160

156-
return $initialized->state($this->state_class);
161+
return $state;
157162
}
158163

159164
protected function getRawData(): array
@@ -170,14 +175,18 @@ protected function getRawData(): array
170175
/** @return static<TStateType> */
171176
protected function clone(array $with = []): static
172177
{
173-
return new static(
178+
$state = new static(
174179
state_class: $with['state_class'] ?? $this->state_class,
175180
transformations: $with['transformations'] ?? $this->transformations,
176181
count: $with['count'] ?? $this->count,
177182
id: $with['id'] ?? $this->id,
178183
singleton: $with['singleton'] ?? $this->singleton,
179184
faker: $with['faker'] ?? $this->faker,
185+
makeCallbacks: $with['makeCallbacks'] ?? $this->makeCallbacks,
186+
createCallbacks: $with['createCallbacks'] ?? $this->createCallbacks,
180187
);
188+
189+
return $state;
181190
}
182191

183192
protected function faker(): Generator

0 commit comments

Comments
 (0)