PHP code example of it-devgroup / laravel-command-bus

1. Go to this page and download the library: Download it-devgroup/laravel-command-bus 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/ */

    

it-devgroup / laravel-command-bus example snippets


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

ItDevgroup\CommandBus\CommandBusServiceProvider::class,

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

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

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

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