Created
February 5, 2023 23:10
-
-
Save IgorDePaula/c13b382954d5f1f4843340119a8645ea to your computer and use it in GitHub Desktop.
CQRS pattern with Specification pattern in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Command | |
{ | |
public function execute(); | |
} | |
class TaskList | |
{ | |
private $tasks = []; | |
public function add(Task $task) | |
{ | |
$this->tasks[] = $task; | |
} | |
public function getTasks() | |
{ | |
return $this->tasks; | |
} | |
} | |
interface Query | |
{ | |
public function execute(); | |
} | |
class CreateTaskCommand implements Command | |
{ | |
private $taskName; | |
private $taskList; | |
public function __construct(Task $task, TaskList $taskList) | |
{ | |
$this->taskName = $task; | |
$this->taskList = $taskList; | |
} | |
public function execute() | |
{ | |
$this->taskList->add($this->taskName); | |
} | |
} | |
class GetTasksQuery implements Query | |
{ | |
private $specification; | |
private $taskList; | |
public function __construct(TaskSpecification $specification, TaskList $taskList) | |
{ | |
$this->specification = $specification; | |
$this->taskList = $taskList; | |
} | |
public function execute() | |
{ | |
// Get all tasks from the database that meet the specified criteria | |
$tasks = $this->taskList->getTasks(); | |
$true = []; | |
foreach($tasks as $task){ | |
if($this->specification->isSatisfiedBy($task)){ | |
$true[] = $task; | |
} | |
} | |
// ... | |
return $true; | |
} | |
} | |
interface TaskSpecification | |
{ | |
public function isSatisfiedBy(Task $task): bool; | |
} | |
class CompletedTaskSpecification implements TaskSpecification | |
{ | |
public function isSatisfiedBy(Task $task): bool | |
{ | |
return $task->isCompleted(); | |
} | |
} | |
class Task | |
{ | |
public $name; | |
public $isCompleted; | |
public function __construct(string $name, bool $isCompleted) | |
{ | |
$this->name = $name; | |
$this->isCompleted = $isCompleted; | |
} | |
public function isCompleted(): bool | |
{ | |
return $this->isCompleted; | |
} | |
} | |
class CommandBus | |
{ | |
public function handle(Command $command) | |
{ | |
$command->execute(); | |
} | |
} | |
class QueryBus | |
{ | |
public function handle(Query $query) | |
{ | |
return $query->execute(); | |
} | |
} | |
$commandBus = new CommandBus(); | |
$queryBus = new QueryBus(); | |
$taskList = new TaskList(); | |
$task1= new Task('task 1', false); | |
$task2= new Task('task 2', true); | |
$command = new CreateTaskCommand($task1, $taskList); | |
$commandBus->handle($command); | |
$command = new CreateTaskCommand($task2, $taskList); | |
$commandBus->handle($command); | |
// Get all completed tasks | |
$specification = new CompletedTaskSpecification(); | |
$query = new GetTasksQuery($specification, $taskList); | |
$tasks = $queryBus->handle($query); | |
var_dump($tasks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment