PHP code example of emerido / domain

1. Go to this page and download the library: Download emerido/domain 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/ */

    

emerido / domain example snippets


 mandBus = \Domain\Bus\CommandBusBuilder::make()
    // Let's define method name resolver.
    // In this case CreateTodo command will handled with handleCreateTodo() method 
    ->usingHandlerMethodResolver(new \Domain\Handler\Method\NamedMethodResolver())
    
    // Let's define handler class resolver (see below)
    // In this case we use PHP-DI with autowiring for creating our handler
    ->usingHandlerResolver(new \Domain\Handler\ContainerResolver($container))
    
    // Register command handlers via providers (see below)
    ->register(new TodoCommandsHandlerProvider())
    
    // Finally build our CommandBus
    ->build()
;




class CreateTodo
{

    protected $subject;
    
    public function getSubject() : string 
    {
        return $this->subject;
    }
    
    public function setSubject(string $subject)
    {
        $this->subject = $subject;    
    }
    
}




class TodoCommandsHandler
{
    
    private $table;
    
    private $eventBus;
    
    public function __construct(TodoGateway $table, EventBusInterface $eventBus) 
    {
        $this->table = $table;
        $this->eventBus = $eventBus;
    }
    
    public function handleCreateTodo(CreateTodo $command)
    {
        $todo = new Todo();
        $todo->setSubject($command->getSubject());
        $todo->setCompleted(false);
        
        try {
            // Save todo
            $this->table->create($todo);
            
            // Emit successful event
            $this->eventBus->emit(new TodoCreated($todo));
            
        } catch (DatabaseException $exception) {
            // TODO : Write error log
            throw $exception;
        }
        
        // Yes, you can return newly created todo
        return $todo;
    }
    
}




class TodoCommandsHandlerProvider implements \Domain\Handler\HandlersProvider
{
    
    public function register(\Domain\Handler\HandlersMap $handlers)
    {
        // Tell handler class name which contains method to handle this command
        $handlers->handle(CreateTodo::class, TodoCommandsHandler::class);    
    }
    
}