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