Пример #1
0
 /**
  * @param CreateWorkshop $command
  * @throws Exception
  */
 public function handle(CreateWorkshop $command)
 {
     if (!is_array($command->lessons) || count($command->lessons) === 0) {
         throw new Exception("Lessons collection can't be empty!");
     }
     $calendar = new Calendar(null, $command->title);
     $command->calendar = $calendar;
     $workshop = $this->workshopFactory->createFromCommand($command);
     /** @var CreateLesson $lessonCommand */
     foreach ($command->lessons as $lessonCommand) {
         $duration = Carbon::instance($lessonCommand->startDate)->diffInMinutes(Carbon::instance($lessonCommand->endDate), true);
         $eventCommand = new CreateEventCommand();
         $eventCommand->calendar = $calendar;
         $eventCommand->duration = $duration;
         $eventCommand->startDate = $lessonCommand->startDate;
         $eventCommand->endDate = Carbon::instance($lessonCommand->startDate)->addMinutes($duration);
         $eventCommand->repetitionDays = [];
         $eventCommand->type = EventType::TYPE_SINGLE;
         $event = $this->eventFactory->createFromCommand($eventCommand);
         $occurrences = $this->occurrenceFactory->generateCollectionFromEvent($event);
         $calendar->events()->add($event);
         if (count($occurrences) == !1) {
             throw new Exception('Could not generate occurrences from event');
         }
         $event->setOccurrences($occurrences);
         $lessonCommand->workshop = $workshop;
         $lessonCommand->event = $event;
         $lesson = $this->lessonFactory->createFromCommand($lessonCommand);
         $workshop->addLesson($lesson);
     }
     $this->calendarRepository->insert($calendar);
     $this->workshopRepository->insert($workshop);
 }
Пример #2
0
 private function product($value)
 {
     if (!is_null($value)) {
         if (is_array($value) || $value instanceof Traversable) {
             $result = [];
             /** @var Lesson $lesson */
             foreach ($value as $lesson) {
                 if ($lesson instanceof Lesson) {
                     $result[] = $this->lessonFactory->createCommand($lesson, $this->commandClass);
                 } elseif ($lesson instanceof LessonCommandInterface) {
                     $result[] = $this->lessonFactory->createFromCommand($lesson);
                 }
             }
             return $result;
         }
     }
 }
Пример #3
0
 private function convertLessons(Collection $lessons)
 {
     return array_map(function (Lesson $lesson) {
         return LessonFactory::createUpdateCommand($lesson);
     }, $lessons->toArray());
 }