WIP: Symfony 6 project remake #2

Draft
skobkin wants to merge 103 commits from symfony6_remake into master
Showing only changes of commit 3d0b2bdbc8 - Show all commits

View file

@ -1,66 +1,40 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Command; namespace App\Command;
use App\Entity\User;
use App\Exception\Api\ForbiddenException;
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\ForbiddenException;
use src\PointToolsBundle\Entity\{Subscription};
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\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\{InputInterface, InputOption}; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(name: 'app:privacy:update', description: 'Check removed users status and restore if user was deleted by error.')]
class UpdateUsersPrivacyCommand extends Command class UpdateUsersPrivacyCommand extends Command
{ {
/** @var EntityManagerInterface */ public function __construct(
private $em; private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
/** @var LoggerInterface */ private readonly UserRepository $userRepo,
private $logger; private readonly UserApi $api,
private readonly int $apiDelay,
/** @var UserRepository */ private readonly int $appUserId,
private $userRepo; ) {
/** @var InputInterface */
private $input;
/** @var UserApi */
private $api;
/** @var int */
private $apiDelay = 500000;
/** @var int */
private $appUserId;
/** @var ProgressBar */
private $progress;
public function __construct(EntityManagerInterface $em, LoggerInterface $logger, UserRepository $userRepo, UserApi $api, int $apiDelay, int $appUserId)
{
parent::__construct(); parent::__construct();
$this->em = $em;
$this->logger = $logger;
$this->userRepo = $userRepo;
$this->api = $api;
$this->apiDelay = $apiDelay;
$this->appUserId = $appUserId;
} }
/**
* {@inheritdoc}
*/
protected function configure() protected function configure()
{ {
$this $this
->setName('point:update:privacy')
->setDescription('Update users privacy')
->addOption( ->addOption(
'all-users', 'all-users',
null, null,
@ -70,47 +44,43 @@ class UpdateUsersPrivacyCommand extends Command
; ;
} }
/** protected function execute(InputInterface $input, OutputInterface $output): int
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->input = $input; $io = new SymfonyStyle($input, $output);
$this->logger->debug(static::class.' started.'); $this->logger->debug(static::class.' started.');
$this->progress = new ProgressBar($output); $progress = $io->createProgressBar();
$this->progress->setFormat('debug'); $progress->setFormat(ProgressBar::FORMAT_DEBUG);
try { try {
/** @var User[] $usersForUpdate */ $usersForUpdate = $this->getUsersForUpdate($input);
$usersForUpdate = $this->getUsersForUpdate();
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]); $this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]);
return 1; return Command::FAILURE;
} }
$this->logger->info('Processing users privacy.'); $this->logger->info('Processing users privacy.');
$this->progress->start(count($usersForUpdate)); $progress->start(count($usersForUpdate));
foreach ($usersForUpdate as $idx => $user) { foreach ($usersForUpdate as $user) {
usleep($this->apiDelay); usleep($this->apiDelay);
$this->progress->advance(); $progress->advance();
$this->logger->info('Processing @'.$user->getLogin()); $this->logger->info('Processing @'.$user->getLogin());
$this->updateUser($user); $this->updateUser($user);
} }
$this->progress->finish(); $progress->finish();
$this->em->flush(); $this->em->flush();
$this->logger->debug('Finished'); $this->logger->debug('Finished');
return 0; return Command::SUCCESS;
} }
private function updateUser(User $user): void private function updateUser(User $user): void
@ -150,9 +120,10 @@ class UpdateUsersPrivacyCommand extends Command
} }
} }
private function getUsersForUpdate(): array /** @return User[] */
private function getUsersForUpdate(InputInterface $input): array
{ {
if ($this->input->getOption('all-users')) { if ($input->getOption('all-users')) {
return $this->userRepo->findBy(['removed' => false]); return $this->userRepo->findBy(['removed' => false]);
} }
@ -197,7 +168,6 @@ class UpdateUsersPrivacyCommand extends Command
$localSubscribers = []; $localSubscribers = [];
/** @var Subscription $subscription */
foreach ($serviceUser->getSubscribers() as $subscription) { foreach ($serviceUser->getSubscribers() as $subscription) {
$localSubscribers[] = $subscription->getSubscriber(); $localSubscribers[] = $subscription->getSubscriber();
} }