Skip to content

Commit

Permalink
Bind the period to the callable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
timothepearce committed Jan 21, 2022
1 parent 35063f8 commit 37d74aa
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Projector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function createOrUpdateGlobalPeriod(): void

is_null($projection) ?
$this->createGlobalProjection() :
$this->updateProjection($projection);
$this->updateProjection($projection, '*');
}

/**
Expand All @@ -79,7 +79,7 @@ private function parsePeriod(string $period): void

is_null($projection) ?
$this->createProjection($period) :
$this->updateProjection($projection);
$this->updateProjection($projection, $period);
}

/**
Expand Down Expand Up @@ -118,7 +118,7 @@ private function createProjection(string $period): void
'key' => $this->hasKey() ? $this->key() : null,
'period' => $period,
'start_date' => app(Quasar::class)->resolveFloorDate($this->projectedModel->created_at, $period),
'content' => $this->mergeProjectedContent((new $this->projectionName())->defaultContent()),
'content' => $this->mergeProjectedContent((new $this->projectionName())->defaultContent(), $period),
]);
}

Expand All @@ -132,16 +132,16 @@ private function createGlobalProjection()
'key' => $this->hasKey() ? $this->key() : null,
'period' => '*',
'start_date' => null,
'content' => $this->mergeProjectedContent((new $this->projectionName())->defaultContent()),
'content' => $this->mergeProjectedContent((new $this->projectionName())->defaultContent(), '*'),
]);
}

/**
* Updates the projection.
*/
private function updateProjection(Projection $projection): void
private function updateProjection(Projection $projection, string $period): void
{
$projection->content = $this->mergeProjectedContent($projection->content);
$projection->content = $this->mergeProjectedContent($projection->content, $period);

$projection->save();
}
Expand All @@ -157,9 +157,9 @@ private function hasKey(): bool
/**
* Merges the projected content with the given one.
*/
private function mergeProjectedContent(array $content): array
private function mergeProjectedContent(array $content, string $period): array
{
return array_merge($content, $this->resolveCallableMethod($content));
return array_merge($content, $this->resolveCallableMethod($content, $period));
}

/**
Expand All @@ -177,15 +177,15 @@ private function hasCallableMethod(): bool
/**
* Resolves the callable method.
*/
private function resolveCallableMethod(array $content): array
private function resolveCallableMethod(array $content, string $period): array
{
$modelName = Str::of($this->projectedModel::class)->explode('\\')->last();
$callableMethod = lcfirst($modelName) . ucfirst($this->eventName);
$defaultCallable = 'projectable' . ucfirst($this->eventName);

return method_exists($this->projectionName, $callableMethod) ?
(new $this->projectionName())->$callableMethod($content, $this->projectedModel) :
(new $this->projectionName())->$defaultCallable($content, $this->projectedModel);
(new $this->projectionName())->$callableMethod($content, $this->projectedModel, $period) :
(new $this->projectionName())->$defaultCallable($content, $this->projectedModel, $period);
}

/**
Expand Down

0 comments on commit 37d74aa

Please sign in to comment.