/** * @param Listing $listing * @param $description * @param array $recipients * @param array $ignoredItemsIDs * @return Listing[] * @throws \DibiException */ public function shareListing(Listing $listing, $description, array $recipients, array $ignoredItemsIDs = null) { $this->checkListingValidity($listing); Validators::assert($description, 'unicode'); $listingItems = $listing->listingItems; if (isset($ignoredItemsIDs)) { $ignoredItemsIDs = array_flip($ignoredItemsIDs); foreach ($listingItems as $key => $item) { if (array_key_exists($item->listingItemID, $ignoredItemsIDs)) { unset($listingItems[$key]); } } } $newListings = []; foreach ($recipients as $recipientID) { $newListing = clone $listing; $newListing->user = $recipientID; $newListing->description = $description; $newListing->hourlyWage = null; $newListings[] = $newListing; } try { $this->transaction->begin(); $this->listingRepository->saveListings($newListings); $items = []; foreach ($newListings as $listing) { $newItemsForListing = $this->itemService->createItemsCopies($listingItems); $newItemsForListing = $this->itemService->setListingForGivenItems($newItemsForListing, $listing); $items = array_merge($items, $newItemsForListing); if (count($items) > 120) { $this->listingItemRepository->saveListingItems($items); unset($items); $items = []; } } // save the rest items if (!empty($items)) { $this->listingItemRepository->saveListingItems($items); } $this->transaction->commit(); return $newListings; } catch (\DibiException $e) { $this->transaction->rollback(); Debugger::log($e, Debugger::ERROR); throw $e; } }