em = $em; $this->eventsOnUserSubscriptionCreator = $eventsOnUserSubscriptionCreator; $this->logger = $logger; $this->mapCalendarToUser = $mapCalendarToUser; $this->userRepository = $userRepository; } public function execute(InputInterface $input, OutputInterface $output): int { $this->logger->info(__CLASS__ . ' execute command'); $limit = 50; $offset = 0; /** @var DateInterval $interval the interval before the end of the expiration */ $interval = new DateInterval('P1D'); $expiration = (new DateTimeImmutable('now'))->add(new DateInterval($input->getOption('subscription-duration'))); $total = $this->userRepository->countByMostOldSubscriptionOrWithoutSubscriptionOrData($interval); $created = 0; $renewed = 0; $this->logger->info(__CLASS__ . ' the number of user to get - renew', [ 'total' => $total, 'expiration' => $expiration->format(DateTimeImmutable::ATOM), ]); while ($offset < $total) { $users = $this->userRepository->findByMostOldSubscriptionOrWithoutSubscriptionOrData( $interval, $limit, $offset ); foreach ($users as $user) { if (!$this->mapCalendarToUser->hasUserId($user)) { $this->mapCalendarToUser->writeMetadata($user); } if ($this->mapCalendarToUser->hasUserId($user)) { // we first try to renew an existing subscription, if any. // if not, or if it fails, we try to create a new one if ($this->mapCalendarToUser->hasActiveSubscription($user)) { $this->logger->debug(__CLASS__ . ' renew a subscription for', [ 'userId' => $user->getId(), 'username' => $user->getUsernameCanonical(), ]); ['secret' => $secret, 'id' => $id, 'expiration' => $expirationTs] = $this->eventsOnUserSubscriptionCreator->renewSubscriptionForUser($user, $expiration); $this->mapCalendarToUser->writeSubscriptionMetadata($user, $expirationTs, $id, $secret); if (0 !== $expirationTs) { ++$renewed; } else { $this->logger->warning(__CLASS__ . ' could not renew subscription for a user', [ 'userId' => $user->getId(), 'username' => $user->getUsernameCanonical(), ]); } } if (!$this->mapCalendarToUser->hasActiveSubscription($user)) { $this->logger->debug(__CLASS__ . ' create a subscription for', [ 'userId' => $user->getId(), 'username' => $user->getUsernameCanonical(), ]); ['secret' => $secret, 'id' => $id, 'expiration' => $expirationTs] = $this->eventsOnUserSubscriptionCreator->createSubscriptionForUser($user, $expiration); $this->mapCalendarToUser->writeSubscriptionMetadata($user, $expirationTs, $id, $secret); if (0 !== $expirationTs) { ++$created; } else { $this->logger->warning(__CLASS__ . ' could not create subscription for a user', [ 'userId' => $user->getId(), 'username' => $user->getUsernameCanonical(), ]); } } } ++$offset; } $this->em->flush(); $this->em->clear(); } $this->logger->warning(__CLASS__ . ' process executed', [ 'created' => $created, 'renewed' => $renewed, ]); return 0; } protected function configure() { parent::configure(); $this ->setDescription('MSGraph: collect user metadata and create subscription on events for users') ->addOption( 'renew-before-end-interval', 'r', InputOption::VALUE_OPTIONAL, 'delay before renewing subscription', 'P1D' ) ->addOption( 'subscription-duration', 's', InputOption::VALUE_OPTIONAL, 'duration for the subscription', 'PT4230M' ); } }