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