mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
When the total of user to subscribe is a multiple of offset, the subscription was an infinite loop
172 lines
6.4 KiB
PHP
172 lines
6.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers.
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\CalendarBundle\Command;
|
|
|
|
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\EventsOnUserSubscriptionCreator;
|
|
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
|
|
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MSGraphUserRepository;
|
|
use DateInterval;
|
|
use DateTimeImmutable;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class MapAndSubscribeUserCalendarCommand extends Command
|
|
{
|
|
private EntityManagerInterface $em;
|
|
|
|
private EventsOnUserSubscriptionCreator $eventsOnUserSubscriptionCreator;
|
|
|
|
private LoggerInterface $logger;
|
|
|
|
private MapCalendarToUser $mapCalendarToUser;
|
|
|
|
private MSGraphUserRepository $userRepository;
|
|
|
|
public function __construct(
|
|
EntityManagerInterface $em,
|
|
EventsOnUserSubscriptionCreator $eventsOnUserSubscriptionCreator,
|
|
LoggerInterface $logger,
|
|
MapCalendarToUser $mapCalendarToUser,
|
|
MSGraphUserRepository $userRepository
|
|
) {
|
|
parent::__construct('chill:calendar:msgraph-user-map-subscribe');
|
|
|
|
$this->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'
|
|
);
|
|
}
|
|
}
|