1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OrcaServices\NovaApi\Factory; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Handler\MockHandler; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use OrcaServices\NovaApi\Client\NovaHttpClient; |
9
|
|
|
use OrcaServices\NovaApi\Configuration\NovaApiConfiguration; |
10
|
|
|
use OrcaServices\NovaApi\Method\NovaLoginMethod; |
11
|
|
|
use OrcaServices\NovaApi\Parser\NovaApiErrorParser; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Factory. |
16
|
|
|
*/ |
17
|
|
|
final class NovaHttpClientFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var NovaApiConfiguration Nova settings |
21
|
|
|
*/ |
22
|
|
|
private $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var NovaApiErrorParser |
26
|
|
|
*/ |
27
|
|
|
private $novaErrorParser; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $mockedResponses = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var HandlerStack|null |
36
|
|
|
*/ |
37
|
|
|
private $handler; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The constructor. |
41
|
|
|
* |
42
|
|
|
* @param NovaApiConfiguration $configuration The settings |
43
|
|
|
* @param NovaApiErrorParser $novaErrorParser The error parser |
44
|
|
|
*/ |
45
|
13 |
|
public function __construct( |
46
|
|
|
NovaApiConfiguration $configuration, |
47
|
|
|
NovaApiErrorParser $novaErrorParser |
48
|
|
|
) { |
49
|
13 |
|
$this->configuration = $configuration; |
50
|
13 |
|
$this->novaErrorParser = $novaErrorParser; |
51
|
13 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a http client with logged-in session. |
55
|
|
|
* |
56
|
|
|
* @return NovaHttpClient The SOAP client |
57
|
|
|
*/ |
58
|
13 |
|
public function createLoggedInHttpClient(): NovaHttpClient |
59
|
|
|
{ |
60
|
|
|
// Create a guzzle client for the single sign on server. |
61
|
13 |
|
$novaSsoSettings = $this->configuration->getWebServiceSsoClientSettings(); |
62
|
13 |
|
$ssoHttpClient = $this->createHttpClient($novaSsoSettings); |
63
|
|
|
|
64
|
13 |
|
$username = $novaSsoSettings['client_id']; |
65
|
13 |
|
$password = $novaSsoSettings['client_secret']; |
66
|
|
|
|
67
|
13 |
|
$loginMethod = new NovaLoginMethod($ssoHttpClient, $this->novaErrorParser); |
68
|
|
|
|
69
|
13 |
|
$accessToken = $loginMethod->login($username, $password); |
70
|
13 |
|
$authorization = sprintf('Bearer %s', $accessToken); |
71
|
|
|
|
72
|
|
|
// Create the soap webservice client |
73
|
13 |
|
$webServiceSettings = $this->configuration->getWebServiceClientSettings(); |
74
|
13 |
|
$webServiceSettings = array_replace_recursive( |
75
|
13 |
|
$webServiceSettings, |
76
|
|
|
[ |
77
|
13 |
|
'headers' => ['Authorization' => $authorization], |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
13 |
|
return $this->createHttpClient($webServiceSettings); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a guzzle client for the API to use. |
86
|
|
|
* |
87
|
|
|
* @param array $settings The settings |
88
|
|
|
* |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
* |
91
|
|
|
* @return NovaHttpClient The http client |
92
|
|
|
*/ |
93
|
13 |
|
private function createHttpClient(array $settings): NovaHttpClient |
94
|
|
|
{ |
95
|
13 |
|
if ($this->mockedResponses || empty($settings['base_uri']) || $settings['base_uri'] === 'http://localhost/') { |
|
|
|
|
96
|
|
|
// Use the same mocked handler stack for login and the endpoint client for testing |
97
|
13 |
|
$this->handler = $this->handler ?: HandlerStack::create(new MockHandler($this->mockedResponses)); |
98
|
13 |
|
$settings['base_uri'] = 'http://localhost'; |
99
|
13 |
|
$settings['handler'] = $this->handler; |
100
|
|
|
} |
101
|
|
|
|
102
|
13 |
|
return new NovaHttpClient($settings); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set mocked responses. |
107
|
|
|
* |
108
|
|
|
* @param ResponseInterface[] $responses The responses |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
13 |
|
public function setMockedResponses(array $responses) |
113
|
|
|
{ |
114
|
13 |
|
$this->mockedResponses = $responses; |
115
|
13 |
|
} |
116
|
|
|
} |
117
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.