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\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
class SyncProductsCommand extends BaseSyncCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ProductRepositoryInterface |
19
|
|
|
*/ |
20
|
|
|
protected $productRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ProductRegisterHandlerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $productRegisterHandler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ProductRepositoryInterface $productRepository |
29
|
|
|
* @param ProductRegisterHandlerInterface $productRegisterHandler |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
ProductRepositoryInterface $productRepository, |
33
|
|
|
ProductRegisterHandlerInterface $productRegisterHandler |
34
|
|
|
) { |
35
|
|
|
parent::__construct(); |
36
|
|
|
|
37
|
|
|
$this->productRepository = $productRepository; |
38
|
|
|
$this->productRegisterHandler = $productRegisterHandler; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->setName('odiseo:mailchimp:sync-products') |
48
|
|
|
->setDescription('Synchronize the products to Mailchimp.') |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$this->io = new SymfonyStyle($input, $output); |
58
|
|
|
|
59
|
|
|
$this->io->title('Synchronizing the products to Mailchimp'); |
60
|
|
|
|
61
|
|
|
$this->registerProducts(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function registerProducts() |
65
|
|
|
{ |
66
|
|
|
$products = $this->productRepository->findAll(); |
67
|
|
|
|
68
|
|
|
$this->io->text('Connecting ' . count($products) . ' products.'); |
69
|
|
|
$this->io->progressStart(count($products)); |
70
|
|
|
|
71
|
|
|
/** @var ProductInterface $product */ |
72
|
|
|
foreach ($products as $product) { |
73
|
|
|
$channels = $product->getChannels(); |
74
|
|
|
|
75
|
|
|
/** @var ChannelInterface $channel */ |
76
|
|
|
foreach ($channels as $channel) { |
77
|
|
|
try { |
78
|
|
|
$response = $this->productRegisterHandler->register($product, $channel); |
79
|
|
|
|
80
|
|
|
if (!isset($response['id']) && $response !== false) { |
81
|
|
|
$this->showError($response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->io->progressAdvance(1); |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
$this->io->error($e->getMessage()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->io->progressFinish(); |
92
|
|
|
$this->io->success('The products has been synchronized successfully.'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|