RestoreRemovedUsersCommand added.
This commit is contained in:
parent
3336bf3502
commit
41079a29a8
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace Skobkin\Bundle\PointToolsBundle\Command;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||
use Skobkin\Bundle\PointToolsBundle\Exception\Api\UserNotFoundException;
|
||||
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
|
||||
use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class RestoreRemovedUsersCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $userRepo;
|
||||
|
||||
/**
|
||||
* @var UserApi
|
||||
*/
|
||||
private $userApi;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $delay;
|
||||
|
||||
public function setDependencies(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $delay): void
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
|
@ -187,4 +187,9 @@ class User
|
|||
{
|
||||
$this->removed = true;
|
||||
}
|
||||
|
||||
public function restore(): void
|
||||
{
|
||||
$this->removed = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue