objectAction = ObjectAction::tryFrom($this->action->action); } public function send() { if (! ConfigRepository::openEmailNotification()) { return; } ConfigRepository::emailDynamicSetting(); $actionObjectType = ActionObjectType::tryFrom($this->action->object_type); $actionObjectModel = $actionObjectType->modelBuilder()->find($this->action->object_id); match ($actionObjectType) { ActionObjectType::REQUIREMENT => $this->requirement($actionObjectModel), ActionObjectType::TASK => $this->task($actionObjectModel), }; } protected function requirement(Requirement $requirement) { $this->dispatch($requirement->mailto, new RequirementAction($requirement, $this->objectAction)); } protected function task(Task $task) { $userIds = array_filter([$task->assign, ...$task->mailto]); $this->dispatch($userIds, new TaskAction($task, $this->objectAction)); } protected function dispatch(array $userIds, Mailable $mailable) { $users = User::query()->whereIn("id", $userIds)->get(); if ($users->isEmpty()) { return; } foreach ($users as $user) { Mail::to($user)->send($mailable); } } }