1
0
mirror of synced 2025-01-26 10:11:41 +03:00
DeliveryModuleBundle/Command/UpdateModuleCommand.php

126 lines
3.3 KiB
PHP
Raw Normal View History

2019-12-26 17:47:33 +03:00
<?php
namespace RetailCrm\DeliveryModuleBundle\Command;
use Doctrine\Persistence\ObjectManager;
2019-12-26 17:47:33 +03:00
use RetailCrm\DeliveryModuleBundle\Service\AccountManager;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateModuleCommand extends Command
{
use LockableTrait;
const QUERY_MAX_RESULTS = 100;
2019-12-26 17:47:33 +03:00
/**
* @var ModuleManagerInterface
*/
private $moduleManager;
/**
* @var AccountManager
*/
private $accountManager;
/**
* @var ObjectManager
*/
private $entityManager;
2019-12-26 17:47:33 +03:00
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('module:update')
->setDescription('Update module')
->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all')
;
2019-12-26 17:47:33 +03:00
}
public function __construct(ModuleManagerInterface $moduleManager, AccountManager $accountManager, ObjectManager $entityManager)
2019-12-26 17:47:33 +03:00
{
$this->moduleManager = $moduleManager;
$this->accountManager = $accountManager;
$this->entityManager = $entityManager;
2019-12-26 17:47:33 +03:00
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$this->lock()) {
$output->writeln('The command is already running in another process.');
return 0;
}
$accountId = $input->getArgument('accountId')
2022-08-01 12:56:48 +03:00
?: null;
2019-12-26 17:47:33 +03:00
$accountQueryBuilder = $this->accountManager->getActiveQueryBuilder()
->andWhere('account.id > :lastId')
->setMaxResults(static::QUERY_MAX_RESULTS)
;
2019-12-26 17:47:33 +03:00
if (null !== $accountId) {
$accountQueryBuilder
->andWhere('account.id = :accountId')
->setParameter('accountId', $accountId)
;
2019-12-26 17:47:33 +03:00
}
$accountQuery = $accountQueryBuilder->getQuery();
$commandResult = 0;
2019-12-26 17:47:33 +03:00
$count = 0;
$lastId = 0;
while (true) {
$accountQuery->setParameter('lastId', $lastId);
$result = $accountQuery->getResult();
if (empty($result)) {
break;
2019-12-26 17:47:33 +03:00
}
foreach ($result as $account) {
$lastId = $account->getId();
try {
$this->moduleManager
->setAccount($account)
->updateModuleConfiguration()
;
++$count;
} catch (\Exception $e) {
$output->writeln(
"<error>Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
$commandResult = 1;
}
}
$this->entityManager->clear();
gc_collect_cycles();
2019-12-26 17:47:33 +03:00
}
$output->writeln("<info>{$count} modules updated.</info>");
$this->release();
return $commandResult;
2019-12-26 17:47:33 +03:00
}
}