init: app:update-all-users-cron command
This commit is contained in:
parent
649c788da2
commit
ff50b04d09
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Entity\UserLog;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
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:update-all-users-cron',
|
||||
description: 'Add a short description for your command',
|
||||
)]
|
||||
class UpdateAllUsersCronCommand extends Command
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
|
||||
/**
|
||||
* @param EntityManager $entityManager
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$em = $this->entityManager;
|
||||
$repo = $em->getRepository(User::class);
|
||||
|
||||
$chunkSize = 100;
|
||||
$count = $repo->count([]);
|
||||
|
||||
$io->progressStart($count);
|
||||
for ($i = 0; $i < $count; $i += $chunkSize) {
|
||||
$data = $repo->findBy([], null, $chunkSize, $i);
|
||||
if ($data == []) {
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var User */
|
||||
foreach ($data as $user) {
|
||||
$score = (int) $user->getScore();
|
||||
if ($score < 100 || $score > 900) {
|
||||
$user->setLog(true);
|
||||
|
||||
$log = new UserLog();
|
||||
$log->setScore($user->getScore());
|
||||
$log->setUserId($user->getId());
|
||||
|
||||
$em->persist($user);
|
||||
$em->persist($log);
|
||||
} else if ($user->getLog()) {
|
||||
$user->setLog(false);
|
||||
$em->persist($user);
|
||||
}
|
||||
}
|
||||
|
||||
$io->progressAdvance($chunkSize);
|
||||
|
||||
$em->flush();
|
||||
$em->clear();
|
||||
}
|
||||
$io->progressFinish();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
|
@ -22,6 +22,11 @@ class UserLog
|
|||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $created_at;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setCreatedAtNow();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
|
@ -62,4 +67,10 @@ class UserLog
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCreatedAtNow(): self {
|
||||
$this->setCreatedAt(new \DateTimeImmutable());
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue