1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Command; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\StoreRegisterHandlerInterface; |
8
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
9
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
final class SyncStoresCommand extends BaseSyncCommand |
16
|
|
|
{ |
17
|
|
|
/** @var ChannelRepositoryInterface */ |
18
|
|
|
private $channelRepository; |
19
|
|
|
|
20
|
|
|
/** @var StoreRegisterHandlerInterface */ |
21
|
|
|
private $storeRegisterHandler; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
ChannelRepositoryInterface $channelRepository, |
25
|
|
|
StoreRegisterHandlerInterface $storeRegisterHandler |
26
|
|
|
) { |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->channelRepository = $channelRepository; |
30
|
|
|
$this->storeRegisterHandler = $storeRegisterHandler; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function configure(): void |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName('odiseo:mailchimp:sync-stores') |
40
|
|
|
->setDescription('Synchronize the Sylius stores (channels) to Mailchimp.') |
41
|
|
|
->addOption('purge', 'p', InputOption::VALUE_NONE, 'Remove all stores before create the new ones.') |
42
|
|
|
->addOption('isSyncing', 's', InputOption::VALUE_NONE, 'Mark the stores with "is_syncing" field like true.') |
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 stores to Mailchimp'); |
54
|
|
|
|
55
|
|
|
$this->registerStores($input); |
56
|
|
|
|
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param InputInterface $input |
62
|
|
|
*/ |
63
|
|
|
protected function registerStores(InputInterface $input): void |
64
|
|
|
{ |
65
|
|
|
$withPurge = $input->getOption('purge'); |
66
|
|
|
$isSyncing = $input->getOption('isSyncing'); |
67
|
|
|
|
68
|
|
|
$channels = $this->channelRepository->findBy([ |
69
|
|
|
'enabled' => true, |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
/** @var ChannelInterface $channel */ |
73
|
|
|
foreach ($channels as $channel) { |
74
|
|
|
if ($withPurge) { |
75
|
|
|
$this->io->write('Removing the "' . $channel->getName() . '" store...'); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
$this->storeRegisterHandler->unregister($channel); |
79
|
|
|
$this->io->writeln('Done.'); |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
$this->io->writeln('Error.'); |
82
|
|
|
$this->io->error($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->io->write('Connecting the "' . $channel->getName() . '" store with is_syncing = '.($isSyncing?'true':'false').'...'); |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
$response = $this->storeRegisterHandler->register($channel, $isSyncing); |
90
|
|
|
|
91
|
|
|
if (isset($response['id'])) { |
92
|
|
|
$this->io->writeln('Done.'); |
93
|
|
|
} else { |
94
|
|
|
$this->io->writeln('Error.'); |
95
|
|
|
if ($response !== false) { |
96
|
|
|
$this->showError($response); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$this->io->writeln('Error.'); |
101
|
|
|
$this->io->error($e->getMessage()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->io->success('The stores has been synchronized successfully.'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|