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
12 changes: 7 additions & 5 deletions docs/ddd/collections.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Collections

A domain collection is bound to `MsgPhp\Domain\DomainCollectionInterface`. It's main usage is to aggregate objects bound
together by a root entity.
A domain collection is bound to `MsgPhp\Domain\DomainCollectionInterface`. Its main purpose is is to aggregate objects
bound together by a root entity.

The technical implementation is generic and may hold any type of elements from any iterable value.

## Implementations

- `MsgPhp\Domain\DomainCollection` (generic collection)
- `MsgPhp\Domain\Infra\Doctrine\DomainCollection` (Doctrine collection)
- requires: `doctrine/collections`
- `MsgPhp\Domain\DomainCollection`
- Generic collection
- `MsgPhp\Domain\Infra\Doctrine\DomainCollection`
- Doctrine collection
- Requires `doctrine/collections`

## API

Expand Down
2 changes: 1 addition & 1 deletion docs/ddd/factory/object.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Object factory

A domain object factory is bound to `MsgPhp\Domain\Factory\DomainObjectFactoryInterface`. It's usage is to initialize
A domain object factory is bound to `MsgPhp\Domain\Factory\DomainObjectFactoryInterface`. Its purpose is to initialize
any domain object based on a class name and context.

## Implementations
Expand Down
12 changes: 7 additions & 5 deletions docs/ddd/identifiers.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Identifiers

An identifier is a domain value object and is bound to `MsgPhp\Domain\DomainIdInterface`. It's used to identify domain
objects, i.e. entities.
An identifier is a domain value object and is bound to `MsgPhp\Domain\DomainIdInterface`. Its purpose is to identify
domain objects, e.g. entities.

## Implementations

- `MsgPhp\Domain\DomainId` (generic scalar values)
- `MsgPhp\Domain\Infra\Uuid\DomainId` (UUID values)
- requires: `ramsey/uuid`
- `MsgPhp\Domain\DomainId`
- Generic scalar values
- `MsgPhp\Domain\Infra\Uuid\DomainId`
- UUID values
- Requires `ramsey/uuid`

## API

Expand Down
12 changes: 7 additions & 5 deletions docs/ddd/identity-mapping.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Identity mapping

An identity mapping is a domain service and is bound to `MsgPhp\Domain\DomainIdentityMappingInterface`. It tells about the
identifier mappings for a known domain object.
An identity mapping is a domain service and is bound to `MsgPhp\Domain\DomainIdentityMappingInterface`. It tells about
the identifier mappings for a known domain object.

## Implementations

- `MsgPhp\Domain\Infra\InMemory\DomainIdentityMapping` (uses in-memory identity map)
- `MsgPhp\Domain\Infra\Doctrine\DomainIdentityMapping` (uses Doctrine identity map)
- requires: `doctrine/orm`
- `MsgPhp\Domain\Infra\InMemory\DomainIdentityMapping`
- In-memory identity map
- `MsgPhp\Domain\Infra\Doctrine\DomainIdentityMapping`
- Doctrine identity map
- Requires `doctrine/orm`

## API

Expand Down
8 changes: 5 additions & 3 deletions docs/ddd/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Note default (interface bound) repositories are provided per domain layer.

## Implementations

- `MsgPhp\Domain\Infra\InMemory\DomainEntityRepositoryTrait` (In-memory persistence)
- `MsgPhp\Domain\Infra\Doctrine\DomainEntityRepositoryTrait` (Doctrine persistence)
- requires: `doctrine/orm`
- `MsgPhp\Domain\Infra\InMemory\DomainEntityRepositoryTrait`
- In-memory persistence
- `MsgPhp\Domain\Infra\Doctrine\DomainEntityRepositoryTrait`
- Doctrine persistence
- Requires `doctrine/orm`

## API

Expand Down
49 changes: 49 additions & 0 deletions docs/event-sourcing/domain-event-handlers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

## Implementations

- `MsgPhp\Domain\Event\DomainEventHandlerTrait`
- Maps `<Type>DomainEvent` to `handle<Type>Event()`

## API

### `handleEvent(DomainEventInterface $event): void`

Handles the given domain event for a known subject.

## Generic example

```php
<?php

use MsgPhp\Domain\Event\DomainEventHandlerInterface;
use MsgPhp\Domain\Event\DomainEventHandlerTrait;

class MyDomainEvent
{
public $newValue;

public function __construct($value) {
$this->newValue = $value;
}
}

class MyEntity implements DomainEventHandlerInterface
{
use DomainEventHandlerTrait;

public $value;

private function handleMyEvent(MyDomainEvent $event): void
{
$this->value = $event->newValue;
}

}

$entity = new MyEntity();
$entity->handleEvent(new MyDomainEvent('value'));
```
12 changes: 12 additions & 0 deletions docs/event-sourcing/domain-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Domain events

A domain event is bound to `MsgPhp\Domain\Event\DomainEventInterface`. Its purpose is to identify concrete domain events
and represents something that happens, leading to an application state change.

## Implementations

Domain events provided and handled by default [entity features](../ddd/entities.md):

- `MsgPhp\Domain\Event\ConfirmDomainEvent`
- `MsgPhp\Domain\Event\DisableDomainEvent`
- `MsgPhp\Domain\Event\EnableDomainEvent`
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

# News

- **`2018-01-26`** Added initial event sourcing chapter
- **`2018-01-25`** Added initial project [code conventions](code-conventions.md)
- **`2018-01-18`** Initial documentation setup. Work in progress as of today.
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ pages:
- Static factory: ddd/factory/static.md
- Repositories: ddd/repositories.md
- Entities: ddd/entities.md
- Event sourcing:
- Domain events: event-sourcing/domain-events.md
- Domain event handlers: event-sourcing/domain-event-handlers.md
7 changes: 7 additions & 0 deletions src/Domain/Entity/Features/CanBeConfirmed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MsgPhp\Domain\Entity\Features;

use MsgPhp\Domain\Event\ConfirmDomainEvent;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
Expand Down Expand Up @@ -35,4 +37,9 @@ public function confirm(): void
$this->confirmationToken = null;
$this->confirmedAt = new \DateTimeImmutable();
}

private function handleConfirmEvent(ConfirmDomainEvent $event): void
{
$this->confirm();
}
}
11 changes: 11 additions & 0 deletions src/Domain/Entity/Features/CanBeEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MsgPhp\Domain\Entity\Features;

use MsgPhp\Domain\Entity\Fields\EnabledField;
use MsgPhp\Domain\Event\{DisableDomainEvent, EnableDomainEvent};

/**
* @author Roland Franssen <franssen.roland@gmail.com>
Expand All @@ -22,4 +23,14 @@ public function disable(): void
{
$this->enabled = false;
}

private function handleEnableEvent(EnableDomainEvent $event): void
{
$this->enable();
}

private function handleDisableEvent(DisableDomainEvent $event): void
{
$this->disable();
}
}
12 changes: 12 additions & 0 deletions src/Domain/Event/ConfirmDomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class ConfirmDomainEvent implements DomainEventInterface
{
}
12 changes: 12 additions & 0 deletions src/Domain/Event/DisableDomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class DisableDomainEvent implements DomainEventInterface
{
}
13 changes: 13 additions & 0 deletions src/Domain/Event/DomainEventHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
interface DomainEventHandlerInterface
{
public function handleEvent(DomainEventInterface $event): void;
}
30 changes: 30 additions & 0 deletions src/Domain/Event/DomainEventHandlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

use MsgPhp\Domain\Exception\UnknownDomainEventException;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
trait DomainEventHandlerTrait
{
public function handleEvent(DomainEventInterface $event): void
{
$method = false === ($pos = strrpos($class = get_class($event), '\\')) ? $class : substr($class, $pos + 1);

if ('DomainEvent' === substr($method, -11)) {
$method = substr($method, 0, -11);
}

$method = 'handle'.ucfirst($method).'Event';

if (!method_exists($this, $method)) {
throw UnknownDomainEventException::createForHandler($this, $event);
}

$this->$method($event);
}
}
12 changes: 12 additions & 0 deletions src/Domain/Event/DomainEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
interface DomainEventInterface
{
}
12 changes: 12 additions & 0 deletions src/Domain/Event/EnableDomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Event;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class EnableDomainEvent implements DomainEventInterface
{
}
18 changes: 18 additions & 0 deletions src/Domain/Exception/UnknownDomainEventException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace MsgPhp\Domain\Exception;

use MsgPhp\Domain\Event\{DomainEventHandlerInterface, DomainEventInterface};

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class UnknownDomainEventException extends \RuntimeException implements DomainExceptionInterface
{
public static function createForHandler(DomainEventHandlerInterface $handler, DomainEventInterface $event): self
{
return new self(sprintf('Domain event "%s" cannot be handled by "%s".', get_class($event), get_class($handler)));
}
}