PHP code example of mistery23 / laravel-aggregate-events
1. Go to this page and download the library: Download mistery23/laravel-aggregate-events 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/ */
mistery23 / laravel-aggregate-events example snippets
use Mistery23\AggregateEvents\EventTrait;
class User extends Model implements AggregateEventRoot
{
use EventTrait;
public static function signUp(
Id $id,
Name $name,
...
): self {
$user = new self();
$user->id = $id;
$user->name = $name;
...
$user->recordEvent(new UserSignupedEvent($user));
return $user;
}
}
use Mistery23\AggregateEvents\EventDispatcher;
class UserRepository
{
private $dispatcher;
/**
* UserRepository constructor.
*
* @param EventDispatcher $dispatcher
*/
public function __construct(EventDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* Add user
*
* @param Model $user
*
* @return void
*
* @throws \RuntimeException
*/
public function add(Model $user): void
{
if (false === $user->save()) {
throw new \RuntimeException('Save error.');
}
$this->dispatcher->dispatchAll($user->releaseEvents());
}
}