我在laminas MVC
中创建了一个与Google Calendar
交互的Calendar module
,然后创建了另一个Todo module
,它应该与我的Calendar module
交互。CalendarController
在Calendar module
中的签名类似
public function __construct(
ListProcess $listProcess,
AddProcess $addProcess,
EditProcess $editProcess,
DeleteProcess $deleteProcess
)
现在,我在Todo module
中的代码应该启动调度过程,如下所示
public function execute(): array
{
$todo = json_decode((new CrmApiService())->getTodo($this->getTodoId()), true);
$eventData["summary"] = $todo->title;
$eventData["description"] = $todo->content;
$eventData["startDateTime"] = $todo->nextActionDate;
$eventData["endDateTime"] = $todo->nextActionDate;
$calendar = new CalendarController();
return $calendar->scheduleFromAnotherSource($eventData);
}
当我执行这个时,我会得到一个错误,如下
Too few arguments to function CalendarModuleControllerCalendarController::__construct(), 0 passed in D:laminastodo-module-integratedvendoriss-moduletodo-modulesrcProcessTodoScheduleProcess.php on line 53 and exactly 4 expected
我知道我不应该直接调用CalendarController
,而是应该通过Service
。
我的问题是,我应该如何在Todo module
中创建一个依赖于Calendar module
的Service
,并且它应该与Calendar module
交互,而不需要有进一步依赖关系的CalendarController
的参与?
感谢您的帮助。
以下是我实现它的方法。(希望它能帮助到别人(
在我的Calendar-module
中,添加逻辑与CalendarController
及其所谓的AddProcess
是分开的,这就是我从控制器添加事件的方式。$result = $this->addProcess->execute($this->params()->fromPost());
。Google身份验证是通过一个单独的服务CalendarClientService
来处理的。我的所有进程都访问此服务以进行如下身份验证,然后执行。
$client = $this->calendarClientService->getClient();
if (!$this->calendarClientService->authenticateClient($client)) {
return ["error" => "authentication", "url" => filter_var($client->createAuthUrl(), FILTER_SANITIZE_URL)];
}
现在,我在Calendar-module
中创建了一个新服务,如下所示,我刚刚调用了AddProcess
并将其传递给新的eventData
。
class CalendarService
{
protected AddProcess $addProcess;
public function __construct(AddProcess $addProcess)
{
$this->addProcess = $addProcess;
}
public function scheduleAsEvent($eventData)
{
$eventData["startDateTime"] = Carbon::parse($eventData["startDateTime"])->format("Y-m-dTH:i");
$eventData["endDateTime"] = Carbon::parse($eventData["endDateTime"])->format("Y-m-dTH:i");
return $this->addProcess->execute($eventData);
}
}
然后从我的Todo-module
,我访问以下中的此服务
namespace TodoModuleProcess;
use CarbonCarbon;
use GoogleException;
use LaminasCacheExceptionExceptionInterface;
use LaminasMvcControllerAbstractActionController;
use CalendarModuleServiceCalendarService;
use TodoModuleServiceCrmApiService;
class TodoScheduleProcess extends AbstractActionController
{
protected int $todoID;
protected CalendarService $calendarService;
public function __construct(CalendarService $calendarService)
{
$this->calendarService = $calendarService;
}
public function execute(): array
{
$todo = json_decode((new CrmApiService())->getTodo($this->getTodoId()));
$eventData["summary"] = $todo->title;
$eventData["description"] = $todo->content;
$eventData["startDateTime"] = $todo->nextActionDate;
$eventData["endDateTime"] = $todo->nextActionDate;
return $this->calendarService->scheduleAsEvent($eventData);
}
public function getTodoId()
{
return $this->todoID;
}
public function setTodoId($id)
{
$this->todoID = $id;
}
}```