WIP: Symfony 6 project remake #2
|
@ -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');
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue