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