mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-26 17:43:54 +00:00
refactor and rename classes
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?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;
|
||||
|
||||
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraphRemoteCalendarConnector;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Write metadata to user, which allow to find his default calendar.
|
||||
*/
|
||||
class MapCalendarToUser
|
||||
{
|
||||
public const METADATA_KEY = 'msgraph';
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private MSGraphRemoteCalendarConnector $remoteCalendarConnector;
|
||||
|
||||
public function __construct(MSGraphRemoteCalendarConnector $remoteCalendarConnector, LoggerInterface $logger)
|
||||
{
|
||||
$this->remoteCalendarConnector = $remoteCalendarConnector;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function getCalendarId(User $user): ?string
|
||||
{
|
||||
if (null === $mskey = ($user->getAttributes()[self::METADATA_KEY] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $msKey['defaultCalendarId'] ?? null;
|
||||
}
|
||||
|
||||
public function writeMetadata(User $user): User
|
||||
{
|
||||
if (null === $userData = $this->remoteCalendarConnector->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'])) {
|
||||
$this->logger->warning('[MapCalendarToUser] could find default calendar', ['userId' => $user->getId(), 'email' => $user->getEmailCanonical()]);
|
||||
|
||||
return $this->writeNullData($user);
|
||||
}
|
||||
|
||||
return $user->setAttributes([self::METADATA_KEY => [
|
||||
'id' => $userData['id'],
|
||||
'userPrincipalName' => $userData['userPrincipalName'],
|
||||
'defaultCalendarId' => $defaultCalendar['id'],
|
||||
]]);
|
||||
}
|
||||
|
||||
private function writeNullData(User $user): User
|
||||
{
|
||||
return $user->unsetAttribute(self::METADATA_KEY);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user