|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sylake\AkeneoProducerBundle\Command; |
| 4 | + |
| 5 | +use Pim\Component\Catalog\Model\ProductInterface; |
| 6 | +use Pim\Component\Catalog\Query\ProductQueryBuilderFactoryInterface; |
| 7 | +use Sylake\AkeneoProducerBundle\Connector\Projector\ItemProjector; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +final class ExportProductCommand extends Command |
| 14 | +{ |
| 15 | + /** @var ProductQueryBuilderFactoryInterface */ |
| 16 | + private $productQueryBuilderFactory; |
| 17 | + |
| 18 | + /** @var ItemProjector */ |
| 19 | + private $itemProjector; |
| 20 | + |
| 21 | + /** |
| 22 | + * @param ProductQueryBuilderFactoryInterface $productQueryBuilderFactory |
| 23 | + * @param ItemProjector $itemProjector |
| 24 | + */ |
| 25 | + public function __construct(ProductQueryBuilderFactoryInterface $productQueryBuilderFactory, ItemProjector $itemProjector) |
| 26 | + { |
| 27 | + $this->productQueryBuilderFactory = $productQueryBuilderFactory; |
| 28 | + $this->itemProjector = $itemProjector; |
| 29 | + |
| 30 | + parent::__construct('sylake:producer:export-product'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + protected function configure() |
| 37 | + { |
| 38 | + $this |
| 39 | + ->addArgument('sku', InputArgument::REQUIRED) |
| 40 | + ; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 47 | + { |
| 48 | + $sku = $input->getArgument('sku'); |
| 49 | + |
| 50 | + $productQueryBuilder = $this->productQueryBuilderFactory->create(); |
| 51 | + $productQueryBuilder->addFilter('sku', '=', $sku); |
| 52 | + |
| 53 | + $products = $productQueryBuilder->execute(); |
| 54 | + |
| 55 | + if (count($products) === 0) { |
| 56 | + $output->writeln(sprintf('<error>Could not find product with SKU "%s"!</error>', $sku)); |
| 57 | + |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + /** @var ProductInterface $product */ |
| 62 | + foreach ($products as $product) { |
| 63 | + $output->writeln(sprintf('Exporting product with SKU "%s".', $sku)); |
| 64 | + |
| 65 | + $this->itemProjector->__invoke($product); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments