Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.

Commit d6d7999

Browse files
authored
Merge pull request #50 from pamil/export-product-command
Implement product export command
2 parents 3ed8392 + 0645dc4 commit d6d7999

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ Usage
5858
$ app/console akeneo:batch:create-job "Sylake - Akeneo Producer" sylake_akeneo_producer export sylake_akeneo_producer
5959
$ app/console akeneo:batch:job sylake_akeneo_producer
6060
```
61+
62+
You can also export just one product by its SKU by running:
63+
64+
```bash
65+
$ app/console sylake:producer:export-product PRODUCT_SKU
66+
```

src/Command/ExportProductCommand.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
33
<imports>
4+
<import resource="services/command.xml" />
45
<import resource="services/export.xml" />
56
<import resource="services/listener.xml" />
67
</imports>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<service class="Sylake\AkeneoProducerBundle\Command\ExportProductCommand">
6+
<argument type="service" id="pim_catalog.query.product_query_builder_factory" />
7+
<argument type="service" id="sylake_akeneo_producer.connector.item_projector" />
8+
<tag name="console.command" />
9+
</service>
10+
</services>
11+
</container>

0 commit comments

Comments
 (0)