1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Command; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\ProductRegisterHandlerInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
9
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
10
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
final class SyncProductsCommand extends BaseSyncCommand |
17
|
|
|
{ |
18
|
|
|
/** @var ProductRepositoryInterface */ |
19
|
|
|
private $productRepository; |
20
|
|
|
|
21
|
|
|
/** @var ProductRegisterHandlerInterface */ |
22
|
|
|
private $productRegisterHandler; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
ProductRepositoryInterface $productRepository, |
26
|
|
|
ProductRegisterHandlerInterface $productRegisterHandler |
27
|
|
|
) { |
28
|
|
|
parent::__construct(); |
29
|
|
|
|
30
|
|
|
$this->productRepository = $productRepository; |
31
|
|
|
$this->productRegisterHandler = $productRegisterHandler; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function configure(): void |
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->setName('odiseo:mailchimp:sync-products') |
41
|
|
|
->setDescription('Synchronize the products to Mailchimp.') |
42
|
|
|
->addOption('create-only', 'c', InputOption::VALUE_NONE, 'With this option the existing products will be not updated.') |
43
|
|
|
; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
50
|
|
|
{ |
51
|
|
|
$this->io = new SymfonyStyle($input, $output); |
52
|
|
|
|
53
|
|
|
$this->io->title('Synchronizing the products to Mailchimp'); |
54
|
|
|
|
55
|
|
|
$this->registerProducts($input); |
56
|
|
|
|
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param InputInterface $input |
62
|
|
|
*/ |
63
|
|
|
protected function registerProducts(InputInterface $input): void |
64
|
|
|
{ |
65
|
|
|
$createOnly = $input->getOption('create-only'); |
66
|
|
|
|
67
|
|
|
$products = $this->productRepository->findAll(); |
68
|
|
|
|
69
|
|
|
$this->io->text('Connecting ' . count($products) . ' products.'); |
70
|
|
|
$this->io->progressStart(count($products)); |
71
|
|
|
|
72
|
|
|
/** @var ProductInterface $product */ |
73
|
|
|
foreach ($products as $product) { |
74
|
|
|
$channels = $product->getChannels(); |
75
|
|
|
|
76
|
|
|
/** @var ChannelInterface $channel */ |
77
|
|
|
foreach ($channels as $channel) { |
78
|
|
|
try { |
79
|
|
|
$response = $this->productRegisterHandler->register($product, $channel, $createOnly); |
80
|
|
|
|
81
|
|
|
if (!isset($response['id']) && $response !== false) { |
82
|
|
|
$this->showError($response); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->io->progressAdvance(1); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
$this->io->error($e->getMessage()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->io->progressFinish(); |
93
|
|
|
$this->io->success('The products has been synchronized successfully.'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|