Skip to content

Commit 3db89fa

Browse files
authored
setup basic event sourcing (#52)
1 parent 49ed446 commit 3db89fa

18 files changed

Lines changed: 219 additions & 19 deletions

‎docs/ddd/collections.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Collections
22

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

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

88
## Implementations
99

10-
- `MsgPhp\Domain\DomainCollection` (generic collection)
11-
- `MsgPhp\Domain\Infra\Doctrine\DomainCollection` (Doctrine collection)
12-
- requires: `doctrine/collections`
10+
- `MsgPhp\Domain\DomainCollection`
11+
- Generic collection
12+
- `MsgPhp\Domain\Infra\Doctrine\DomainCollection`
13+
- Doctrine collection
14+
- Requires `doctrine/collections`
1315

1416
## API
1517

‎docs/ddd/factory/object.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Object factory
22

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

66
## Implementations

‎docs/ddd/identifiers.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Identifiers
22

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

66
## Implementations
77

8-
- `MsgPhp\Domain\DomainId` (generic scalar values)
9-
- `MsgPhp\Domain\Infra\Uuid\DomainId` (UUID values)
10-
- requires: `ramsey/uuid`
8+
- `MsgPhp\Domain\DomainId`
9+
- Generic scalar values
10+
- `MsgPhp\Domain\Infra\Uuid\DomainId`
11+
- UUID values
12+
- Requires `ramsey/uuid`
1113

1214
## API
1315

‎docs/ddd/identity-mapping.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Identity mapping
22

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

66
## Implementations
77

8-
- `MsgPhp\Domain\Infra\InMemory\DomainIdentityMapping` (uses in-memory identity map)
9-
- `MsgPhp\Domain\Infra\Doctrine\DomainIdentityMapping` (uses Doctrine identity map)
10-
- requires: `doctrine/orm`
8+
- `MsgPhp\Domain\Infra\InMemory\DomainIdentityMapping`
9+
- In-memory identity map
10+
- `MsgPhp\Domain\Infra\Doctrine\DomainIdentityMapping`
11+
- Doctrine identity map
12+
- Requires `doctrine/orm`
1113

1214
## API
1315

‎docs/ddd/repositories.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ Note default (interface bound) repositories are provided per domain layer.
88

99
## Implementations
1010

11-
- `MsgPhp\Domain\Infra\InMemory\DomainEntityRepositoryTrait` (In-memory persistence)
12-
- `MsgPhp\Domain\Infra\Doctrine\DomainEntityRepositoryTrait` (Doctrine persistence)
13-
- requires: `doctrine/orm`
11+
- `MsgPhp\Domain\Infra\InMemory\DomainEntityRepositoryTrait`
12+
- In-memory persistence
13+
- `MsgPhp\Domain\Infra\Doctrine\DomainEntityRepositoryTrait`
14+
- Doctrine persistence
15+
- Requires `doctrine/orm`
1416

1517
## API
1618

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Domain event handlers
2+
3+
A domain event handler is bound to `MsgPhp\Domain\Event\DomainEventHandlerInterface`. Its purpose is to handle domain
4+
events. By convention a trait implementation is provided to map concrete events to specialized handling methods.
5+
6+
## Implementations
7+
8+
- `MsgPhp\Domain\Event\DomainEventHandlerTrait`
9+
- Maps `<Type>DomainEvent` to `handle<Type>Event()`
10+
11+
## API
12+
13+
### `handleEvent(DomainEventInterface $event): void`
14+
15+
Handles the given domain event for a known subject.
16+
17+
## Generic example
18+
19+
```php
20+
<?php
21+
22+
use MsgPhp\Domain\Event\DomainEventHandlerInterface;
23+
use MsgPhp\Domain\Event\DomainEventHandlerTrait;
24+
25+
class MyDomainEvent
26+
{
27+
public $newValue;
28+
29+
public function __construct($value) {
30+
$this->newValue = $value;
31+
}
32+
}
33+
34+
class MyEntity implements DomainEventHandlerInterface
35+
{
36+
use DomainEventHandlerTrait;
37+
38+
public $value;
39+
40+
private function handleMyEvent(MyDomainEvent $event): void
41+
{
42+
$this->value = $event->newValue;
43+
}
44+
45+
}
46+
47+
$entity = new MyEntity();
48+
$entity->handleEvent(new MyDomainEvent('value'));
49+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Domain events
2+
3+
A domain event is bound to `MsgPhp\Domain\Event\DomainEventInterface`. Its purpose is to identify concrete domain events
4+
and represents something that happens, leading to an application state change.
5+
6+
## Implementations
7+
8+
Domain events provided and handled by default [entity features](../ddd/entities.md):
9+
10+
- `MsgPhp\Domain\Event\ConfirmDomainEvent`
11+
- `MsgPhp\Domain\Event\DisableDomainEvent`
12+
- `MsgPhp\Domain\Event\EnableDomainEvent`

‎docs/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
# News
88

9+
- **`2018-01-26`** Added initial event sourcing chapter
910
- **`2018-01-25`** Added initial project [code conventions](code-conventions.md)
1011
- **`2018-01-18`** Initial documentation setup. Work in progress as of today.

‎mkdocs.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ pages:
2323
- Static factory: ddd/factory/static.md
2424
- Repositories: ddd/repositories.md
2525
- Entities: ddd/entities.md
26+
- Event sourcing:
27+
- Domain events: event-sourcing/domain-events.md
28+
- Domain event handlers: event-sourcing/domain-event-handlers.md

‎src/Domain/Entity/Features/CanBeConfirmed.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MsgPhp\Domain\Entity\Features;
66

7+
use MsgPhp\Domain\Event\ConfirmDomainEvent;
8+
79
/**
810
* @author Roland Franssen <franssen.roland@gmail.com>
911
*/
@@ -35,4 +37,9 @@ public function confirm(): void
3537
$this->confirmationToken = null;
3638
$this->confirmedAt = new \DateTimeImmutable();
3739
}
40+
41+
private function handleConfirmEvent(ConfirmDomainEvent $event): void
42+
{
43+
$this->confirm();
44+
}
3845
}

0 commit comments

Comments
 (0)