diff --git a/src/Data/Context.php b/src/Data/Context.php index df7b124c..47bb1a4e 100644 --- a/src/Data/Context.php +++ b/src/Data/Context.php @@ -2,25 +2,35 @@ namespace Vin\ShopwareSdk\Data; +/** + * @property string $languageId + * @property string $currencyId + * @property string $versionId + * @property bool $compatibility + * @property bool $inheritance + * @property AccessToken $accessToken + * @property string $apiEndpoint + * @property array $additionalHeaders + */ class Context { use EndPointTrait; - public string $languageId = Defaults::LANGUAGE_SYSTEM; + private string $languageId = Defaults::LANGUAGE_SYSTEM; - public string $currencyId = Defaults::CURRENCY; + private string $currencyId = Defaults::CURRENCY; - public string $versionId = Defaults::LIVE_VERSION; + private string $versionId = Defaults::LIVE_VERSION; - public bool $compatibility = true; + private bool $compatibility = true; - public bool $inheritance = true; + private bool $inheritance = true; - public AccessToken $accessToken; + private AccessToken $accessToken; - public string $apiEndpoint; + private string $apiEndpoint; - public array $additionalHeaders; + private array $additionalHeaders; public function __construct( string $apiEndpoint, @@ -41,4 +51,12 @@ public function __construct( $this->apiEndpoint = $this->removeLastSlashes($apiEndpoint); $this->additionalHeaders = $additionalHeaders; } + + function __get(string $name){ + if(!isset($this->{$name})){ + throw new \Exception("Unknown property '{$name}' not found"); + } + + return $this->{$name}; + } } diff --git a/src/Data/RenewalContext.php b/src/Data/RenewalContext.php new file mode 100644 index 00000000..c4bde8f1 --- /dev/null +++ b/src/Data/RenewalContext.php @@ -0,0 +1,42 @@ +context->accessToken->isExpired()) { + return; + } + + $adminClient = new AdminAuthenticator( + new RefreshTokenGrantType($this->context->accessToken->refreshToken), + $this->context->apiEndpoint + ); + + $this->context->accessToken = $adminClient->fetchAccessToken(); + } + + function __get(string $name) + { + if($name === 'accessToken'){ + $this->checkRenewToken(); + } + + return parent::__get($name); + } +} \ No newline at end of file