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