mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-26 17:43:54 +00:00
refactor access to calendar and use real userid
This commit is contained in:
@@ -11,7 +11,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph;
|
||||
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraphRemoteCalendarConnector;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
@@ -24,32 +23,61 @@ class MapCalendarToUser
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private MSGraphRemoteCalendarConnector $remoteCalendarConnector;
|
||||
private MachineHttpClient $machineHttpClient;
|
||||
|
||||
public function __construct(MSGraphRemoteCalendarConnector $remoteCalendarConnector, LoggerInterface $logger)
|
||||
{
|
||||
$this->remoteCalendarConnector = $remoteCalendarConnector;
|
||||
public function __construct(
|
||||
MachineHttpClient $machineHttpClient,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->machineHttpClient = $machineHttpClient;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function getCalendarId(User $user): ?string
|
||||
{
|
||||
if (null === $mskey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
|
||||
if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $msKey['defaultCalendarId'] ?? null;
|
||||
}
|
||||
|
||||
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 getUserByEmail(string $email): ?array
|
||||
{
|
||||
$value = $this->machineHttpClient->request('GET', 'users', [
|
||||
'query' => ['$filter' => "mail eq '{$email}'"],
|
||||
])->toArray()['value'];
|
||||
|
||||
return $value[0] ?? null;
|
||||
}
|
||||
|
||||
public function getUserId(User $user): ?string
|
||||
{
|
||||
if (null === $msKey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $msKey['id'] ?? null;
|
||||
}
|
||||
|
||||
public function writeMetadata(User $user): User
|
||||
{
|
||||
if (null === $userData = $this->remoteCalendarConnector->getUserByEmail($user->getEmailCanonical())) {
|
||||
if (null === $userData = $this->getUserByEmail($user->getEmailCanonical())) {
|
||||
$this->logger->warning('[MapCalendarToUser] could find user on msgraph', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]);
|
||||
|
||||
return $this->writeNullData($user);
|
||||
}
|
||||
|
||||
if (null === $defaultCalendar = $this->remoteCalendarConnector->getDefaultUserCalendar($userData['id'])) {
|
||||
if (null === $defaultCalendar = $this->getDefaultUserCalendar($userData['id'])) {
|
||||
$this->logger->warning('[MapCalendarToUser] could find default calendar', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]);
|
||||
|
||||
return $this->writeNullData($user);
|
||||
|
Reference in New Issue
Block a user