/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (null === $data || '' === $data) {
         return null;
     }
     if (is_file($data)) {
         return $this->storer->store(new \SplFileInfo($data), FileStorage::CATALOG_STORAGE_ALIAS);
     }
     return $this->repository->findOneByIdentifier($data);
 }
 /**
  * Handles the media of the given product template
  *
  * @param ProductTemplateInterface $template
  */
 public function handleProductTemplateMedia(ProductTemplateInterface $template)
 {
     $mediaHandled = false;
     foreach ($template->getValues() as $value) {
         if (null !== $value->getMedia() && true === $value->getMedia()->isRemoved()) {
             $mediaHandled = true;
             $value->setMedia(null);
         } elseif (null !== $value->getMedia() && null !== $value->getMedia()->getUploadedFile()) {
             $mediaHandled = true;
             $file = $this->fileStorer->store($value->getMedia()->getUploadedFile(), FileStorage::CATALOG_STORAGE_ALIAS, true);
             $value->setMedia($file);
         }
     }
     if ($mediaHandled) {
         $this->updateNormalizedValues($template);
     }
 }
 /**
  * @param ProductInterface   $fromProduct
  * @param ProductInterface   $toProduct
  * @param AttributeInterface $fromAttribute
  * @param AttributeInterface $toAttribute
  * @param string             $fromLocale
  * @param string             $toLocale
  * @param string             $fromScope
  * @param string             $toScope
  */
 protected function copySingleValue(ProductInterface $fromProduct, ProductInterface $toProduct, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope)
 {
     $fromValue = $fromProduct->getValue($fromAttribute->getCode(), $fromLocale, $fromScope);
     if (null !== $fromValue) {
         $toValue = $toProduct->getValue($toAttribute->getCode(), $toLocale, $toScope);
         if (null === $toValue) {
             $toValue = $this->productBuilder->addProductValue($toProduct, $toAttribute, $toLocale, $toScope);
         }
         $file = null;
         if (null !== $fromValue->getMedia()) {
             $filesystem = $this->mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS);
             $rawFile = $this->rawFileFetcher->fetch($filesystem, $fromValue->getMedia()->getKey());
             $file = $this->rawFileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false);
             $file->setOriginalFilename($fromValue->getMedia()->getOriginalFilename());
         }
         $toValue->setMedia($file);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function transform($value, array $options = [])
 {
     $value = trim($value);
     if (empty($value) || is_dir($value)) {
         return;
     }
     try {
         $rawFile = new File($value);
         $file = $this->storer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS);
     } catch (FileNotFoundException $e) {
         throw new PropertyTransformerException('File not found: "%value%"', ['%value%' => $value]);
     } catch (FileTransferException $e) {
         throw new PropertyTransformerException('Impossible to transfer the file "%value%"', ['%value%' => $value]);
     } catch (\Exception $e) {
         throw new PropertyTransformerException('An error occurred during the process of the file "%value%"', ['%value%' => $value]);
     }
     return $file;
 }
 /**
  * {@inheritdoc}
  *
  * Before sending configuration to the job, we store uploaded files.
  * This way, the job process can have access to uploaded files.
  */
 public function finalize()
 {
     foreach ($this->getValues() as $productValue) {
         $media = $productValue->getMedia();
         if (null !== $media && null !== $media->getUploadedFile()) {
             $file = $this->rawFileStorer->store($media->getUploadedFile(), FileStorage::CATALOG_STORAGE_ALIAS, true);
             $productValue->setMedia($file);
         }
     }
 }
 /**
  * TODO: inform the user that this could take some time
  *
  * @param AttributeInterface $attribute
  * @param mixed              $data
  *
  * @throws InvalidArgumentException If an invalid filePath is provided
  * @return FileInterface|null
  */
 protected function storeFile(AttributeInterface $attribute, $data)
 {
     if (null === $data || null === $data['filePath'] && null === $data['originalFilename']) {
         return null;
     }
     try {
         $rawFile = new UploadedFile($data['filePath'], $data['originalFilename']);
         $file = $this->storer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS);
     } catch (FileNotFoundException $e) {
         throw InvalidArgumentException::expected($attribute->getCode(), 'a valid pathname', 'setter', 'media', $data['filePath']);
     }
     return $file;
 }
 function it_copies_when_a_product_value_has_a_media_but_not_the_target_value($builder, $attrValidatorHelper, FileInterface $fromMedia, FileInterface $toMedia, \SplFileInfo $rawFile, FileInterface $file, ProductMediaInterface $fromMedia, ProductMediaInterface $toMedia, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, ProductInterface $product, ProductValueInterface $fromProductValue, RawFileStorerInterface $rawFileStorer, ProductValueInterface $toProductValue, RawFileFetcherInterface $rawFileFetcher, MountManager $mountManager, FilesystemInterface $fileSystem)
 {
     $fromLocale = 'fr_FR';
     $toLocale = 'fr_FR';
     $toScope = 'mobile';
     $fromScope = 'mobile';
     $fromAttribute->getCode()->willReturn('fromAttributeCode');
     $toAttribute->getCode()->willReturn('toAttributeCode');
     $attrValidatorHelper->validateLocale(Argument::cetera())->shouldBeCalled();
     $attrValidatorHelper->validateScope(Argument::cetera())->shouldBeCalled();
     $fromProductValue->getMedia()->willReturn($fromMedia);
     $fromMedia->getOriginalFilename()->willReturn('akeneo.jpg');
     $fromMedia->getKey()->willReturn('key');
     $mountManager->getFilesystem(FileStorage::CATALOG_STORAGE_ALIAS)->willReturn($fileSystem);
     $rawFileFetcher->fetch($fileSystem, 'key')->willReturn($rawFile);
     $rawFileStorer->store($rawFile, FileStorage::CATALOG_STORAGE_ALIAS, false)->willReturn($file);
     $product->getValue('fromAttributeCode', $fromLocale, $fromScope)->willReturn($fromProductValue);
     $product->getValue('toAttributeCode', $toLocale, $toScope)->willReturn(null);
     $builder->addProductValue($product, $toAttribute, $toLocale, $toScope)->willReturn($toProductValue);
     $toProductValue->getMedia()->willReturn($toMedia);
     $toProductValue->setMedia($file)->shouldBeCalled();
     $this->copyAttributeData($product, $product, $fromAttribute, $toAttribute, ['from_locale' => $fromLocale, 'to_locale' => $toLocale, 'from_scope' => $fromScope, 'to_scope' => $toScope]);
 }