From dd730f15ba484537ee37f8af9e2c1fde84dd5f09 Mon Sep 17 00:00:00 2001 From: pierallard Date: Mon, 22 Feb 2016 15:05:22 +0100 Subject: [PATCH] Move media function into ProductProcessor --- features/Context/FixturesContext.php | 14 ++++++---- .../CsvSerializer/ProductProcessor.php | 28 ++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/features/Context/FixturesContext.php b/features/Context/FixturesContext.php index edd2a4d1de7b..f27b3f8ef959 100644 --- a/features/Context/FixturesContext.php +++ b/features/Context/FixturesContext.php @@ -12,6 +12,7 @@ use League\Flysystem\MountManager; use Oro\Bundle\UserBundle\Entity\Role; use Pim\Behat\Context\FixturesContext as BaseFixturesContext; +use Pim\Bundle\CatalogBundle\AttributeType\AttributeTypes; use Pim\Bundle\CatalogBundle\Doctrine\Common\Saver\ProductSaver; use Pim\Bundle\CatalogBundle\Entity\AssociationType; use Pim\Bundle\CatalogBundle\Entity\AttributeOption; @@ -1265,12 +1266,15 @@ public function iSetProductFamilyTo($identifier, $family) public function iDeleteProductMediaFromFilesystem($productName) { $product = $this->getProduct($productName); - $allMedia = $product->getMedia(); $mountManager = $this->getMountManager(); - foreach ($allMedia as $media) { - if (null !== $media) { - $fs = $mountManager->getFilesystem($media->getStorage()); - $fs->delete($media->getKey()); + + foreach ($product->getValues() as $value) { + if (in_array($value->getAttribute()->getAttributeType(), [AttributeTypes::IMAGE, AttributeTypes::FILE])) { + $media = $value->getData(); + if (null !== $media) { + $fs = $mountManager->getFilesystem($media->getStorage()); + $fs->delete($media->getKey()); + } } } } diff --git a/src/Pim/Bundle/BaseConnectorBundle/Processor/CsvSerializer/ProductProcessor.php b/src/Pim/Bundle/BaseConnectorBundle/Processor/CsvSerializer/ProductProcessor.php index 571c5e8858b8..59c9c63a6f64 100644 --- a/src/Pim/Bundle/BaseConnectorBundle/Processor/CsvSerializer/ProductProcessor.php +++ b/src/Pim/Bundle/BaseConnectorBundle/Processor/CsvSerializer/ProductProcessor.php @@ -2,7 +2,10 @@ namespace Pim\Bundle\BaseConnectorBundle\Processor\CsvSerializer; +use Akeneo\Component\FileStorage\Model\FileInfoInterface; +use Pim\Bundle\CatalogBundle\AttributeType\AttributeTypes; use Pim\Bundle\CatalogBundle\Manager\ChannelManager; +use Pim\Component\Catalog\Model\ProductInterface; use Pim\Component\Catalog\Repository\LocaleRepositoryInterface; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Validator\Constraints as Assert; @@ -95,7 +98,7 @@ public function process($products) $media = []; foreach ($products as $product) { - $media = array_merge($product->getMedia(), $media); + $media = array_merge($this->getProductMedia($product), $media); } return [ @@ -139,4 +142,27 @@ protected function getLocaleCodes($channelCode) return $channel->getLocaleCodes(); } + + + /** + * Get all the media of the product + * + * @param ProductInterface $product + * + * @return FileInfoInterface[] + */ + public function getProductMedia(ProductInterface $product) + { + $media = []; + foreach ($product->getValues() as $value) { + if (in_array( + $value->getAttribute()->getAttributeType(), + [AttributeTypes::IMAGE, AttributeTypes::FILE] + )) { + $media[] = $value->getData(); + } + } + + return $media; + } }