mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
104 lines
3.8 KiB
PHP
104 lines
3.8 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);
|
|
|
|
namespace Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteToLocalSync;
|
|
|
|
use Chill\CalendarBundle\Entity\CalendarRange;
|
|
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient;
|
|
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use RuntimeException;
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
class CalendarRangeSyncer
|
|
{
|
|
private EntityManagerInterface $em;
|
|
|
|
private LoggerInterface $logger;
|
|
|
|
private HttpClientInterface $machineHttpClient;
|
|
|
|
/**
|
|
* @param MachineHttpClient $machineHttpClient
|
|
*/
|
|
public function __construct(
|
|
EntityManagerInterface $em,
|
|
LoggerInterface $logger,
|
|
HttpClientInterface $machineHttpClient
|
|
) {
|
|
$this->em = $em;
|
|
$this->logger = $logger;
|
|
$this->machineHttpClient = $machineHttpClient;
|
|
}
|
|
|
|
public function handleCalendarRangeSync(CalendarRange $calendarRange, array $notification, User $user): void
|
|
{
|
|
switch ($notification['changeType']) {
|
|
case 'deleted':
|
|
// test if the notification is not linked to a Calendar
|
|
if (null !== $calendarRange->getCalendar()) {
|
|
return;
|
|
}
|
|
$calendarRange->preventEnqueueChanges = true;
|
|
|
|
$this->logger->info(__CLASS__ . ' remove a calendar range because deleted on remote calendar');
|
|
$this->em->remove($calendarRange);
|
|
|
|
break;
|
|
|
|
case 'updated':
|
|
try {
|
|
$new = $this->machineHttpClient->request(
|
|
'GET',
|
|
$notification['resource']
|
|
)->toArray();
|
|
} catch (ClientExceptionInterface $clientException) {
|
|
$this->logger->warning(__CLASS__ . ' could not retrieve event from ms graph. Already deleted ?', [
|
|
'calendarRangeId' => $calendarRange->getId(),
|
|
'remoteEventId' => $notification['resource'],
|
|
]);
|
|
|
|
throw $clientException;
|
|
}
|
|
|
|
$lastModified = RemoteEventConverter::convertStringDateWithTimezone($new['lastModifiedDateTime']);
|
|
|
|
if ($calendarRange->getRemoteAttributes()['lastModifiedDateTime'] === $lastModified->getTimestamp()) {
|
|
$this->logger->info(__CLASS__ . ' change key is equals. Source is probably a local update', [
|
|
'calendarRangeId' => $calendarRange->getId(),
|
|
'remoteEventId' => $notification['resource'],
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
$startDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['start']['dateTime']);
|
|
$endDate = RemoteEventConverter::convertStringDateWithoutTimezone($new['end']['dateTime']);
|
|
|
|
$calendarRange
|
|
->setStartDate($startDate)->setEndDate($endDate)
|
|
->addRemoteAttributes([
|
|
'lastModifiedDateTime' => $lastModified->getTimestamp(),
|
|
'changeKey' => $new['changeKey'],
|
|
])
|
|
->preventEnqueueChanges = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
throw new RuntimeException('This changeType is not suppored: ' . $notification['changeType']);
|
|
}
|
|
}
|
|
}
|