Skip to content

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