setName('statuses:update') ->setDescription('Update statuses') ->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all'); } public function __construct(EntityManagerInterface $em, ModuleManager $moduleManager) { $this->em = $em; $this->moduleManager = $moduleManager; 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') ? (int) $input->getArgument('accountId') : null; $paginator = []; if (null !== $accountId) { $paginator = [$this->em->getRepository($this->getConnectionClass())->find($accountId)]; } else { $accountQuery = $this->em->createQuery(' SELECT account FROM ' . Account::class . ' account WHERE account.isActive = true AND account.isFreeze != true ') ->setFirstResult(0) ->setMaxResults(100); $paginator = new Paginator($accountQuery); } $count = 0; foreach ($paginator as $account) { try { $count += $this->moduleManager ->setAccount($account) ->updateStatuses() ; } catch (\Exception $e) { $output->writeln( "Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]" ); $output->writeln("Error: {$e->getMessage()}"); } } $output->writeln(" {$count} statuses updated."); $this->release(); } }