DefaultCommandTesterWithSideEffect::applyCommand()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 22
loc 22
ccs 12
cts 12
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 4
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\CommandTesterWithSideEffect;
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 DefaultCommandTesterWithSideEffect implements CommandTesterWithSideEffect
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 1
    public function __construct(
52
        CommandSubscriber $commandSubscriber,
53
        CommandApplier $commandApplier,
54
        AggregateRepository $aggregateRepository,
55
        EventsApplierOnAggregate $eventsApplier,
56
        EventMetadataFactory $eventMetadataFactory,
57
        CommandMetadataFactory $commandMetadataFactory
58
    )
59
    {
60 1
        $this->commandSubscriber = $commandSubscriber;
61 1
        $this->commandApplier = $commandApplier;
62 1
        $this->aggregateRepository = $aggregateRepository;
63 1
        $this->eventsApplierOnAggregate = $eventsApplier;
64 1
        $this->eventMetadataFactory = $eventMetadataFactory;
65 1
        $this->commandMetadataFactory = $commandMetadataFactory;
66 1
    }
67
68 1
    public function shouldExecuteCommand(Command $command): bool
69
    {
70 1
        $command = $this->commandMetadataFactory->wrapCommandWithMetadata($command, null);
71 1
        return $this->applyCommand($command, $this->loadCommandHandlerAndAggregate($command));
72
    }
73
74 1
    private function loadCommandHandlerAndAggregate(CommandWithMetadata $command): CommandHandlerAndAggregate
75
    {
76 1
        return new CommandHandlerAndAggregate(
77 1
            $this->commandSubscriber->getHandlerForCommand($command->getCommand()),
78 1
            $this->aggregateRepository->loadAggregate($this->commandSubscriber->getHandlerForCommand($command->getCommand())->getHandlerClass(), $command->getAggregateId())
79
        );
80
    }
81
82 1
    private function decorateEventWithMetaData($event, MetaData $metaData): EventWithMetaData
83
    {
84 1
        return new EventWithMetaData($event, $metaData->withEventId(Guid::generate()));
85
    }
86
87
    /**
88
     * @param CommandWithMetadata $command
89
     * @param CommandHandlerAndAggregate $handlerAndAggregate
90
     * @return bool
91
     */
92 1
    private function applyCommand(CommandWithMetadata $command, CommandHandlerAndAggregate $handlerAndAggregate): bool
93
    {
94 1
        $aggregate = $handlerAndAggregate->getAggregate();
95 1
        $handler = $handlerAndAggregate->getCommandHandler();
96
97 1
        $metaData = $this->eventMetadataFactory->factoryEventMetadata($command, $aggregate);
98
99 1
        $newMessageGenerator = $this->commandApplier->applyCommand($aggregate, $command->getCommand(), $handler->getMethodName());
100
101 1
        foreach ($newMessageGenerator as $message) {
102 1
            if (!$this->isScheduledCommand($message)) {
103 1
                $eventWithMetaData = $this->decorateEventWithMetaData($message, $metaData);
104 1
                if (!$this->isScheduledEvent($message)) {
105 1
                    $this->eventsApplierOnAggregate->applyEventsOnAggregate($aggregate, [$eventWithMetaData]);
106
                }
107
            }
108
109 1
            return true;
110
        }
111
112 1
        return false;
113
    }
114
115 1
    private function isScheduledEvent($event): bool
116
    {
117 1
        return $event instanceof ScheduledEvent;
118
    }
119
120 1
    private function isScheduledCommand($message): bool
121
    {
122 1
        return $message instanceof ScheduledCommand;
123
    }
124
}