1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusMailchimpPlugin\Command; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusMailchimpPlugin\Handler\OrderRegisterHandlerInterface; |
8
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
9
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
10
|
|
|
use Sylius\Component\Core\OrderPaymentStates; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
final class SyncOrdersCommand extends BaseSyncCommand |
17
|
|
|
{ |
18
|
|
|
/** @var EntityRepository */ |
19
|
|
|
private $orderRepository; |
20
|
|
|
|
21
|
|
|
/** @var OrderRegisterHandlerInterface */ |
22
|
|
|
private $orderRegisterHandler; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
EntityRepository $orderRepository, |
26
|
|
|
OrderRegisterHandlerInterface $orderRegisterHandler |
27
|
|
|
) { |
28
|
|
|
parent::__construct(); |
29
|
|
|
|
30
|
|
|
$this->orderRepository = $orderRepository; |
31
|
|
|
$this->orderRegisterHandler = $orderRegisterHandler; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function configure(): void |
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->setName('odiseo:mailchimp:sync-orders') |
41
|
|
|
->setDescription('Synchronize the orders to Mailchimp.') |
42
|
|
|
->addOption('create-only', 'c', InputOption::VALUE_NONE, 'With this option the existing carts will be not updated.') |
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 orders to Mailchimp'); |
54
|
|
|
|
55
|
|
|
$this->registerOrders($input); |
56
|
|
|
|
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param InputInterface $input |
62
|
|
|
*/ |
63
|
|
|
protected function registerOrders(InputInterface $input): void |
64
|
|
|
{ |
65
|
|
|
$createOnly = $input->getOption('create-only'); |
66
|
|
|
|
67
|
|
|
$orders = $this->orderRepository->createQueryBuilder('o') |
68
|
|
|
->andWhere('o.paymentState = :paymentState') |
69
|
|
|
->setParameter('paymentState', OrderPaymentStates::STATE_PAID) |
70
|
|
|
->getQuery() |
71
|
|
|
->getResult() |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
$this->io->text('Connecting ' . count($orders) . ' carts.'); |
|
|
|
|
75
|
|
|
$this->io->progressStart(count($orders)); |
76
|
|
|
|
77
|
|
|
/** @var OrderInterface $order */ |
78
|
|
|
foreach ($orders as $order) { |
79
|
|
|
try { |
80
|
|
|
$response = $this->orderRegisterHandler->register($order, $createOnly); |
81
|
|
|
|
82
|
|
|
if (!isset($response['id']) && $response !== false) { |
83
|
|
|
$this->showError($response); |
84
|
|
|
} |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
$this->io->error($e->getMessage()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->io->progressAdvance(1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->io->progressFinish(); |
93
|
|
|
$this->io->success('The orders has been synchronized successfully.'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|