Compare commits

...

3 commits

Author SHA1 Message Date
Alexey Skobkin 64855c5076
Ported UsersRenamedEvent, UserSubscribersUpdatedEvent. 2023-03-19 19:20:12 +03:00
Alexey Skobkin 405b8a9f59
Ported RestoreRemovedUsersCommand. 2023-03-19 18:50:25 +03:00
Alexey Skobkin 4cb8e715bf
Ported ImportUsersCommand. 2023-03-19 17:55:24 +03:00
6 changed files with 92 additions and 174 deletions

View file

@ -1,65 +0,0 @@
<?php
namespace src\PointToolsBundle\Event;
use src\PointToolsBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;
/**
* Dispatched when user subscribers list was changed
*/
class UserSubscribersUpdatedEvent extends Event
{
const NAME = 'app.user.subscribers_updated';
/**
* @var User
*/
private $user;
/**
* @var User[]
*/
private $subscribed;
/**
* @var User[]
*/
private $unsubscribed;
/**
* @param User $user
* @param User[] $subscribed
* @param User[] $unsubscribed
*/
public function __construct(User $user, array $subscribed, array $unsubscribed)
{
$this->user = $user;
$this->subscribed = $subscribed;
$this->unsubscribed = $unsubscribed;
}
/**
* @return User
*/
public function getUser(): User
{
return $this->user;
}
/**
* @return User[]
*/
public function getSubscribedUsers(): array
{
return $this->subscribed;
}
/**
* @return User[]
*/
public function getUnsubscribedUsers(): array
{
return $this->unsubscribed;
}
}

View file

@ -1,37 +0,0 @@
<?php
namespace src\PointToolsBundle\Event;
use src\PointToolsBundle\Entity\UserRenameEvent;
use Symfony\Component\EventDispatcher\Event;
/**
* Dispatched when one or more users were renamed
*/
class UsersRenamedEvent extends Event
{
const NAME = 'app.users.renamed';
/**
* @var UserRenameEvent[]
*/
private $renames;
/**
* UsersRenamedEvent constructor.
*
* @param UserRenameEvent[] $renames
*/
public function __construct(array $renames)
{
$this->renames = $renames;
}
/**
* @return UserRenameEvent[]
*/
public function getRenames(): array
{
return $this->renames;
}
}

View file

@ -1,12 +1,17 @@
<?php <?php
declare(strict_types=1);
namespace src\PointToolsBundle\Command; namespace App\Command;
use Doctrine\ORM\{EntityManager, EntityManagerInterface}; use App\Entity\User;
use src\PointToolsBundle\Entity\User; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputInterface, InputArgument, InputOption}; use Symfony\Component\Console\Input\InputArgument;
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;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
/** /**
@ -18,23 +23,17 @@ use Symfony\Component\Filesystem\Filesystem;
* WHERE u.id <> (-1) * WHERE u.id <> (-1)
* ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV; * ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV;
*/ */
#[AsCommand(name: 'app:import:users', description: 'Import users from CSV file')]
class ImportUsersCommand extends Command class ImportUsersCommand extends Command
{ {
/** @var EntityManager */ public function __construct(private readonly EntityManagerInterface $em)
private $em;
public function __construct(EntityManagerInterface $em)
{ {
$this->em = $em;
parent::__construct(); parent::__construct();
} }
protected function configure() protected function configure(): void
{ {
$this $this
->setName('point:import:users')
->setDescription('Import users from CSV file')
->addArgument( ->addArgument(
'file', 'file',
InputArgument::REQUIRED, InputArgument::REQUIRED,
@ -55,20 +54,23 @@ class ImportUsersCommand extends Command
; ;
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output);
$fs = new Filesystem(); $fs = new Filesystem();
$fileName = $input->getArgument('file'); $fileName = $input->getArgument('file');
if (!($fs->exists($fileName) && is_readable($fileName))) { if (!($fs->exists($fileName) && \is_readable($fileName))) {
$output->writeln('File does not exists or not readable.'); $io->error('File does not exists or not readable.');
return 1;
return Command::FAILURE;
} }
if (false === ($file = fopen($fileName, 'r'))) { if (false === ($file = fopen($fileName, 'rb'))) {
$output->writeln('fopen() error'); $io->error('fopen() error');
return 1;
return Command::FAILURE;
} }
if (!$input->getOption('no-skip-first')) { if (!$input->getOption('no-skip-first')) {
@ -79,7 +81,7 @@ class ImportUsersCommand extends Command
$count = 0; $count = 0;
while (false !== ($row = fgetcsv($file, 1000, '|'))) { while (false !== ($row = fgetcsv($file, 1000, '|'))) {
if (count($row) !== 4) { if (\count($row) !== 4) {
continue; continue;
} }
@ -93,15 +95,15 @@ class ImportUsersCommand extends Command
$this->em->detach($user); $this->em->detach($user);
} }
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { if (OutputInterface::VERBOSITY_VERBOSE === $io->getVerbosity()) {
$output->writeln('@' . $row[1] . ' added'); $io->info('@' . $row[1] . ' added');
} }
$count++; $count++;
} }
$output->writeln($count . ' users imported.'); $io->success($count . ' users imported.');
return 0; return Command::SUCCESS;
} }
} }

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;
} }
} }

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Event;
use App\Entity\User;
use Symfony\Contracts\EventDispatcher\Event;
/** Dispatched when user subscribers list was changed */
class UserSubscribersUpdatedEvent extends Event
{
const NAME = 'app.user.subscribers_updated';
/**
* @param User $user
* @param User[] $subscribed
* @param User[] $unsubscribed
*/
public function __construct(
public readonly User $user,
public readonly array $subscribed,
public readonly array $unsubscribed
) {
}
}

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Event;
use App\Entity\UserRenameEvent;
use Symfony\Contracts\EventDispatcher\Event;
/** Dispatched when one or more users were renamed */
class UsersRenamedEvent extends Event
{
const NAME = 'app.users.renamed';
/** @param UserRenameEvent[] $renames */
public function __construct(
public readonly array $renames
) {
}
}