/** * Sets the medias of the given account to the given medias. * Currently associated medias are replaced. * * @param Account $account * @param $medias * * @throws EntityNotFoundException */ public function setMedias(Account $account, $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)); } $account->getMedias()->clear(); foreach ($foundMedias as $media) { $account->addMedia($media); } }