|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/cqrs project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\Aggregate; |
|
12
|
|
|
|
|
13
|
|
|
use Daikon\EventSourcing\EventStore\CommitInterface; |
|
14
|
|
|
use Daikon\EventSourcing\EventStore\StreamRevision; |
|
15
|
|
|
use Daikon\EventSourcing\EventStore\UnitOfWorkInterface; |
|
16
|
|
|
use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerInterface; |
|
17
|
|
|
use Daikon\MessageBus\EnvelopeInterface; |
|
18
|
|
|
use Daikon\MessageBus\MessageBusInterface; |
|
19
|
|
|
use Daikon\MessageBus\Metadata\Metadata; |
|
20
|
|
|
|
|
21
|
|
|
abstract class CommandHandler implements MessageHandlerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var MessageBusInterface */ |
|
24
|
|
|
private $messageBus; |
|
25
|
|
|
|
|
26
|
|
|
/** @var UnitOfWorkInterface */ |
|
27
|
|
|
private $unitOfWork; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(UnitOfWorkInterface $unitOfWork, MessageBusInterface $messageBus) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->messageBus = $messageBus; |
|
32
|
|
|
$this->unitOfWork = $unitOfWork; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function handle(EnvelopeInterface $envelope): bool |
|
36
|
|
|
{ |
|
37
|
|
|
$commandMessage = $envelope->getMessage(); |
|
38
|
|
|
$handlerName = (new \ReflectionClass($commandMessage))->getShortName(); |
|
39
|
|
|
$handlerMethod = "handle".ucfirst($handlerName); |
|
40
|
|
|
$handler = [ $this, $handlerMethod ]; |
|
41
|
|
|
if (!is_callable($handler)) { |
|
42
|
|
|
throw new \Exception("Handler '$handlerMethod' isn't callable on ".static::class); |
|
43
|
|
|
} |
|
44
|
|
|
return call_user_func($handler, $commandMessage, $envelope->getMetadata()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function commit(AggregateRootInterface $aggregateRoot, Metadata $metadata): bool |
|
48
|
|
|
{ |
|
49
|
|
|
$committed = false; |
|
50
|
|
|
foreach ($this->unitOfWork->commit($aggregateRoot, $metadata) as $newCommit) { |
|
51
|
|
|
if ($this->dispatch($newCommit) && !$committed) { |
|
52
|
|
|
$committed = true; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
return $committed; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function checkout( |
|
59
|
|
|
AggregateIdInterface $aggregateId, |
|
60
|
|
|
AggregateRevision $revision = null |
|
61
|
|
|
): AggregateRootInterface { |
|
62
|
|
|
return $this->unitOfWork->checkout($aggregateId, $revision); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function dispatch(CommitInterface $commit): bool |
|
66
|
|
|
{ |
|
67
|
|
|
$commitPublished = $this->messageBus->publish($commit, "commits"); |
|
68
|
|
|
// @todo might wanna send these from within the commit channel to guarantee order? |
|
69
|
|
|
foreach ($commit->getEventLog() as $committedEvent) { |
|
70
|
|
|
$this->messageBus->publish($committedEvent, "events"); |
|
71
|
|
|
} |
|
72
|
|
|
return $commitPublished; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):