it-devgroup/laravel-command-bus

Simple Command Bus for Laravel framework

1.0 2018-01-23 12:23 UTC

This package is auto-updated.

Last update: 2024-10-22 21:49:56 UTC


README

Simple Command Bus for Laravel framework

Installation

Require this package in your composer.json and update composer.

"it-devgroup/laravel-command-bus ": "^1.0"

After updating composer, add the ServiceProvider to the providers array in config/app.php

ItDevgroup\CommandBus\CommandBusServiceProvider::class,

Example

class UserController extends AbstractController
{
    public function store(Request $request)
    {
        $user = $this->dispatch(new RegisterUser(
            trim($request->input('email')),
            trim($request->input('password'))
        ));
    
        return $user;
    }
}

Usage

Command

class RegisterUser implements \ItDevgroup\CommandBus\Command
{
    private $email;
    private $password;
    
    public function __construct($email, $password)
    {
        $this->email = $email;
        $this->password = $password;
    }
    
    public function email()
    {
        return $this->email;
    }
    
    public function password()
    {
        return $this->password;
    }
}

Handler

class RegisterUserHandler implements \ItDevgroup\CommandBus\Handler
{
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function handle(\ItDevgroup\CommandBus\Command $command)
    {
        $user = new User(
            $command->email(),
            $command->password()
        );
        
        $this->userRepository->store($user);
        
        return $user;
    }
}

Controllers

class AbsctractController extends \Illuminate\Routing\Controller
{
    private $dispatcher;
    
    public function __construct(\ItDevgroup\CommandBus\CommandBus $dispatcher) 
    {
        $this->dispatcher = $dispatcher;
    }
    
    public function dispatch(\ItDevgroup\CommandBus\Command $command)
    {
        return $this->dispatcher->execute($command);
    }
}
class UserController extends AbstractController
{
    public function store(Request $request)
    {
        $user = $this->dispatch(new RegisterUser(
            trim($request->input('email')),
            trim($request->input('password'))
        ));
    
        return $user;
    }
}