Ported RestoreRemovedUsersCommand.

This commit is contained in:
Alexey Skobkin 2023-03-19 18:50:25 +03:00
parent 4cb8e715bf
commit 405b8a9f59
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 20 additions and 47 deletions

View file

@ -57,7 +57,6 @@ class ImportUsersCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$fs = new Filesystem(); $fs = new Filesystem();
$fileName = $input->getArgument('file'); $fileName = $input->getArgument('file');

View file

@ -1,67 +1,40 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Command; namespace App\Command;
use App\Exception\Api\UserNotFoundException;
use App\Repository\UserRepository;
use App\Service\Api\UserApi;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use src\PointToolsBundle\Entity\User; use Symfony\Component\Console\Attribute\AsCommand;
use src\PointToolsBundle\Exception\Api\UserNotFoundException;
use src\PointToolsBundle\Repository\UserRepository;
use src\PointToolsBundle\Service\Api\UserApi;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'app:users:restore', description: 'Check removed users status and restore if user was deleted by error.')]
class RestoreRemovedUsersCommand extends Command class RestoreRemovedUsersCommand extends Command
{ {
/** @var LoggerInterface */ public function __construct(
private $logger; private readonly LoggerInterface $logger,
private readonly EntityManagerInterface $em,
/** @var EntityManagerInterface */ private readonly UserRepository $userRepo,
private $em; private readonly UserApi $userApi,
private readonly int $apiDelay,
/** @var UserRepository */ ) {
private $userRepo;
/** @var UserApi */
private $userApi;
/** @var int */
private $delay;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em, UserRepository $userRepo, UserApi $userApi, int $apiDelay)
{
parent::__construct(); parent::__construct();
$this->logger = $logger;
$this->em = $em;
$this->userRepo = $userRepo;
$this->userApi = $userApi;
$this->delay = $apiDelay;
} }
/** protected function execute(InputInterface $input, OutputInterface $output): int
* {@inheritdoc}
*/
protected function configure()
{ {
$this $io = new SymfonyStyle($input, $output);
->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) { foreach ($this->userRepo->findBy(['removed' => true]) as $removedUser) {
usleep($this->delay); \usleep($this->apiDelay);
try { try {
/** @var User $remoteUser */
$remoteUser = $this->userApi->getUserById($removedUser->getId()); $remoteUser = $this->userApi->getUserById($removedUser->getId());
if ($remoteUser->getId() === $removedUser->getId()) { if ($remoteUser->getId() === $removedUser->getId()) {
@ -70,7 +43,6 @@ class RestoreRemovedUsersCommand extends Command
'login' => $removedUser->getLogin(), 'login' => $removedUser->getLogin(),
]); ]);
$removedUser->restore(); $removedUser->restore();
$this->em->flush(); $this->em->flush();
} }
} catch (UserNotFoundException $e) { } catch (UserNotFoundException $e) {
@ -93,5 +65,7 @@ class RestoreRemovedUsersCommand extends Command
} }
$this->em->flush(); $this->em->flush();
return Command::SUCCESS;
} }
} }