<?php
namespace Lnb\Shopware6\LnbPartnerAuth\TherapistsApi;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class BearerTokenAuthProvider
{
public const TOKEN_KEY = 'lnb_partner_auth_token';
public const PARTNER_ID_KEY = 'lnb_partner_id';
private ?string $token = null;
private ?string $partnerId = null;
public function __construct(
private readonly SessionInterface $session
) {
}
public function save(): void
{
$this->session->set(self::TOKEN_KEY, $this->token);
$this->session->set(self::PARTNER_ID_KEY, $this->partnerId);
}
public function restore(): void
{
if ($this->hasSessionToken()) {
$this->token = $this->session->get(self::TOKEN_KEY);
}
if ($this->hasPartnerId()) {
$this->partnerId = $this->session->get(self::PARTNER_ID_KEY);
}
}
public function reset(): void
{
$this->token = null;
$this->partnerId = null;
$this->session->remove(self::TOKEN_KEY);
$this->session->remove(self::PARTNER_ID_KEY);
}
public function hasSessionToken(): bool
{
return $this->session->has(self::TOKEN_KEY);
}
public function hasPartnerId(): bool
{
return $this->session->has(self::PARTNER_ID_KEY);
}
public function getBearerToken(): ?string
{
return $this->token;
}
public function getPartnerId(): ?string
{
return $this->partnerId;
}
public function set(string $token, string $partnerId): void
{
$this->token = $token;
$this->partnerId = $partnerId;
$this->save();
}
}