Skip to content

Commit

Permalink
Move media function into ProductProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
pierallard committed Feb 23, 2016
1 parent ae2493c commit dd730f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 9 additions & 5 deletions features/Context/FixturesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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;
}
}

0 comments on commit dd730f1

Please sign in to comment.