PHP code example of php-commons / cqrs-bus

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

    

php-commons / cqrs-bus example snippets



declare(strict_types=1);

namespace App\Controller;

use PhpCommons\CQRSBus\Bus\Command\CommandBus;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
    /**
     * @var CommandBus
     */
    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    /**
     * @Route("/", name="homepage")
     */
    public function index()
    {
        $this->commandBus->handle(new TestCommand('test_message'));
    }
}


declare(strict_types=1);

namespace App\Command;

use PhpCommons\CQRSBus\Application\Command\CommandInterface;

class TestCommand implements CommandInterface
{
    /**
     * @var string
     */
    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    /**
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }
}

 
declare(strict_types=1);
namespace App\Command;

use App\Repositories\UserRepository;
use PhpCommons\CQRSBus\Application\Command\CommandInterface;
use PhpCommons\CQRSBus\Handlers\Command\AbstractCommandHandler;

class TestCommandHandler extends AbstractCommandHandler
{
    /**
     * @var UserRepository
     */
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    /**
     * @param CommandInterface|TestCommand $command
     */
    public function handle(CommandInterface $command): void
    {
        $this->userRepository->save($command->getMessage());
    }
}


declare(strict_types=1);

namespace App\Controller;

use App\Query\TestQuery;
use PhpCommons\CQRSBus\QueryBus\QueryBus;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/tests")
 */
class UsersController extends AbstractController
{
    /**
     * @var QueryBus
     */
    private $queryBus;

    public function __construct(QueryBus $queryBus)
    {
        $this->queryBus = $queryBus;
    }

    /**
     * @Route("/{id}", name="get_test")
     */
    public function getTest(int $id)
    {
        // GetUserByIdQuery(1)
        $testQueryResult = $this->queryBus->handle(new TestQuery(1));
    }
}


declare(strict_types=1);

namespace App\Query;

use PhpCommons\CQRSBus\QueryBus\AbstractQuery;

class TestQuery extends AbstractQuery
{
    /**
     * @var int
     */
    private $id;

    public function __construct(int $id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }
}


declare(strict_types=1);

namespace App\Query;

use PhpCommons\CQRSBus\Application\Query\QueryInterface;
use PhpCommons\CQRSBus\Handlers\Query\AbstractQueryHandler;

class TestQueryHandler extends AbstractQueryHandler
{
    /**
     * @var TestRepository
     */
    private $testRepository;

    public function __construct(TestRepository $testRepository)
    {
        $this->testRepository = $testRepository;
    }

    /**
     * @param QueryInterface|TestQuery $query
     */
    public function handle(QueryInterface $query): object
    {
        return $this->testRepository->findById($query->getId());
    }
}
bash
composer