public function search(SearchFilter $filter, $page = 1, $limit = 10) { // get workshop and lessons filtered separately $workshops = $this->workshopRepository->findForCriteria($filter); $lessons = $this->lessonsRepository->findForCriteria($filter); // get all lessons for found workshops if (count($workshops) > 0) { $lessonsFromWorkshops = $this->lessonsRepository->findByWorkshopIds(array_unique(array_column($workshops, "id"))); $lessons = array_unique(array_merge($lessons, $lessonsFromWorkshops), SORT_REGULAR); } // get all workshops for found lessons if (count($lessons) > 0) { $workshopsFromLessons = $this->workshopRepository->findByIds(array_unique(array_column($lessons, "workshop_id"))); $workshops = array_unique(array_merge($workshops, $workshopsFromLessons), SORT_REGULAR); } // join of the lessons and workshops $result = array_map(function ($workshopArray) use($lessons) { $workshopArray["lessons"] = array_values(array_filter($lessons, function ($lessonArray) use($workshopArray) { return $lessonArray["workshop_id"] == $workshopArray["id"]; })); return $workshopArray; }, $workshops); $this->pagination = $this->paginator->paginate($result, $page, $limit); $this->results = $result; return $this; }
/** * @param UpdateWorkshop $command * @throws Exception */ public function handle(UpdateWorkshop $command) { /** @var Workshop $workshop */ $workshop = $this->workshopRepository->findOneById($command->id); $workshop->updateWithCommand($command); $workshop->updateSlug($this->slugGenerator->generate($workshop)); $command->lessons; foreach ($workshop->getLessons() as $existingLesson) { if (!in_array($existingLesson, $command->lessons)) { $this->lessonRepository->remove($existingLesson); $workshop->getLessons()->removeElement($existingLesson); } } $this->workshopRepository->update($workshop); }
/** * @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); }