/** * @param AbstractProductMedia $media * * @return void */ protected function copyMedia(AbstractProductMedia $media) { if (null === $media->getFilePath() || '' === $media->getFileName()) { return; } $result = $this->mediaManager->copy($media, dirname($this->getPath())); $exportPath = $this->mediaManager->getExportPath($media); if (true === $result) { $this->writtenFiles[sprintf('%s/%s', dirname($this->getPath()), $exportPath)] = $exportPath; } else { $this->stepExecution->addWarning($this->getName(), sprintf('Copy of "%s" failed.', $media->getFilename()), [], $media); } }
function it_returns_flat_data(Channel $channel, $channelManager, AbstractProduct $item, AbstractProductMedia $media1, AbstractProductMedia $media2, $serializer) { $media1->getFilename()->willReturn('media_name'); $media1->getFilePath()->willReturn('media_file_path'); $media1->getOriginalFilename()->willReturn('media_original_name'); $media2->getFilename()->willReturn('media_name'); $media2->getFilePath()->willReturn('media_file_path'); $media2->getOriginalFilename()->willReturn('media_original_name'); $item->getMedia()->willReturn([$media1, $media2]); $serializer->normalize($item, 'flat', ['scopeCode' => 'foobar', 'localeCodes' => ''])->willReturn(['normalized_product']); $channelManager->getChannelByCode('foobar')->willReturn($channel); $this->setChannel('foobar'); $this->process($item)->shouldReturn(['media' => [$media1, $media2], 'product' => ['normalized_product']]); }
/** * Get the export path of the media * * Examples: * - files/sku-001/front_view/en_US/ecommerce * - files/sku-002/manual/ecommerce * - files/sku-003/back_view/en_US * - files/sku-004/insurance * * @param AbstractProductMedia $media * * @return string */ public function getExportPath(AbstractProductMedia $media) { if ($media->getFilePath() === null) { return ''; } $value = $media->getValue(); $attribute = $value->getAttribute(); $target = sprintf('files/%s/%s', $value->getEntity()->getIdentifier(), $attribute->getCode()); if ($attribute->isLocalizable()) { $target .= '/' . $value->getLocale(); } if ($attribute->isScopable()) { $target .= '/' . $value->getScope(); } return $target . '/' . $media->getOriginalFilename(); }