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

View File

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