Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
23 / 23
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractHttpClient
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
23 / 23
29
100.00% covered (success)
100.00%
1 / 1
 setClientId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getClientId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasClientId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setClientSecret
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getClientSecret
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasClientSecret
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getScope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasScope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAccountId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAccountId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasAccountId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUsername
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setToken
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTokenExpires
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTokenExpires
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasTokenExpires
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isTokenExpired
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 verifyToken
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
6
 requestToken
n/a
0 / 0
n/a
0 / 0
0
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;
17
18/**
19 * Abstract HTTP class
20 *
21 * @category   Pop
22 * @package    Pop\Mail
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28abstract class AbstractHttpClient extends AbstractHttp implements HttpClientInterface
29{
30
31    /**
32     * Client ID
33     * @var ?string
34     */
35    protected ?string $clientId = null;
36
37    /**
38     * Client Secret
39     * @var ?string
40     */
41    protected ?string $clientSecret = null;
42
43    /**
44     * Scope
45     * @var ?string
46     */
47    protected ?string $scope = null;
48
49    /**
50     * Account ID
51     * @var ?string
52     */
53    protected ?string $accountId = null;
54
55    /**
56     * Account username
57     * @var ?string
58     */
59    protected ?string $username = null;
60
61    /**
62     * Token
63     * @var ?string
64     */
65    protected ?string $token = null;
66
67    /**
68     * Token expires
69     * @var ?string
70     */
71    protected ?string $tokenExpires = null;
72
73    /**
74     * Token request URI
75     * @var ?string
76     */
77    protected ?string $tokenRequestUri = null;
78
79    /**
80     * Base URL
81     * @var ?string
82     */
83    protected ?string $baseUri = null;
84
85    /**
86     * Set client ID
87     *
88     * @param  string $clientId
89     * @return AbstractHttpClient
90     */
91    public function setClientId(string $clientId): AbstractHttpClient
92    {
93        $this->clientId = $clientId;
94        return $this;
95    }
96
97    /**
98     * Get client ID
99     *
100     * @return ?string
101     */
102    public function getClientId(): ?string
103    {
104        return $this->clientId;
105    }
106
107    /**
108     * Has client ID
109     *
110     * @return bool
111     */
112    public function hasClientId(): bool
113    {
114        return ($this->clientId !== null);
115    }
116
117    /**
118     * Set client secret
119     *
120     * @param  string $clientSecret
121     * @return AbstractHttpClient
122     */
123    public function setClientSecret(string $clientSecret): AbstractHttpClient
124    {
125        $this->clientSecret = $clientSecret;
126        return $this;
127    }
128
129    /**
130     * Get client ID
131     *
132     * @return ?string
133     */
134    public function getClientSecret(): ?string
135    {
136        return $this->clientSecret;
137    }
138
139    /**
140     * Has client ID
141     *
142     * @return bool
143     */
144    public function hasClientSecret(): bool
145    {
146        return ($this->clientSecret !== null);
147    }
148
149
150    /**
151     * Set scope
152     * @param  string $scope
153     * @return AbstractHttpClient
154     */
155    public function setScope(string $scope): AbstractHttpClient
156    {
157        $this->scope = $scope;
158        return $this;
159    }
160
161    /**
162     * Get scope ID
163     *
164     * @return ?string
165     */
166    public function getScope(): ?string
167    {
168        return $this->scope;
169    }
170
171    /**
172     * Has scope ID
173     *
174     * @return bool
175     */
176    public function hasScope(): bool
177    {
178        return ($this->scope !== null);
179    }
180
181    /**
182     * Set account ID
183     *
184     * @param  string $accountId
185     * @return AbstractHttpClient
186     */
187    public function setAccountId(string $accountId): AbstractHttpClient
188    {
189        $this->accountId = $accountId;
190        return $this;
191    }
192
193    /**
194     * Get account ID
195     *
196     * @return ?string
197     */
198    public function getAccountId(): ?string
199    {
200        return $this->accountId;
201    }
202
203    /**
204     * Has account ID
205     *
206     * @return bool
207     */
208    public function hasAccountId(): bool
209    {
210        return ($this->accountId !== null);
211    }
212
213    /**
214     * Set account username
215     *
216     * @param  string $username
217     * @return AbstractHttpClient
218     */
219    public function setUsername(string $username): AbstractHttpClient
220    {
221        $this->username = $username;
222        return $this;
223    }
224
225    /**
226     * Get account username
227     *
228     * @return ?string
229     */
230    public function getUsername(): ?string
231    {
232        return $this->username;
233    }
234
235    /**
236     * Has account username
237     *
238     * @return bool
239     */
240    public function hasUsername(): bool
241    {
242        return ($this->username !== null);
243    }
244
245    /**
246     * Set token
247     *
248     * @param  string $token
249     * @return AbstractHttpClient
250     */
251    public function setToken(string $token): AbstractHttpClient
252    {
253        $this->token = $token;
254        return $this;
255    }
256
257    /**
258     * Get token
259     *
260     * @return ?string
261     */
262    public function getToken(): ?string
263    {
264        return $this->token;
265    }
266
267    /**
268     * Has token
269     *
270     * @return bool
271     */
272    public function hasToken(): bool
273    {
274        return ($this->token !== null);
275    }
276
277    /**
278     * Set token expires
279     *
280     * @param  string $tokenExpires
281     * @return AbstractHttpClient
282     */
283    public function setTokenExpires(string $tokenExpires): AbstractHttpClient
284    {
285        $this->tokenExpires = $tokenExpires;
286        return $this;
287    }
288
289    /**
290     * Get token expires
291     *
292     * @return ?string
293     */
294    public function getTokenExpires(): ?string
295    {
296        return $this->tokenExpires;
297    }
298
299    /**
300     * Has token expires
301     *
302     * @return bool
303     */
304    public function hasTokenExpires(): bool
305    {
306        return ($this->tokenExpires !== null);
307    }
308
309    /**
310     * Check if token is expired
311     *
312     * @return boolean
313     */
314    public function isTokenExpired(): bool
315    {
316        return (((int)$this->tokenExpires > 0) && ($this->tokenExpires <= time()));
317    }
318
319    /**
320     * Verify token and refresh is expired
321     *
322     * @return boolean
323     */
324    public function verifyToken(): bool
325    {
326        if (!isset($this->token) || !isset($this->tokenExpires) || ($this->isTokenExpired())) {
327            $this->requestToken();
328        }
329
330        return (isset($this->token) && isset($this->tokenExpires) && (!$this->isTokenExpired()));
331    }
332
333    /**
334     * Request token
335     *
336     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
337     * @return AbstractHttpClient
338     */
339    abstract public function requestToken(): AbstractHttpClient;
340
341}