-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSynchronizeSubscribersCommand.php
More file actions
145 lines (123 loc) · 4.07 KB
/
SynchronizeSubscribersCommand.php
File metadata and controls
145 lines (123 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Welp\MailchimpBundle\Command;
use DrewM\MailChimp\MailChimp;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Welp\MailchimpBundle\Provider\ListProviderInterface;
use Welp\MailchimpBundle\Subscriber\ListSynchronizer;
class SynchronizeSubscribersCommand extends Command
{
/**
* List Synchronizer.
*
* @var ListSynchronizer
*/
private ListSynchronizer $listSynchronizer;
/**
* The configured list provider.
*
* @var ListProviderInterface
*/
private ListProviderInterface $listProvider;
/**
* Mailchimp API class.
*
* @var MailChimp
*/
private MailChimp $mailchimp;
public function __construct(ListSynchronizer $listSynchronizer, ListProviderInterface $listProvider, MailChimp $mailchimp)
{
$this->listSynchronizer = $listSynchronizer;
$this->listProvider = $listProvider;
$this->mailchimp = $mailchimp;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Synchronizing subscribers in MailChimp')
->setName('welp:mailchimp:synchronize-subscribers')
->addOption(
'follow-sync',
null,
InputOption::VALUE_NONE,
'If you want to follow batches execution'
)
// @TODO add params : listId, providerServiceKey
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln(sprintf('<info>%s</info>', $this->getDescription()));
$lists = $this->listProvider->getLists();
foreach ($lists as $list) {
$output->writeln(sprintf('Synchronize list %s', $list->getListId()));
$batchesResult = $this->listSynchronizer->synchronize($list);
if ($input->getOption('follow-sync')) {
while (!$this->batchesFinished($batchesResult)) {
$batchesResult = $this->refreshBatchesResult($batchesResult);
foreach ($batchesResult as $key => $batch) {
$output->writeln($this->displayBatchInfo($batch));
}
sleep(2);
}
}
}
return Command::SUCCESS;
}
/**
* Refresh all batch from MailChimp API.
*
* @param array $batchesResult
*
* @return array
*/
private function refreshBatchesResult(array $batchesResult): array
{
$refreshedBatchsResults = [];
foreach ($batchesResult as $key => $batch) {
$batch = $this->mailchimp->get('batches/'.$batch['id']);
$refreshedBatchsResults[] = $batch;
}
return $refreshedBatchsResults;
}
/**
* Test if all batches are finished.
*
* @param array $batchesResult
*
* @return bool
*/
private function batchesFinished(array $batchesResult): bool
{
$allfinished = true;
foreach ($batchesResult as $key => $batch) {
if ('finished' !== $batch['status']) {
$allfinished = false;
}
}
return $allfinished;
}
/**
* Pretty display of batch info.
*
* @param array $batch
*
* @return string
*/
private function displayBatchInfo(array $batch): string
{
if ('finished' === $batch['status']) {
return sprintf('batch %s is finished, operations %d/%d with %d errors. http responses: %s', $batch['id'], $batch['finished_operations'], $batch['total_operations'], $batch['errored_operations'], $batch['response_body_url']);
}
return sprintf('batch %s, current status %s, operations %d/%d with %d errors', $batch['id'], $batch['status'], $batch['finished_operations'], $batch['total_operations'], $batch['errored_operations']);
}
}