From 41079a29a824d3b6354820e0f67d64872c465933 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 18 Jan 2017 04:06:55 +0300 Subject: [PATCH] RestoreRemovedUsersCommand added. --- .../Command/RestoreRemovedUsersCommand.php | 102 ++++++++++++++++++ .../Bundle/PointToolsBundle/Entity/User.php | 5 + .../Resources/config/services.yml | 7 ++ .../Service/Api/AbstractApi.php | 30 ++++++ 4 files changed, 144 insertions(+) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Command/RestoreRemovedUsersCommand.php diff --git a/src/Skobkin/Bundle/PointToolsBundle/Command/RestoreRemovedUsersCommand.php b/src/Skobkin/Bundle/PointToolsBundle/Command/RestoreRemovedUsersCommand.php new file mode 100644 index 0000000..a5efac4 --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Command/RestoreRemovedUsersCommand.php @@ -0,0 +1,102 @@ +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(); + } +} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index 69f0a06..d1ac1d9 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -187,4 +187,9 @@ class User { $this->removed = true; } + + public function restore(): void + { + $this->removed = false; + } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index e58fb82..56e737f 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -64,6 +64,13 @@ services: tags: - { name: console.command } - { name: monolog.logger, channel: subscribers_update } + # Restore users removed by error + app.point.restore_users: + class: Skobkin\Bundle\PointToolsBundle\Command\RestoreRemovedUsersCommand + calls: + - [setDependencies, ['@logger', '@doctrine.orm.entity_manager', '@app.point.user_repository', '@app.point.api_user', '%point_api_delay%']] + tags: + - { name: console.command } # Webhook management app.telegram.webhook_command: class: Skobkin\Bundle\PointToolsBundle\Command\TelegramWebHookCommand diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/AbstractApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/AbstractApi.php index eec91f7..454ed38 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Api/AbstractApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Api/AbstractApi.php @@ -136,10 +136,40 @@ class AbstractApi return $response; } catch (TransferException $e) { + $this->processTransferException($e); + throw new NetworkException('Request error', $e->getCode(), $e); } } + /** + * @todo refactor with $this->checkResponse() + * + * @param \Exception $e + * + * @throws ForbiddenException + * @throws NotFoundException + * @throws ServerProblemException + * @throws UnauthorizedException + */ + private function processTransferException(\Exception $e): void + { + switch ($e->getCode()) { + case SymfonyResponse::HTTP_UNAUTHORIZED: + throw new UnauthorizedException('Unauthorized', SymfonyResponse::HTTP_UNAUTHORIZED, $e); + case SymfonyResponse::HTTP_NOT_FOUND: + throw new NotFoundException('Resource not found', SymfonyResponse::HTTP_NOT_FOUND, $e); + case SymfonyResponse::HTTP_FORBIDDEN: + throw new ForbiddenException('Forbidden', SymfonyResponse::HTTP_FORBIDDEN, $e); + case SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR: + case SymfonyResponse::HTTP_NOT_IMPLEMENTED: + case SymfonyResponse::HTTP_BAD_GATEWAY: + case SymfonyResponse::HTTP_SERVICE_UNAVAILABLE: + case SymfonyResponse::HTTP_GATEWAY_TIMEOUT: + throw new ServerProblemException('Server error', SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR, $e); + } + } + /** * @throws ForbiddenException * @throws NotFoundException