Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/ddd/factory/entity.md → docs/ddd/factory/entity_aware.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Entity factory
# Entity aware factory

An entity factory is a domain object factory and is bound to `MsgPhp\Domain\Factory\EntityFactoryInterface`.
Besides initializing the entity via `create()` it can also initialize an identifier using either `identify()` or
`nextIdentifier()`.
An entity aware factory is a domain object factory and is bound to `MsgPhp\Domain\Factory\EntityAwareFactoryInterface`.
Besides initializing any domain object via `create()` it can also initialize an entity identifier using either
`identify()` or `nextIdentifier()`.

## Implementations

### `MsgPhp\Domain\Factory\EntityFactory`
### `MsgPhp\Domain\Factory\EntityAwareFactory`

Generic entity factory and decorates any object factory. Additionally it must be provided with the identifier class
mapping.
Generic entity factory and decorates any object factory. Additionally it must be provided with an entity to identifier
class mapping.

## API

Expand Down
5 changes: 3 additions & 2 deletions docs/event-sourcing/domain-event-handlers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Domain event handlers

A domain event handler is bound to `MsgPhp\Domain\Event\DomainEventHandlerInterface`. Its purpose is to handle domain
events. By convention a trait implementation is provided to map concrete events to specialized handling methods.
A domain event handler is bound to `MsgPhp\Domain\Event\DomainEventHandlerInterface`. Its purpose is to handle
[domain events](domain-events.md). By convention a trait implementation is provided to map concrete events to
specialized handling methods.

## Implementations

Expand Down
5 changes: 2 additions & 3 deletions docs/message-driven/cqrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ Usually a command handler is designed, but not limited, to handle one specific c

### `MsgPhp\Domain\Command\EventSourcingCommandHandlerTrait`

Sources a domain event from a command message.
Handles a command message by sourcing a domain event.

- `doHandle(object $command, callable $onHandled = null): void`
- Invokes the domain event for the given command message
- `handle(object $command, callable $onHandled = null): void`
- If the domain event is handled `$onHandled` will be invoked (if given), receiving the handler as first argument
- `abstract getDomainEvent(object $command): DomainEventInterface`
- The [domain event](../event-sourcing/domain-events.md) to be handled
Expand Down
4 changes: 2 additions & 2 deletions docs/message-driven/domain-message-bus.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Domain message bus

A domain message bus is bound to `MsgPhp\Domain\DomainMessageBusInterface`. Its purpose is to dispatch any type of
message object and helps you to use CQRS and event sourcing.
A domain message bus is bound to `MsgPhp\Domain\Message\DomainMessageBusInterface`. Its purpose is to dispatch any type
of message object and helps you to use [CQRS](cqrs.md) and [event sourcing](../event-sourcing/domain-event-handlers.md).

## Implementations

Expand Down
32 changes: 32 additions & 0 deletions docs/message-driven/domain-message-dispatcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Domain message dispatcher

The domain message dispatcher is a utility trait. Its purpose is to dispatch a message, factorized by a
[object factory](../ddd/factory/object.md), using any [message bus](domain-message-bus.md).

## API

> Exposed `private` as a trait. You can decide to [change method visibility](https://secure.php.net/manual/en/language.oop5.traits.php#language.oop5.traits.visibility)
on a per case basis.

### `dispatch(string $class, array $context = []): mixed`

Dispatches the given message object. The dispatcher can return a value coming from handlers, but is not required to do
so.

## Basic example

```php
<?php

use MsgPhp\Domain\Message\DomainMessageDispatchingTrait;

class MyClass
{
use DomainMessageDispatchingTrait;

public function doSomething(): void
{
$this->dispatch(SomeMessage::class, ['arg' => 'value']);
}
}
```
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pages:
- Collections: ddd/collections.md
- Factories:
- Object factory: ddd/factory/object.md
- Entity factory: ddd/factory/entity.md
- Entity aware factory: ddd/factory/entity_aware.md
- Static factory: ddd/factory/static.md
- Repositories: ddd/repositories.md
- Entities: ddd/entities.md
Expand All @@ -28,4 +28,5 @@ pages:
- Domain event handlers: event-sourcing/domain-event-handlers.md
- Message driven:
- Domain message bus: message-driven/domain-message-bus.md
- Domain message dispatcher: message-driven/domain-message-dispatcher.md
- CQRS: message-driven/cqrs.md
2 changes: 1 addition & 1 deletion src/Domain/Command/EventSourcingCommandHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait EventSourcingCommandHandlerTrait
/**
* @param object $command
*/
protected function doHandle($command, callable $onHandled = null): void
private function handle($command, callable $onHandled = null): void
{
$event = $this->getDomainEvent($command);
$handler = $this->getDomainEventHandler($command);
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Event/ConfirmDomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class ConfirmDomainEvent implements DomainEventInterface
class ConfirmDomainEvent implements DomainEventInterface
{
}
2 changes: 1 addition & 1 deletion src/Domain/Event/DisableDomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class DisableDomainEvent implements DomainEventInterface
class DisableDomainEvent implements DomainEventInterface
{
}
2 changes: 1 addition & 1 deletion src/Domain/Event/EnableDomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class EnableDomainEvent implements DomainEventInterface
class EnableDomainEvent implements DomainEventInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class EntityFactory implements EntityFactoryInterface
final class EntityAwareFactory implements EntityAwareFactoryInterface
{
private $identifierMapping;
private $factory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
interface EntityFactoryInterface extends DomainObjectFactoryInterface
interface EntityAwareFactoryInterface extends DomainObjectFactoryInterface
{
public function identify(string $class, $id): DomainIdInterface;

Expand Down
25 changes: 19 additions & 6 deletions src/Domain/Infra/DependencyInjection/Bundle/ContainerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,24 @@ public static function configureDoctrineOrmTargetEntities(ContainerBuilder $cont
]);
}

public static function removeDisabledCommandMessages(ContainerBuilder $container, array $commands): void
public static function configureCommandMessages(ContainerBuilder $container, array $classMapping, array $commands): void
{
if (!self::hasBundle($container, SimpleBusCommandBusBundle::class)) {
return;
}

foreach ($container->findTaggedServiceIds('command_handler') as $id => $attr) {
foreach ($container->findTaggedServiceIds($tag = 'command_handler') as $id => $attr) {
foreach ($attr as $attr) {
if (!isset($attr['handles']) || !empty($commands[$attr['handles']])) {
if (!isset($attr[$attrName = 'handles'])) {
continue;
}

if ($commands[$command = $attr[$attrName]] ?? false) {
if (isset($classMapping[$command])) {
$container->getDefinition($id)
->addTag($tag, [$attrName => $classMapping[$command]]);
}

continue;
}

Expand All @@ -215,19 +224,23 @@ public static function removeDisabledCommandMessages(ContainerBuilder $container
}
}

public static function registerEventMessages(ContainerBuilder $container, array $events): void
public static function configureEventMessages(ContainerBuilder $container, array $classMapping, array $events): void
{
if (!self::hasBundle($container, SimpleBusCommandBusBundle::class)) {
return;
}

$definition = $container->register(uniqid('msgphp'), SimpleBusInfra\EventMessageHandler::class);
$definition = $container->register(uniqid(SimpleBusInfra\EventMessageHandler::class), SimpleBusInfra\EventMessageHandler::class);
$definition
->setPublic(true)
->setArgument('$bus', new Reference('simple_bus.event_bus', ContainerBuilder::NULL_ON_INVALID_REFERENCE));

foreach ($events as $event) {
$definition->addTag('command_handler', ['handles' => $event]);
$definition->addTag($tag = 'command_handler', [$attrName = 'handles' => $event]);

if (isset($classMapping[$event])) {
$definition->addTag($tag, [$attrName => $classMapping[$event]]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace MsgPhp\Domain\Infra\DependencyInjection\Compiler;

use Doctrine\ORM\EntityManagerInterface as DoctrineEntityManager;
use MsgPhp\Domain\{Factory, DomainIdentityMappingInterface, DomainMessageBusInterface};
use MsgPhp\Domain\{DomainIdentityMappingInterface, Factory, Message};
use MsgPhp\Domain\Infra\{Doctrine as DoctrineInfra, InMemory as InMemoryInfra, SimpleBus as SimpleBusInfra};
use SimpleBus\Message\Bus\MessageBus as SimpleMessageBus;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -81,12 +81,12 @@ private function registerEntityFactory(ContainerBuilder $container): void
->setArgument('$mapping', array_merge(...$classMapping))
->setArgument('$factory', new Reference(Factory\ClassMappingObjectFactory::class.'.inner'));

self::register($container, Factory\EntityFactory::class)
self::register($container, Factory\EntityAwareFactory::class)
->setArgument('$identifierMapping', array_merge(...$idClassMapping))
->setArgument('$factory', new Reference(Factory\DomainObjectFactory::class));

self::alias($container, Factory\DomainObjectFactoryInterface::class, Factory\DomainObjectFactory::class);
self::alias($container, Factory\EntityFactoryInterface::class, Factory\EntityFactory::class);
self::alias($container, Factory\DomainObjectFactoryInterface::class, Factory\EntityAwareFactory::class);
self::alias($container, Factory\EntityAwareFactoryInterface::class, Factory\EntityAwareFactory::class);
}

private function registerMessageBus(ContainerBuilder $container): void
Expand All @@ -103,6 +103,6 @@ private function registerMessageBus(ContainerBuilder $container): void
$definition->setArgument('$bus', new Reference('simple_bus.command_bus'));
}

self::alias($container, DomainMessageBusInterface::class, SimpleBusInfra\DomainMessageBus::class);
self::alias($container, Message\DomainMessageBusInterface::class, SimpleBusInfra\DomainMessageBus::class);
}
}
2 changes: 1 addition & 1 deletion src/Domain/Infra/SimpleBus/DomainMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MsgPhp\Domain\Infra\SimpleBus;

use MsgPhp\Domain\DomainMessageBusInterface;
use MsgPhp\Domain\Message\DomainMessageBusInterface;
use SimpleBus\Message\Bus\MessageBus;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MsgPhp\Domain;
namespace MsgPhp\Domain\Message;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
Expand Down
27 changes: 27 additions & 0 deletions src/Domain/Message/DomainMessageDispatchingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Message;

use MsgPhp\Domain\Factory\DomainObjectFactoryInterface;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
trait DomainMessageDispatchingTrait
{
private $factory;
private $bus;

public function __construct(DomainObjectFactoryInterface $factory, DomainMessageBusInterface $bus)
{
$this->factory = $factory;
$this->bus = $bus;
}

private function dispatch(string $class, array $context = [])
{
return $this->bus->dispatch($this->factory->create($class, $context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use MsgPhp\Domain\DomainIdInterface;
use MsgPhp\Domain\Exception\InvalidClassException;
use MsgPhp\Domain\Factory\{DomainObjectFactoryInterface, EntityFactory};
use MsgPhp\Domain\Factory\{DomainObjectFactoryInterface, EntityAwareFactory};
use PHPUnit\Framework\TestCase;

final class EntityFactoryTest extends TestCase
final class EntityAwareFactoryTest extends TestCase
{
/** @var EntityFactory */
/** @var EntityAwareFactory */
private $factory;

protected function setUp(): void
Expand All @@ -36,7 +36,7 @@ protected function setUp(): void
return $o;
});

$this->factory = new EntityFactory(['alias_id' => 'id'], $innerFactory);
$this->factory = new EntityAwareFactory(['alias_id' => 'id'], $innerFactory);
}

public function testCreate(): void
Expand Down
2 changes: 1 addition & 1 deletion src/User/Command/DisableUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class DisableUserCommand
class DisableUserCommand
{
public $userId;

Expand Down
2 changes: 1 addition & 1 deletion src/User/Command/EnableUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class EnableUserCommand
class EnableUserCommand
{
public $userId;

Expand Down
17 changes: 8 additions & 9 deletions src/User/Command/Handler/DisableUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace MsgPhp\User\Command\Handler;

use MsgPhp\Domain\Command\EventSourcingCommandHandlerTrait;
use MsgPhp\Domain\DomainMessageBusInterface;
use MsgPhp\Domain\Event\{DomainEventInterface, DisableDomainEvent};
use MsgPhp\Domain\Message\{DomainMessageBusInterface, DomainMessageDispatchingTrait};
use MsgPhp\Domain\Event\DisableDomainEvent;
use MsgPhp\Domain\Factory\DomainObjectFactoryInterface;
use MsgPhp\User\Command\DisableUserCommand;
use MsgPhp\User\Entity\User;
Expand All @@ -19,27 +19,26 @@
final class DisableUserHandler
{
use EventSourcingCommandHandlerTrait;
use DomainMessageDispatchingTrait;

private $bus;
private $repository;
private $factory;

public function __construct(DomainMessageBusInterface $bus, UserRepositoryInterface $repository, DomainObjectFactoryInterface $factory)
public function __construct(DomainObjectFactoryInterface $factory, DomainMessageBusInterface $bus, UserRepositoryInterface $repository)
{
$this->factory = $factory;
$this->bus = $bus;
$this->repository = $repository;
$this->factory = $factory;
}

public function __invoke(DisableUserCommand $command): void
{
$this->doHandle($command, function (User $user): void {
$this->handle($command, function (User $user): void {
$this->repository->save($user);
$this->bus->dispatch($this->factory->create(UserDisabledEvent::class, ['user' => $user]));
$this->dispatch(UserDisabledEvent::class, [$user]);
});
}

protected function getDomainEvent(DisableUserCommand $command): DomainEventInterface
protected function getDomainEvent(DisableUserCommand $command): DisableDomainEvent
{
return $this->factory->create(DisableDomainEvent::class);
}
Expand Down
Loading