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