1. Go to this page and download the library: Download bartoszbartniczak/cqrs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bartoszbartniczak / cqrs example snippets
use BartoszBartniczak\CQRS\Command\Command;
use BartoszBartniczak\CQRS\Command\Handler\CommandHandler;
interface EmailSenderService
{
public function sendEmail(string $receiver, string $subject, string $body);
}
class FakeEmailSenderService implements EmailSenderService
{
public function sendEmail(string $receiver, string $subject, string $body)
{
// TODO: Here you should send email!
}
}
class SendEmailCommand implements Command
{
/**
* @var string
*/
private $receiver;
/**
* @var string
*/
private $subject;
/**
* @var string
*/
private $body;
/**
* @var EmailSenderService
*/
private $emailSenderService;
/**
* SendEmail constructor.
* @param string $receiver
* @param string $subject
* @param string $body
* @param EmailSenderService $emailSenderService
*/
public function __construct(string $receiver, string $subject, string $body, EmailSenderService $emailSenderService)
{
$this->receiver = $receiver;
$this->subject = $subject;
$this->body = $body;
$this->emailSenderService = $emailSenderService;
}
/**
* @return string
*/
public function getReceiver(): string
{
return $this->receiver;
}
/**
* @return string
*/
public function getSubject(): string
{
return $this->subject;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @return EmailSenderService
*/
public function getEmailSenderService(): EmailSenderService
{
return $this->emailSenderService;
}
}
class SendEmailHandler extends CommandHandler
{
public function handle(Command $command)
{
//TODO: Add some validation here
/* @var $command SendEmailCommand */
$command->getEmailSenderService()->sendEmail(
$command->getReceiver(),
$command->getSubject(),
$command->getBody()
);
}
}
$fakeEmailSenderService = new FakeEmailSenderService();
$sendEmailCommand = new SendEmailCommand('[email protected]', 'Very important message!', 'Here is the body of the message.', $fakeEmailSenderService);
$sendEmailHandler = new SendEmailHandler();
$sendEmailHandler->handle($sendEmailCommand);
use BartoszBartniczak\CQRS\Command\Bus\CommandBus;
class SimpleCommandBus extends CommandBus{
protected function handleHandlerException(CommandHandler $handler)
{
// TODO: Here you can react on HandlerException and then you should throw the CannotExecuteTheCommandException
}
protected function saveDataInRepository($data)
{
// TODO: Here you shoud persist the data
}
}
$simpleCommandBus = new SimpleCommandBus();
$simpleCommandBus->registerHandler(SendEmailCommand::class, $sendEmailHandler);
try{
$simpleCommandBus->execute($sendEmailCommand);
}catch(CannotExecuteTheCommandException $cannotExecuteTheCommandException){
// TODO: //Do some buisiness logic in here.
}
use BartoszBartniczak\CQRS\Command\Handler\CannotHandleTheCommandException;
use BartoszBartniczak\CQRS\Command\Query;
interface ProductRepository
{
/**
* @param ProductId $productId
* @return Product
* @throws CannotFindProductException
*/
public function findProductById(ProductId $productId);
}
class FindProductInRepositoryCommand implements Query
{
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var ProductId
*/
private $productId;
/**
* FindProductInRepositoryCommand constructor.
* @param ProductId $productId
* @param ProductRepository $productRepository
*/
public function __construct(ProductId $productId, ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
$this->productId = $productId;
}
/**
* @return ProductRepository
*/
public function getProductRepository(): ProductRepository
{
return $this->productRepository;
}
/**
* @return ProductId
*/
public function getProductId(): ProductId
{
return $this->productId;
}
}
class FindProductInRepositoryHandler extends CommandHandler
{
public function handle(Command $command): Product
{
/* @var $command FindProductInRepositoryCommand */
try {
$product = $command->getProductRepository()->findProductById(
$command->getProductId()
);
return $product;
} catch (CannotFindProductException $cannotFindProductException) {
//TODO: Do some buisiness logic in here. E.g. Save the wrong phrase/id for further computing.
throw new CannotHandleTheCommandException("Product cannot be found.", null, $cannotFindProductException);
}
}
}