From ff50b04d096e585533ac38f08cb8753a75f53133 Mon Sep 17 00:00:00 2001 From: b1ek Date: Fri, 4 Oct 2024 17:49:08 +1000 Subject: [PATCH] init: app:update-all-users-cron command --- src/Command/UpdateAllUsersCronCommand.php | 76 +++++++++++++++++++++++ src/Entity/UserLog.php | 11 ++++ 2 files changed, 87 insertions(+) create mode 100644 src/Command/UpdateAllUsersCronCommand.php diff --git a/src/Command/UpdateAllUsersCronCommand.php b/src/Command/UpdateAllUsersCronCommand.php new file mode 100644 index 0000000..a39e23d --- /dev/null +++ b/src/Command/UpdateAllUsersCronCommand.php @@ -0,0 +1,76 @@ +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; + } +} diff --git a/src/Entity/UserLog.php b/src/Entity/UserLog.php index 36d66ad..c9ddf18 100644 --- a/src/Entity/UserLog.php +++ b/src/Entity/UserLog.php @@ -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; + } }