Completed
Push — master ( ca729a...c6cc13 )
by Constantin
02:37
created

DefaultCommandTester   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 40.2 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 41
loc 102
c 0
b 0
f 0
wmc 11
lcom 1
cbo 12
ccs 35
cts 35
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 1
A canExecuteCommand() 0 10 2
A loadCommandHandlerAndAggregate() 7 7 1
A decorateEventWithMetaData() 0 4 1
A applyCommand() 18 18 4
A isScheduledEvent() 0 4 1
A isScheduledCommand() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class DefaultCommandTester implements CommandTester
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 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    private function loadCommandHandlerAndAggregate(CommandWithMetadata $command): CommandHandlerAndAggregate
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    private function applyCommand(CommandWithMetadata $command, CommandHandlerAndAggregate $handlerAndAggregate)
0 ignored issues
show
Duplication introduced by
This method 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...
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
}