mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 09:03:48 +00:00
refactor access to calendar and use real userid
This commit is contained in:
@@ -12,19 +12,26 @@ declare(strict_types=1);
|
||||
namespace Chill\CalendarBundle\RemoteCalendar\Connector;
|
||||
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MachineHttpClient;
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserHttpClient;
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserTokenStorage;
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use DateTimeImmutable;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
|
||||
class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
{
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private MachineHttpClient $machineHttpClient;
|
||||
|
||||
private MapCalendarToUser $mapCalendarToUser;
|
||||
|
||||
private RemoteEventConverter $remoteEventConverter;
|
||||
|
||||
private OnBehalfOfUserTokenStorage $tokenStorage;
|
||||
@@ -35,42 +42,28 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
|
||||
public function __construct(
|
||||
MachineHttpClient $machineHttpClient,
|
||||
MapCalendarToUser $mapCalendarToUser,
|
||||
LoggerInterface $logger,
|
||||
OnBehalfOfUserTokenStorage $tokenStorage,
|
||||
OnBehalfOfUserHttpClient $userHttpClient,
|
||||
RemoteEventConverter $remoteEventConverter,
|
||||
UrlGeneratorInterface $urlGenerator
|
||||
) {
|
||||
$this->machineHttpClient = $machineHttpClient;
|
||||
$this->mapCalendarToUser = $mapCalendarToUser;
|
||||
$this->logger = $logger;
|
||||
$this->remoteEventConverter = $remoteEventConverter;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->userHttpClient = $userHttpClient;
|
||||
}
|
||||
|
||||
public function getDefaultUserCalendar(string $idOrUserPrincipalName): ?array
|
||||
{
|
||||
$value = $this->machineHttpClient->request('GET', "users/{$idOrUserPrincipalName}/calendars", [
|
||||
'query' => ['$filter' => 'isDefaultCalendar eq true'],
|
||||
])->toArray()['value'];
|
||||
|
||||
return $value[0] ?? null;
|
||||
}
|
||||
|
||||
public function getMakeReadyResponse(string $returnPath): Response
|
||||
{
|
||||
return new RedirectResponse($this->urlGenerator
|
||||
->generate('chill_calendar_remote_connect_azure', ['returnPath' => $returnPath]));
|
||||
}
|
||||
|
||||
public function getUserByEmail(string $email): ?array
|
||||
{
|
||||
$value = $this->machineHttpClient->request('GET', 'users', [
|
||||
'query' => ['$filter' => "mail eq '{$email}'"],
|
||||
])->toArray()['value'];
|
||||
|
||||
return $value[0] ?? null;
|
||||
}
|
||||
|
||||
public function isReady(): bool
|
||||
{
|
||||
return $this->tokenStorage->hasToken();
|
||||
@@ -78,18 +71,33 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
|
||||
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array
|
||||
{
|
||||
$bareEvents = $this->userHttpClient->request(
|
||||
'GET',
|
||||
'users/c4f1fcc7-10e4-4ea9-89ac-c89a00e0a51a/calendarView',
|
||||
[
|
||||
'query' => [
|
||||
'startDateTime' => $startDate->format(DateTimeImmutable::ATOM),
|
||||
'endDateTime' => $endDate->format(DateTimeImmutable::ATOM),
|
||||
'$select' => 'id,subject,start,end',
|
||||
],
|
||||
]
|
||||
)->toArray();
|
||||
$userId = $this->mapCalendarToUser->getUserId($user);
|
||||
|
||||
return array_map(function ($item) { return $this->remoteEventConverter->convertToRemote($item); }, $bareEvents['value']);
|
||||
if (null === $userId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$bareEvents = $this->userHttpClient->request(
|
||||
'GET',
|
||||
'users/' . $userId . '/calendarView',
|
||||
[
|
||||
'query' => [
|
||||
'startDateTime' => $startDate->format(DateTimeImmutable::ATOM),
|
||||
'endDateTime' => $endDate->format(DateTimeImmutable::ATOM),
|
||||
'$select' => 'id,subject,start,end',
|
||||
],
|
||||
]
|
||||
)->toArray();
|
||||
|
||||
return array_map(function ($item) { return $this->remoteEventConverter->convertToRemote($item); }, $bareEvents['value']);
|
||||
} catch (ClientExceptionInterface $e) {
|
||||
$this->logger->debug('Could not get list of event on MSGraph', [
|
||||
'error_code' => $e->getResponse()->getStatusCode(),
|
||||
'error' => $e->getResponse()->getInfo(),
|
||||
]);
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user