2017 © Pedro Peláez
 

library domain

image

emerido/domain

  • Wednesday, June 28, 2017
  • by emerido
  • Repository
  • 1 Watchers
  • 3 Stars
  • 343 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 33 % Grown

The README.md

Domain

PHP library for managing domain logic, (*1)

Why?

  • Why CommandBus and QueryBus throws exception if handler for message not found?, (*2)

    • If you execute message and handler not found it's not good. But you can handle this case in your middlewares

CommandBus Example

Create CommandBus, (*3)

<?php require_once __DIR__ . '/../vendor/autoload.php';

$commandBus = \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()
;

Create your first command message, (*4)

<?php

class CreateTodo
{

    protected $subject;

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

    public function setSubject(string $subject)
    {
        $this->subject = $subject;    
    }

}

Additionally you can implement some interfaces in messages and handle this contracts in custom middlewares., (*5)

For example in my case i have: - StoryInterface for mark some messages like one history line - PersistentInterface for automatically save message with payload in event store, (*6)

Example of command handler, (*7)

<?php

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;
    }

}

Create command handler provider and register previously created handler for Todo commands, (*8)

<?php


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);    
    }

}

The Versions