logger = $logger; $this->em = $em; $this->userRepo = $userRepo; $this->userApi = $userApi; $this->delay = $delay; } /** * {@inheritdoc} */ protected function configure() { $this ->setName('point:users:restore') ->setDescription('Check removed users status and restore if user was deleted by error.') ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var User $removedUser */ foreach ($this->userRepo->findBy(['removed' => true]) as $removedUser) { usleep($this->delay); try { /** @var User $remoteUser */ $remoteUser = $this->userApi->getUserById($removedUser->getId()); if ($remoteUser->getId() === $removedUser->getId()) { $this->logger->info('Restoring user', [ 'id' => $removedUser->getId(), 'login' => $removedUser->getLogin(), ]); $removedUser->restore(); } } catch (UserNotFoundException $e) { $this->logger->debug('User is really removed. Keep going.', [ 'id' => $removedUser->getId(), 'login' => $removedUser->getLogin(), ]); continue; } catch (\Exception $e) { $this->logger->error('Error while trying to restore user', [ 'user_id' => $removedUser->getId(), 'user_login' => $removedUser->getLogin(), 'exception' => get_class($e), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]); } } $this->em->flush(); } }