1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP SDK library for the Superdesk Content API. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Superdesk\ContentApiSdk\Client; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\API\Authentication\AuthenticationInterface; |
18
|
|
|
|
19
|
|
|
abstract class AbstractApiClient implements ApiClientInterface |
20
|
|
|
{ |
21
|
|
|
const MAX_RETRY_LIMIT = 3; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HTTP client. |
25
|
|
|
* |
26
|
|
|
* @var ClientInterface |
27
|
|
|
*/ |
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Authentication object. |
32
|
|
|
* |
33
|
|
|
* @var AuthenticationInterface |
34
|
|
|
*/ |
35
|
|
|
protected $authenticator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retry attempt counter for authentication. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $authenticationRetryAttempt = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
ClientInterface $client, |
49
|
|
|
AuthenticationInterface $authenticator |
50
|
|
|
) { |
51
|
|
|
$this->client = $client; |
52
|
|
|
$this->authenticator = $authenticator; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets authentication retry limit to 0. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function resetAuthenticationRetryAttempt() |
61
|
|
|
{ |
62
|
|
|
$this->authenticationRetryAttempt = 0; |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Increments the authentication retry attempt with 1. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
protected function incrementAuthenticationRetryAttempt() |
73
|
|
|
{ |
74
|
|
|
$this->authenticationRetryAttempt++; |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns authentication retry count. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
|
|
public function getAuthenticationRetryAttempt() |
85
|
|
|
{ |
86
|
|
|
return $this->authenticationRetryAttempt; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns whether the authentication limit is reached. |
91
|
|
|
* |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
|
|
protected function isAuthenticationRetryLimitReached() |
95
|
|
|
{ |
96
|
|
|
return $this->getAuthenticationRetryAttempt() > self::MAX_RETRY_LIMIT; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|