DefaultCommandTester::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 16
loc 16
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 6
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2017 Constantin Galbenu <[email protected]>
4
 */
5
6
namespace Gica\Cqrs\Command\CommandTester;
7
8
use Gica\Cqrs\Aggregate\AggregateRepository;
9
use Gica\Cqrs\Command;
10
use Gica\Cqrs\Command\CommandApplier;
11
use Gica\Cqrs\Command\CommandSubscriber;
12
use Gica\Cqrs\Command\CommandTester;
13
use Gica\Cqrs\Command\CommandWithMetadata;
14
use Gica\Cqrs\Command\MetadataWrapper as CommandMetadataFactory;
15
use Gica\Cqrs\Command\ValueObject\CommandHandlerAndAggregate;
16
use Gica\Cqrs\Event\EventsApplier\EventsApplierOnAggregate;
17
use Gica\Cqrs\Event\EventWithMetaData;
18
use Gica\Cqrs\Event\MetaData;
19
use Gica\Cqrs\Event\MetadataFactory as EventMetadataFactory;
20
use Gica\Cqrs\Event\ScheduledEvent;
21
use Gica\Cqrs\Scheduling\ScheduledCommand;
22
use Gica\Types\Guid;
23
24 View Code Duplication
class DefaultCommandTester implements CommandTester
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
{
26
    /**
27
     * @var CommandSubscriber
28
     */
29
    private $commandSubscriber;
30
    /**
31
     * @var CommandApplier
32
     */
33
    private $commandApplier;
34
    /**
35
     * @var AggregateRepository
36
     */
37
    private $aggregateRepository;
38
    /**
39
     * @var EventsApplierOnAggregate
40
     */
41
    private $eventsApplierOnAggregate;
42
    /**
43
     * @var EventMetadataFactory
44
     */
45
    private $eventMetadataFactory;
46
    /**
47
     * @var CommandMetadataFactory
48
     */
49
    private $commandMetadataFactory;
50
51 2
    public function __construct(
52
        CommandSubscriber $commandSubscriber,
53
        CommandApplier $commandApplier,
54
        AggregateRepository $aggregateRepository,
55
        EventsApplierOnAggregate $eventsApplier,
56
        EventMetadataFactory $eventMetadataFactory,
57
        CommandMetadataFactory $commandMetadataFactory
58
    )
59
    {
60 2
        $this->commandSubscriber = $commandSubscriber;
61 2
        $this->commandApplier = $commandApplier;
62 2
        $this->aggregateRepository = $aggregateRepository;
63 2
        $this->eventsApplierOnAggregate = $eventsApplier;
64 2
        $this->eventMetadataFactory = $eventMetadataFactory;
65 2
        $this->commandMetadataFactory = $commandMetadataFactory;
66 2
    }
67
68 2
    public function canExecuteCommand(Command $command): bool
69
    {
70
        try {
71 2
            $command = $this->commandMetadataFactory->wrapCommandWithMetadata($command, null);
72 2
            $this->applyCommand($command, $this->loadCommandHandlerAndAggregate($command));
73 1
            return true;
74 1
        } catch (\Exception $exception) {
75 1
            return false;
76
        }
77
    }
78
79 2
    private function loadCommandHandlerAndAggregate(CommandWithMetadata $command): CommandHandlerAndAggregate
80
    {
81 2
        return new CommandHandlerAndAggregate(
82 2
            $this->commandSubscriber->getHandlerForCommand($command->getCommand()),
83 2
            $this->aggregateRepository->loadAggregate($this->commandSubscriber->getHandlerForCommand($command->getCommand())->getHandlerClass(), $command->getAggregateId())
84
        );
85
    }
86
87 1
    private function decorateEventWithMetaData($event, MetaData $metaData): EventWithMetaData
88
    {
89 1
        return new EventWithMetaData($event, $metaData->withEventId(Guid::generate()));
90
    }
91
92
    /**
93
     * @param CommandWithMetadata $command
94
     * @param CommandHandlerAndAggregate $handlerAndAggregate
95
     * @return void
96
     */
97 2
    private function applyCommand(CommandWithMetadata $command, CommandHandlerAndAggregate $handlerAndAggregate)
98
    {
99 2
        $aggregate = $handlerAndAggregate->getAggregate();
100 2
        $handler = $handlerAndAggregate->getCommandHandler();
101
102 2
        $metaData = $this->eventMetadataFactory->factoryEventMetadata($command, $aggregate);
103
104 2
        $newMessageGenerator = $this->commandApplier->applyCommand($aggregate, $command->getCommand(), $handler->getMethodName());
105
106 2
        foreach ($newMessageGenerator as $message) {
107 1
            if (!$this->isScheduledCommand($message)) {
108 1
                $eventWithMetaData = $this->decorateEventWithMetaData($message, $metaData);
109 1
                if (!$this->isScheduledEvent($message)) {
110 1
                    $this->eventsApplierOnAggregate->applyEventsOnAggregate($aggregate, [$eventWithMetaData]);
111
                }
112
            }
113
        }
114 1
    }
115
116 1
    private function isScheduledEvent($event): bool
117
    {
118 1
        return $event instanceof ScheduledEvent;
119
    }
120
121 1
    private function isScheduledCommand($message): bool
122
    {
123 1
        return $message instanceof ScheduledCommand;
124
    }
125
}