Completed
Push — master ( e9d24b...17f0c0 )
by Jonas
15s queued 11s
created

LocationMarkedAsDuplicateProcessManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CultuurNet\UDB3\Event;
6
7
use Broadway\CommandHandling\CommandBusInterface;
8
use Broadway\Domain\DomainMessage;
9
use Broadway\EventHandling\EventListenerInterface;
10
use CultuurNet\UDB3\Event\Commands\UpdateLocation;
11
use CultuurNet\UDB3\Event\ReadModel\Relations\RepositoryInterface;
12
use CultuurNet\UDB3\Event\ValueObjects\LocationId;
13
use CultuurNet\UDB3\Place\Events\MarkedAsDuplicate;
14
15
final class LocationMarkedAsDuplicateProcessManager implements EventListenerInterface
16
{
17
    /**
18
     * @var RepositoryInterface
19
     */
20
    private $relationsRepository;
21
22
    /**
23
     * @var CommandBusInterface
24
     */
25
    private $commandBus;
26
27
    public function __construct(
28
        RepositoryInterface $relationsRepository,
29
        CommandBusInterface $commandBus
30
    ) {
31
        $this->relationsRepository = $relationsRepository;
32
        $this->commandBus = $commandBus;
33
    }
34
35
    public function handle(DomainMessage $domainMessage)
36
    {
37
        $domainEvent = $domainMessage->getPayload();
38
39
        // Only handle (Place)MarkedAsDuplicate events.
40
        if (!($domainEvent instanceof MarkedAsDuplicate)) {
41
            return;
42
        }
43
44
        $duplicatePlaceId = $domainEvent->getPlaceId();
45
        $canonicalPlaceId = $domainEvent->getDuplicateOf();
46
47
        $eventIds = $this->relationsRepository->getEventsLocatedAtPlace($duplicatePlaceId);
48
49
        foreach ($eventIds as $eventId) {
50
            $this->commandBus->dispatch(
51
                new UpdateLocation($eventId, new LocationId($canonicalPlaceId))
52
            );
53
        }
54
    }
55
}
56