Documentation

AccessTokenHandler
in package

Manages the access token that is needed to authenticate the Google Firebase API calls.

Table of Contents

Constants

CACHE_ACCESS_TOKEN_NAME  = 'mve_fcm_php_token'
SCOPE_FIREBASE_MESSAGING  = 'https://www.googleapis.com/auth/firebase.messaging'

Properties

$cache  : CacheInterface
$logger  : LoggerInterface|null
$serviceAccount  : array<string|int, mixed>

Methods

__construct()  : mixed
getProjectId()  : string
Returns the project ID.
getToken()  : string
Returns a non expired access token from the cache, or if there isn't one, from the Google API and stores this one in the cache.
base64EncodeUrl()  : string
Returns a base64 encoded URL safe string.
fetchTokenFromGoogleAPI()  : array<string|int, mixed>
Requests an access token from the Google API.
generateJWT()  : string
Generates a JWT token that we need to request an access token.

Constants

CACHE_ACCESS_TOKEN_NAME

public mixed CACHE_ACCESS_TOKEN_NAME = 'mve_fcm_php_token'

SCOPE_FIREBASE_MESSAGING

public mixed SCOPE_FIREBASE_MESSAGING = 'https://www.googleapis.com/auth/firebase.messaging'

Properties

Methods

__construct()

public __construct(CacheInterface $cache, string $jsonFile, LoggerInterface|null $logger) : mixed
Parameters
$cache : CacheInterface

A CacheInterface implementation, used to cache the access token.

$jsonFile : string

The Google private key JSON file.

$logger : LoggerInterface|null

An optional LoggerInterface implementation.

getProjectId()

Returns the project ID.

public getProjectId() : string
Return values
string

getToken()

Returns a non expired access token from the cache, or if there isn't one, from the Google API and stores this one in the cache.

public getToken([bool $forceFromApi = false ]) : string
Parameters
$forceFromApi : bool = false

If true, then it gets the access token from the Google API always, even there is still one in the cache.

Tags
throws
FcmClientException

In case we cannot generate a JWT token.

throws
FcmException

In case the Google API returns a valid error JSON response.

throws
Exception

In case of another error.

Return values
string

A non expired access token that can be used to authenticate the API calls.

base64EncodeUrl()

Returns a base64 encoded URL safe string.

private base64EncodeUrl(string $data) : string
Parameters
$data : string
Return values
string

fetchTokenFromGoogleAPI()

Requests an access token from the Google API.

private fetchTokenFromGoogleAPI() : array<string|int, mixed>
Tags
throws
FcmClientException

In case we cannot generate a JWT token.

throws
FcmException

When we don't get a 200 status code back.

throws
Exception

In case of some other error, for example if the server doesn't return valid JSON.

Return values
array<string|int, mixed>

Array of the response. Contains the 'access_token' and 'expires_in' fields.


        
On this page

Search results