<?php
// models/Task.php
namespace app\models;
use yii\db\ActiveRecord;
class Task extends ActiveRecord
{
public static function tableName()
{
return 'tasks';
}
// Propriedades da entidade
public $id;
public $title;
public $description;
public $status;
}
// services/TaskDomainService.php
namespace app\services;
use app\models\Task;
class TaskDomainService
{
public function completeTask(Task $task)
{
$task->status = 'completed';
// Aqui você pode adicionar lógica adicional, como disparar eventos de domínio, verificar regras de negócios, etc.
}
}
// services/TaskAppService.php
namespace app\services;
use app\models\Task;
class TaskAppService
{
protected $taskDomainService;
public function __construct(TaskDomainService $taskDomainService)
{
$this->taskDomainService = $taskDomainService;
}
public function completeTask($taskId)
{
$task = Task::findOne($taskId);
if (!$task) {
throw new \InvalidArgumentException("Task with ID $taskId not found.");
}
$this->taskDomainService->completeTask($task);
$task->save();
}
}
// controllers/TaskController.php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\services\TaskAppService;
class TaskController extends Controller
{
protected $taskAppService;
public function __construct($id, $module, TaskAppService $taskAppService, $config = [])
{
$this->taskAppService = $taskAppService;
parent::__construct($id, $module, $config);
}
public function actionComplete($id)
{
try {
$this->taskAppService->completeTask($id);
Yii::$app->session->setFlash('success', 'Task completed successfully.');
} catch (\Exception $e) {
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['task/index']);
}
}