diff --git a/src/Projector.php b/src/Projector.php index 7851693..1e9211c 100644 --- a/src/Projector.php +++ b/src/Projector.php @@ -67,7 +67,7 @@ private function createOrUpdateGlobalPeriod(): void is_null($projection) ? $this->createGlobalProjection() : - $this->updateProjection($projection); + $this->updateProjection($projection, '*'); } /** @@ -79,7 +79,7 @@ private function parsePeriod(string $period): void is_null($projection) ? $this->createProjection($period) : - $this->updateProjection($projection); + $this->updateProjection($projection, $period); } /** @@ -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), ]); } @@ -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(); } @@ -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)); } /** @@ -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); } /**