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
|
|
|
class SyncStoresCommand extends BaseSyncCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ChannelRepositoryInterface |
19
|
|
|
*/ |
20
|
|
|
protected $channelRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var StoreRegisterHandlerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $storeRegisterHandler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
29
|
|
|
* @param StoreRegisterHandlerInterface $storeRegisterHandler |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
ChannelRepositoryInterface $channelRepository, |
33
|
|
|
StoreRegisterHandlerInterface $storeRegisterHandler |
34
|
|
|
) { |
35
|
|
|
parent::__construct(); |
36
|
|
|
|
37
|
|
|
$this->channelRepository = $channelRepository; |
38
|
|
|
$this->storeRegisterHandler = $storeRegisterHandler; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->setName('odiseo:mailchimp:sync-stores') |
48
|
|
|
->setDescription('Synchronize the Sylius stores (channels) to Mailchimp.') |
49
|
|
|
->addOption('purge', 'p', InputOption::VALUE_NONE, 'Remove all stores before create the new ones.') |
50
|
|
|
->addOption('isSyncing', 's', InputOption::VALUE_NONE, 'Mark the stores with "is_syncing" field like true.') |
51
|
|
|
; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
$this->io = new SymfonyStyle($input, $output); |
60
|
|
|
|
61
|
|
|
$this->io->title('Synchronizing the stores to Mailchimp'); |
62
|
|
|
|
63
|
|
|
$this->registerStores($input); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param InputInterface $input |
68
|
|
|
*/ |
69
|
|
|
protected function registerStores(InputInterface $input) |
70
|
|
|
{ |
71
|
|
|
$withPurge = $input->getOption('purge'); |
72
|
|
|
$isSyncing = $input->getOption('isSyncing'); |
73
|
|
|
|
74
|
|
|
$channels = $this->channelRepository->findBy([ |
75
|
|
|
'enabled' => true, |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
/** @var ChannelInterface $channel */ |
79
|
|
|
foreach ($channels as $channel) { |
80
|
|
|
if ($withPurge) { |
81
|
|
|
$this->io->write('Removing the "' . $channel->getName() . '" store...'); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$this->storeRegisterHandler->unregister($channel); |
85
|
|
|
$this->io->writeln('Done.'); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
$this->io->writeln('Error.'); |
88
|
|
|
$this->io->error($e->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->io->write('Connecting the "' . $channel->getName() . '" store with is_syncing = '.($isSyncing?'true':'false').'...'); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$response = $this->storeRegisterHandler->register($channel, $isSyncing); |
96
|
|
|
|
97
|
|
|
if (isset($response['id'])) { |
98
|
|
|
$this->io->writeln('Done.'); |
99
|
|
|
} else { |
100
|
|
|
$this->io->writeln('Error.'); |
101
|
|
|
if ($response !== false) { |
102
|
|
|
$this->showError($response); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
$this->io->writeln('Error.'); |
107
|
|
|
$this->io->error($e->getMessage()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->io->success('The stores has been synchronized successfully.'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|