Laralabs/here-api-oauth2-client

View on GitHub
src/HereOAuth.php

Summary

Maintainability
A
0 mins
Test Coverage
F
55%
<?php

namespace Laralabs\HereOAuth;

use Laralabs\HereOAuth\Cache\Manager as Cache;
use Laralabs\HereOAuth\Exceptions\HereOAuthTokenExpired;
use Laralabs\HereOAuth\Factories\HereOAuthToken;

class HereOAuth
{
    protected Cache $cache;

    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

    public function getToken(): string
    {
        try {
            return $this->cache->retrieve();
        } catch (HereOAuthTokenExpired $exception) {
            $token = app(HereOAuthToken::class)->generate();

            $this->cache->store($token['access_token'], $token['expires_in']);

            return $token['access_token'];
        }
    }
}