/**
  * Upload new attachment
  *
  * @param UploadedFile $file
  * @param string       $descriptionText
  * @param Language     $language
  * @param array        $attributes
  * @param Attachment   $attachment
  *
  * @return Attachment
  */
 public function upload(UploadedFile $file, $descriptionText, Language $language, array $attributes, Attachment $attachment = null)
 {
     $filesystem = new Filesystem();
     $filesize = $file->getClientSize();
     if ($filesize == false) {
         throw new FileException('File size is not valid');
     }
     if (!file_exists($this->config['file_directory']) || !is_writable($this->config['file_directory'])) {
         throw new FileException('Directory ' . $this->config['file_directory'] . ' is not writable');
     }
     if (!is_null($attachment)) {
         if ($filesystem->exists($this->getStorageLocation($attachment))) {
             $filesystem->remove($this->getStorageLocation($attachment));
         }
         if ($descriptionText != $attachment->getDescription()->getTranslationText()) {
             $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
             $description = new Translation($nextTranslationPhraseId);
             $description->setLanguage($language);
             $description->setTranslationText($descriptionText);
             $this->em->persist($description);
         }
         unset($attributes['description']);
     } else {
         $attachment = new Attachment();
         $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId();
         $description = new Translation($nextTranslationPhraseId);
         $description->setLanguage($language);
         $description->setTranslationText($descriptionText);
         unset($attributes['description']);
         $attachment->setCreated(new \DateTime());
         $this->em->persist($description);
         $this->em->persist($attachment);
     }
     $attributes = array_merge(array('language' => $language, 'name' => $file->getClientOriginalName(), 'extension' => $file->getClientOriginalExtension(), 'mimeType' => $file->getClientMimeType(), 'contentDisposition' => Attachment::CONTENT_DISPOSITION, 'sizeInBytes' => $file->getClientSize(), 'description' => $description), $attributes);
     $this->fillAttachment($attachment, $attributes);
     if (is_null($attributes['name'])) {
         $attachment->setName($file->getClientOriginalName());
     }
     $this->em->flush();
     $target = $this->makeDirectories($attachment);
     try {
         $file->move($target, $this->getFileName($attachment));
         $filesystem->chmod($target . '/' . $this->getFileName($attachment), 0644);
     } catch (\Exceptiom $e) {
         $filesystem->remove($target);
         $this->em->remove($attachment);
         $this->em->flush();
         throw new \Exception($e->getMessage(), $e->getCode());
     }
     return $attachment;
 }
 public function it_should_update_attachment()
 {
     $filesystem = new Filesystem();
     $newFileName = __DIR__ . '/../../assets/images/temp-image.jpg';
     $filesystem->copy(__DIR__ . '/../../assets/images/picture.jpg', $newFileName);
     $uploadedFile = new UploadedFile($newFileName, 'temp-image.jpg', 'image/jpg', filesize($newFileName), null, true);
     $language = new Language();
     $attachment = new Attachment();
     $phraseId = 1;
     $description = new Translation($phraseId);
     $description->setLanguage($language);
     $description->setTranslationText('Description text');
     $attachment->setDescription($description);
     $this->upload($uploadedFile, 'Test file - updated', $language, array(), $attachment);
 }