Completed
Pull Request — master (#264)
by Luc
05:13
created

DefaultEventEditingService::copyEvent()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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