PHP code example of bystro / domain-events-publisher

1. Go to this page and download the library: Download bystro/domain-events-publisher 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/ */

    

bystro / domain-events-publisher example snippets




use Bystro\DomainEventPublisher\Domain\DomainEventPublisher;
use Bystro\DomainEventPublisher\Infrastructure\MessageProducerDomainEventSubscriber;
use Bystro\DomainEventPublisher\Infrastructure\RabbitMqMessageProducer;
use PhpAmqpLib\Connection\AMQPStreamConnection;

$messageProducer = new RabbitMqMessageProducer(
        new AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest')
);
$messageProducer->open('example-exchange-name');

DomainEventPublisher::instance()->subscribe(
        new MessageProducerDomainEventSubscriber(
               $messageProducer 
        )
);



namespace yourApp/namespace;

use Bystro\DomainEventPublisher\Domain\DomainEvent;

class FileSavedEvent implements DomainEvent
{
    private /* string */ $filename;
    private /* string */ $destinationPath;    
    private /* \DateTimeImmutable */ $occurredOn;

    public function __construct(string $filename, string $destinationPath)
    {
        $this->filename = $filename;
        $this->destinationPath = $destinationPath;
        $this->occurredOn = new \DateTimeImmutable();
    }

    public function filename(): string
    {
        return $this->filename;
    }
    
    public function destinationPath(): string
    {
        return $this->destinationPath;
    }

    public function occurredOn(): \DateTimeImmutable
    {
        return $this->occurredOn;
    }
}



namespace yourApp/namespace;

use Bystro\DomainEventPublisher\Domain\DomainEventPublisher;

class File
{

    private /* string */ $filename;
    private /* string */ $contents;

    public function __construct(string $filename, string $contents)
    {
        $this->filename = $filename;
        $this->contents = $contents;
    }
    
    public function save(string $destinationPath): void
    {        
        file_put_contents($destinationPath . $this->filename, $this->contents);
        
        DomainEventPublisher::instance()->publish(
            new FileSavedEvent($this->name, $destinationPath)
        );        
    }

}