Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/Data/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};
}
}
42 changes: 42 additions & 0 deletions src/Data/RenewalContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace Vin\ShopwareSdk\Data;

use Vin\ShopwareSdk\Client\AdminAuthenticator;
use Vin\ShopwareSdk\Client\GrantType\RefreshTokenGrantType;

/**
* @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 RenewableContext extends Context
{
protected function checkRenewToken(): void
{
if (!$this->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);
}
}