/** * Sets the medias of the given contact to the given medias. * Currently associated medias are replaced. * * @param Contact $contact * @param $medias * * @throws EntityNotFoundException */ private function setMedias(Contact $contact, $medias) { $mediaIds = array_map(function ($media) { return $media['id']; }, $medias); $foundMedias = $this->mediaRepository->findById($mediaIds); $foundMediaIds = array_map(function ($mediaEntity) { return $mediaEntity->getId(); }, $foundMedias); if ($missingMediaIds = array_diff($mediaIds, $foundMediaIds)) { throw new EntityNotFoundException($this->mediaRepository->getClassName(), reset($missingMediaIds)); } $contact->getMedias()->clear(); foreach ($foundMedias as $media) { $contact->addMedia($media); } }