/** * Writes file to a DB and moves it to an appropriate path * * @param FileUpload $form * @return FileEntity * @throws \Exception */ public function writeFile(FileUpload $form) { $ext = $this->getExt($form->getData()[$form->getFileType()]['name']); $type = $form->getFileType(); //Creating new image to get ID for building its path $file = new FileEntity(); $file->setExtension($ext); $file->setType($type); $this->sm->get('doctrine.entitymanager.orm_default')->persist($file); $this->sm->get('doctrine.entitymanager.orm_default')->flush(); //Building path and creating directory. Then - moving $destination = null; switch ($type) { case FileEntity::AUDIO_FILETYPE: $destination = Audio::audioPath($file->getId(), $ext); break; case FileEntity::VIDEO_FILETYPE: $destination = Video::videoPath($file->getId(), $ext); break; case FileEntity::IMAGE_FILETYPE: $destination = Image::imgPath(Image::ORIGINAL, $file->getId(), $ext); break; default: } $this->prepareDir($destination); $this->moveFile($destination, $form->getData()[$type]); return $file; }