Completed
Pull Request — master (#344)
by Luc
10:55
created

DefaultEventEditingService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 38.03 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 10
dl 54
loc 142
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updateAudience() 0 6 1
A deleteEvent() 0 4 1
A __construct() 21 21 1
B createEvent() 24 24 1
B copyEvent() 0 25 3
A updateMajorInfo() 8 8 1
A updateLocation() 0 8 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
namespace CultuurNet\UDB3\Event;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\Repository\AggregateNotFoundException;
7
use Broadway\Repository\RepositoryInterface;
8
use Broadway\UuidGenerator\UuidGeneratorInterface;
9
use CultuurNet\UDB3\CalendarInterface;
10
use CultuurNet\UDB3\Event\Commands\UpdateAudience;
11
use CultuurNet\UDB3\Event\Commands\UpdateLocation;
12
use CultuurNet\UDB3\Event\Commands\UpdateMajorInfo;
13
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
14
use CultuurNet\UDB3\Event\ValueObjects\Audience;
15
use CultuurNet\UDB3\Label\LabelServiceInterface;
16
use CultuurNet\UDB3\Language;
17
use CultuurNet\UDB3\Location\Location;
18
use CultuurNet\UDB3\Location\LocationId;
19
use CultuurNet\UDB3\Offer\Commands\OfferCommandFactoryInterface;
20
use CultuurNet\UDB3\Offer\DefaultOfferEditingService;
21
use CultuurNet\UDB3\Title;
22
23
class DefaultEventEditingService extends DefaultOfferEditingService implements EventEditingServiceInterface
24
{
25
    /**
26
     * @var EventServiceInterface
27
     */
28
    protected $eventService;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    protected $writeRepository;
34
35
    /**
36
     * @param EventServiceInterface $eventService
37
     * @param CommandBusInterface $commandBus
38
     * @param UuidGeneratorInterface $uuidGenerator
39
     * @param DocumentRepositoryInterface $readRepository
40
     * @param OfferCommandFactoryInterface $commandFactory
41
     * @param RepositoryInterface $writeRepository
42
     * @param LabelServiceInterface $labelService
43
     */
44 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...
45
        EventServiceInterface $eventService,
46
        CommandBusInterface $commandBus,
47
        UuidGeneratorInterface $uuidGenerator,
48
        DocumentRepositoryInterface $readRepository,
49
        OfferCommandFactoryInterface $commandFactory,
50
        RepositoryInterface $writeRepository,
51
        LabelServiceInterface $labelService
52
    ) {
53
        parent::__construct(
54
            $commandBus,
55
            $uuidGenerator,
56
            $readRepository,
57
            $commandFactory,
58
            $labelService,
59
            new EventTypeResolver(),
60
            new EventThemeResolver()
61
        );
62
        $this->eventService = $eventService;
63
        $this->writeRepository = $writeRepository;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 View Code Duplication
    public function createEvent(
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...
70
        Language $mainLanguage,
71
        Title $title,
72
        EventType $eventType,
73
        Location $location,
74
        CalendarInterface $calendar,
75
        $theme = null
76
    ) {
77
        $eventId = $this->uuidGenerator->generate();
78
79
        $event = Event::create(
80
            $eventId,
81
            $title,
82
            $eventType,
83
            $location,
84
            $calendar,
85
            $theme,
86
            $this->publicationDate
87
        );
88
89
        $this->writeRepository->save($event);
90
91
        return $eventId;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function copyEvent($originalEventId, CalendarInterface $calendar)
98
    {
99
        if (!is_string($originalEventId)) {
100
            throw new \InvalidArgumentException(
101
                'Expected originalEventId to be a string, received ' . gettype($originalEventId)
102
            );
103
        }
104
105
        try {
106
            /** @var Event $event */
107
            $event = $this->writeRepository->load($originalEventId);
108
        } catch (AggregateNotFoundException $exception) {
109
            throw new \InvalidArgumentException(
110
                'No original event found to copy with id ' . $originalEventId
111
            );
112
        }
113
114
        $eventId = $this->uuidGenerator->generate();
115
116
        $newEvent = $event->copy($eventId, $calendar);
117
118
        $this->writeRepository->save($newEvent);
119
120
        return $eventId;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 View Code Duplication
    public function updateMajorInfo($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null)
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...
127
    {
128
        $this->guardId($eventId);
129
130
        return $this->commandBus->dispatch(
131
            new UpdateMajorInfo($eventId, $title, $eventType, $location, $calendar, $theme)
132
        );
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public function updateLocation($eventId, LocationId $locationId)
139
    {
140
        $this->guardId($eventId);
141
142
        return $this->commandBus->dispatch(
143
            new UpdateLocation($eventId, $locationId)
144
        );
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function updateAudience($eventId, Audience $audience)
151
    {
152
        return $this->commandBus->dispatch(
153
            new UpdateAudience($eventId, $audience)
154
        );
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function deleteEvent($eventId)
161
    {
162
        return $this->delete($eventId);
163
    }
164
}
165