|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\AppnexusClient; |
|
4
|
|
|
|
|
5
|
|
|
use Audiens\AppnexusClient\authentication\AuthStrategyInterface; |
|
6
|
|
|
use Doctrine\Common\Cache\Cache; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Psr7\Response; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Auth |
|
14
|
|
|
* |
|
15
|
|
|
* see https://wiki.apnexus.com/display/adnexusdocumentation/Segment+Service |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class Auth implements ClientInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** @var Cache */ |
|
22
|
|
|
protected $cache; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Client */ |
|
25
|
|
|
protected $client; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $token; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $username; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $password; |
|
35
|
|
|
|
|
36
|
|
|
protected $authStrategy; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param $username |
|
40
|
|
|
* @param $password |
|
41
|
|
|
* @param ClientInterface $clientInterface |
|
42
|
|
|
* @param AuthStrategyInterface $authStrategy |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
$username, |
|
46
|
|
|
$password, |
|
47
|
|
|
ClientInterface $clientInterface, |
|
48
|
|
|
AuthStrategyInterface $authStrategy |
|
49
|
|
|
) { |
|
50
|
|
|
$this->username = $username; |
|
51
|
|
|
$this->password = $password; |
|
52
|
|
|
|
|
53
|
|
|
$this->client = $clientInterface; |
|
|
|
|
|
|
54
|
|
|
$this->authStrategy = $authStrategy; |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $method |
|
60
|
|
|
* @param null $uri |
|
61
|
|
|
* @param array $options |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public function request($method, $uri = null, array $options = []) |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
$optionForToken = [ |
|
70
|
|
|
'headers' => [ |
|
71
|
|
|
'Authorization' => $this->authStrategy->authenticate($this->username, $this->password), |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$options = array_merge($options, $optionForToken); |
|
76
|
|
|
|
|
77
|
|
|
$response = $this->client->request($method, $uri, $options); |
|
78
|
|
|
|
|
79
|
|
|
if (!$this->needToRevalidate($response)) { |
|
|
|
|
|
|
80
|
|
|
return $response; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$optionForToken = [ |
|
84
|
|
|
'headers' => [ |
|
85
|
|
|
'Authorization' => $this->authStrategy->authenticate($this->username, $this->password), |
|
86
|
|
|
], |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
$options = array_merge($options, $optionForToken); |
|
90
|
|
|
|
|
91
|
|
|
return $this->client->request($method, $uri, $options); |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritDoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function send(RequestInterface $request, array $options = []) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->client->send($request, $options); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function sendAsync(RequestInterface $request, array $options = []) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->client->sendAsync($request, $options); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritDoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function requestAsync($method, $uri, array $options = []) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->client->requestAsync($method, $uri, $options); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritDoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getConfig($option = null) |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->client->getConfig($option); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param Response $response |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function needToRevalidate(Response $response) |
|
135
|
|
|
{ |
|
136
|
|
|
|
|
137
|
|
|
$content = json_decode($response->getBody()->getContents(), true); |
|
138
|
|
|
|
|
139
|
|
|
$response->getBody()->rewind(); |
|
140
|
|
|
|
|
141
|
|
|
return isset($content['response']['error_id']) && $content['response']['error_id'] == 'NOAUTH'; |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.