Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractGoogle
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
11
100.00% covered (success)
100.00%
1 / 1
 createClient
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 requestToken
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Mail\Api;
16
17use Pop\Http;
18use Google;
19
20/**
21 * Abstract Google Mail API class
22 *
23 * @category   Pop
24 * @package    Pop\Mail
25 * @author     Nick Sagona, III <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30abstract class AbstractGoogle extends AbstractHttpClient
31{
32
33    /**
34     * Create client
35     *
36     * @param  array|string $options
37     * @param  ?string      $username
38     * @throws Exception
39     * @return AbstractGoogle
40     */
41    public function createClient(array|string $options, ?string $username = null): AbstractGoogle
42    {
43        if ($username !== null) {
44            $this->setUsername($username);
45        }
46
47        if ($this->username === null) {
48            throw new Exception('Error: The username is required to create the client object.');
49        }
50
51        $this->client = new Google\Client();
52        $this->client->setAuthConfig($options);
53        $this->client->setSubject($this->username);
54        $this->client->setAccessType('offline');
55        $this->client->setIncludeGrantedScopes(true);
56        $this->client->addScope(Google\Service\Gmail::MAIL_GOOGLE_COM);
57
58        return $this;
59    }
60
61    /**
62     * Request token
63     *
64     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
65     * @return AbstractGoogle
66     */
67    public function requestToken(): AbstractGoogle
68    {
69        if (empty($this->client)) {
70            throw new Exception('Error: The client object has not yet been instantiated.');
71        } else {
72            if (isset($this->token) && isset($this->tokenExpires) && !$this->isTokenExpired()) {
73                return $this;
74            }
75        }
76
77        $response = $this->client->fetchAccessTokenWithAssertion();
78
79        if (is_array($response) && isset($response['access_token']) && isset($response['expires_in'])) {
80            $this->setToken($response['access_token'])
81                ->setTokenExpires((string)(time() + $response['expires_in']));
82        }
83
84        return $this;
85    }
86
87}