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