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

DefaultEventEditingService::createEvent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 6
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