-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Lelaisant
committed
Nov 7, 2022
1 parent
95ccd9c
commit 15db081
Showing
19 changed files
with
354 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ services: | |
|
||
Application\: | ||
resource: '../src/Application/' | ||
exclude: | ||
- '../src/Application/DomainEventsHandler/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
services: | ||
_defaults: | ||
autowire: true | ||
public: false | ||
|
||
Domain\EventsRegisterer: ~ | ||
|
||
Application\DomainEventsHandler\: | ||
resource: '%kernel.project_dir%/src/Application/DomainEventsHandler/**.php' | ||
tags: | ||
- { name: messenger.message_handler, bus: event.bus } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Application/DomainEventsHandler/LogWhenDinosaurDied.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\DomainEventsHandler; | ||
|
||
use Domain\Event\DinosaurDied; | ||
use Domain\Event\EventInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; | ||
|
||
class LogWhenDinosaurDied implements MessageSubscriberInterface | ||
{ | ||
public function __construct( | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
public function __invoke(EventInterface $event): void | ||
{ | ||
if (!$event instanceof DinosaurDied) { | ||
return; | ||
} | ||
|
||
$this->logger->info(sprintf( | ||
'Dinosaur %s died', | ||
$event->getDinosaurName() | ||
)); | ||
} | ||
|
||
public static function getHandledMessages(): iterable | ||
{ | ||
yield DinosaurDied::class; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Application/DomainEventsHandler/LogWhenDinosaurIsBorn.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\DomainEventsHandler; | ||
|
||
use Domain\Collection\DinosaursCollection; | ||
use Domain\Event\DinosaurIsBorn; | ||
use Domain\Event\EventInterface; | ||
use Domain\Exception\DinosaurNotFoundException; | ||
use Domain\Model\Dinosaur; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; | ||
|
||
class LogWhenDinosaurIsBorn implements MessageSubscriberInterface | ||
{ | ||
public function __construct( | ||
private DinosaursCollection $dinosaursCollection, | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
public function __invoke(EventInterface $event): void | ||
{ | ||
$dinosaurId = $event->getAggregateRootId(); | ||
|
||
$dinosaur = $this->dinosaursCollection->find($dinosaurId); | ||
|
||
if (!$dinosaur instanceof Dinosaur) { | ||
throw new DinosaurNotFoundException($dinosaurId); | ||
} | ||
|
||
$this->logger->info(sprintf( | ||
'Dinosaur %s was born', | ||
$dinosaur->getName() | ||
)); | ||
} | ||
|
||
public static function getHandledMessages(): iterable | ||
{ | ||
yield DinosaurIsBorn::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\MessageBus; | ||
|
||
interface EventBus | ||
{ | ||
public function dispatch(object $event): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Event; | ||
|
||
use Domain\Model\Dinosaur; | ||
|
||
final class DinosaurDied implements EventInterface | ||
{ | ||
private string $dinosaurName; | ||
|
||
public function __construct(private Dinosaur $dinosaur) | ||
{ | ||
$this->dinosaurName = $dinosaur->getName(); | ||
} | ||
|
||
public function getAggregateRootId(): string | ||
{ | ||
return (string) $this->dinosaur->getId(); | ||
} | ||
|
||
public function getDinosaurName(): string | ||
{ | ||
return $this->dinosaurName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Event; | ||
|
||
use Domain\Model\Dinosaur; | ||
|
||
class DinosaurIsBorn implements EventInterface | ||
{ | ||
public function __construct( | ||
private Dinosaur $dinosaur | ||
) { | ||
} | ||
|
||
public function getAggregateRootId(): string | ||
{ | ||
return (string) $this->dinosaur->getId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Event; | ||
|
||
interface EventInterface | ||
{ | ||
public function getAggregateRootId(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain; | ||
|
||
use Domain\Event\EventInterface; | ||
|
||
class EventsRegisterer | ||
{ | ||
/** @var array<EventInterface> */ | ||
private array $events = []; | ||
|
||
public function register(EventInterface ...$events): void | ||
{ | ||
array_push($this->events, ...$events); | ||
} | ||
|
||
/** | ||
* @return array<EventInterface> | ||
*/ | ||
public function getEvents(): array | ||
{ | ||
return $this->events; | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
$this->events = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain; | ||
|
||
use Domain\Event\EventInterface; | ||
|
||
trait HasEventsRegisterer | ||
{ | ||
private EventsRegisterer $eventsRegisterer; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function setEventsRegisterer(EventsRegisterer $eventsRegisterer): void | ||
{ | ||
$this->eventsRegisterer = $eventsRegisterer; | ||
} | ||
|
||
public function getEventsRegisterer(): EventsRegisterer | ||
{ | ||
return $this->eventsRegisterer; | ||
} | ||
|
||
public function registerEvents(EventInterface ...$events): void | ||
{ | ||
$this->eventsRegisterer->register(...$events); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Model; | ||
|
||
interface AggregateRoot | ||
{ | ||
public function getId(): int; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.