/**
* @param Listing $listing
* @param bool $withItems
* @param array|null $valuesForNewListing
* @return Listing
* @throws \Exception
*/
public function establishListingCopy(Listing $listing, $withItems = true, array $valuesForNewListing = null)
{
Validators::assert($withItems, 'boolean');
$newListing = clone $listing;
if (isset($valuesForNewListing)) {
$newListing->setDescription($valuesForNewListing['description']);
$newListing->setHourlyWage($valuesForNewListing['hourlyWage']);
}
$this->em->persist($newListing);
if ($withItems === true) {
$copies = $this->getItemsCopies($listing);
if (!empty($copies)) {
foreach ($copies as $copy) {
$copy->setListing($newListing);
$this->em->persist($copy);
}
}
}
try {
$this->em->flush();
$this->em->clear();
} catch (\Exception $e) {
$this->onError('establishListingCopy', $e, self::class);
throw $e;
}
return $newListing;
}