PHP code example of carlosbuenosvinos / ddd

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

    

carlosbuenosvinos / ddd example snippets




namespace Acme\Inventory\Infrastructure\Messaging\Amqp;

use stdClass;
use AMQPQueue;
use JMS\Serializer\Serializer;
use League\Tactician\CommandBus;
use React\EventLoop\LoopInterface;
use Ddd\Infrastructure\Application\Notification\AmqpExchangeListener;

class OrderWasCreatedListener extends AmqpExchangeListener
{
    private $commandBus;
    
    public function __construct(AMQPQueue $queue, LoopInterface $loop, Serializer $serializer, CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
        
        parent::construct($queue, $loop, $serializer);
    }
    
    /**
     * This method will be responsible to decide whether this listener listens to an specific
     * event type or not, given an event type name
     *
     * @param string $typeName
     *
     * @return bool
     */
    protected function listensTo($typeName)
    {
        return 'Acme\Billing\DomainModel\Order\OrderWasCreated' === $typeName;
    }

    /**
     * The action to perform
     *
     * @param stdClass $event
     *
     * @return void
     */
    protected function handle($event)
    {
        $this->commandBus->handle(new CreateOrder(
            $event->order_id,
            // ...
        ));
    }
}



namespace AppBundle\Command;

use AMQPConnection;
use AMQPChannel;
use React\EventLoop\Factory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use JMS\Serializer\Serializer;
use League\Tactician\CommandBus;

class OrderWasCreatedWorkerCommand extends Command
{
    private $serializer;
    private $commandBus;
    
    public function __construct(Serializer $serializer, CommandBus $commandBus)
    {
        $this->serializer = $serializer;
        $this->commandBus = $commandBus;
        
        parent::__construct();
    }
    
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $connection = new AMQPConnection([
            'host' => 'example.host',
            'vhost' => '/',
            'port' => 5763,
            'login' => 'user',
            'password' => 'password'
        ]);
        $connection->connect();
        
        $queue = new AMQPQueue(new AMQPChannel($connection));
        $queue->setName('events');
        $queue->setFlags(AMQP_NOPARAM);
        $queue->declareQueue();
        
        $loop = Factory::create();
        
        $listener = new OrderWasCreatedListener(
            $queue,
            $loop,
            $serializer,
            $this->commandBus
        );
        
        $loop->run();
    }
}



use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Ddd\Infrastructure\Application\Notification\AmqpMessageProducer;
use Ddd\Application\Notification\NotificationService;

$connection = new AMQPConnection([
    'host' => 'example.host',
    'vhost' => '/',
    'port' => 5763,
    'login' => 'user',
    'password' => 'password'
]);
$connection->connect();

$exchange = new AMQPExchange(new AMQPChannel($connection));
$exchange->setName('events');
$exchange->declare();

$config = Setup::createYAMLMetadataConfiguration([__DIR__."/src/Infrastructure/Application/Persistence/Doctrine/Config"], false);
$entityManager = EntityManager::create(['driver' => 'pdo_sqlite', 'path' => __DIR__ . '/db.sqlite'], $config);

$eventStore = $entityManager->getRepository('Ddd\Domain\Event\StoredEvent');
$publishedMessageTracker = $entityManager->getRepository('Ddd\Domain\Event\PublishedMessage');
$messageProducer = new AmqpMessageProducer($exchange);

$notificationService = new NotificationService(
    $eventStore,
    $publishedMessageTracker,
    $messageProducer
);

$notificationService->publish(/** ... **/);