Skip to content

add RenewableContext to automatically create new accessTokens#82

Open
Scarbous wants to merge 1 commit into
vienthuong:masterfrom
Scarbous:feature/renewable-context
Open

add RenewableContext to automatically create new accessTokens#82
Scarbous wants to merge 1 commit into
vienthuong:masterfrom
Scarbous:feature/renewable-context

Conversation

@Scarbous

@Scarbous Scarbous commented Jan 5, 2024

Copy link
Copy Markdown

No description provided.

@Scarbous

Scarbous commented Jan 5, 2024

Copy link
Copy Markdown
Author

In my opinion, this solution has the least impact on the project.

@vienthuong

Copy link
Copy Markdown
Owner

Hi @Scarbous
First of all, thanks for your contribution,

Could you give me an example why there's need to this "magic" getter?, the Context class is an DTO so i'd expect less magic code there :)

@Scarbous

Copy link
Copy Markdown
Author

@vienthuong my aim is to have a context that can renew the token independently.

I am aware that a DTO should not contain any logic, but the question for me is where this can be better implemented without rebuilding everything.

Übersetzt mit DeepL (https://www.deepl.com/app/?utm_source=ios&utm_medium=app&utm_campaign=share-translation)

@vienthuong

Copy link
Copy Markdown
Owner

Hi @Scarbous

In that case, you can just create a class in your project and do exactly the same

class ContextRenewable
{
    private Context $originContext;
    public function __construct(Context $originContext) {
        $this->originContext = $originContext;
    }

    public function getContext(): Context
    {
        if (!$this->originContext->accessToken->isExpired()) {
            return $this->originContext;
        }

        $adminClient = new AdminAuthenticator(
            new RefreshTokenGrantType($this->originContext->accessToken->refreshToken),
            $this->originContext->apiEndpoint
        );

        $this->originContext = new Context($this->originContext->shopUrl, $adminClient->fetchAccessToken());

        return $this->originContext;
    }
}

// Usage:
$nonExpiredContext = (new ContextRenewable($context))->getContext();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants