Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RenewableContext to automatically create new accessTokens #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Scarbous
Copy link

@Scarbous Scarbous commented Jan 5, 2024

No description provided.

@Scarbous
Copy link
Author

Scarbous commented Jan 5, 2024

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

@vienthuong
Copy link
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
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
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