mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-27 09:05:01 +00:00
refactor and rename classes
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?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 LogicException;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use TheNetworg\OAuth2\Client\Provider\Azure;
|
||||
use TheNetworg\OAuth2\Client\Token\AccessToken;
|
||||
|
||||
/**
|
||||
* Store token obtained on behalf of a User.
|
||||
*/
|
||||
class OnBehalfOfUserTokenStorage
|
||||
{
|
||||
public const MS_GRAPH_ACCESS_TOKEN = 'msgraph_access_token';
|
||||
|
||||
private Azure $azure;
|
||||
|
||||
private SessionInterface $session;
|
||||
|
||||
public function __construct(Azure $azure, SessionInterface $session)
|
||||
{
|
||||
$this->azure = $azure;
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function getToken(): AccessToken
|
||||
{
|
||||
/** @var ?AccessToken $token */
|
||||
$token = $this->session->get(self::MS_GRAPH_ACCESS_TOKEN, null);
|
||||
|
||||
if (null === $token) {
|
||||
throw new LogicException('unexisting token');
|
||||
}
|
||||
|
||||
if ($token->hasExpired()) {
|
||||
$token = $this->azure->getAccessToken('refresh_token', [
|
||||
'refresh_token' => $token->getRefreshToken(),
|
||||
]);
|
||||
|
||||
$this->setToken($token);
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function hasToken(): bool
|
||||
{
|
||||
return $this->session->has(self::MS_GRAPH_ACCESS_TOKEN);
|
||||
}
|
||||
|
||||
public function setToken(AccessToken $token): void
|
||||
{
|
||||
$this->session->set(self::MS_GRAPH_ACCESS_TOKEN, $token);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user