1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebservicesNl\Protocol\Soap\Helper; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\MessageFormatter; |
8
|
|
|
use GuzzleHttp\Middleware; |
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use WebservicesNl\Platform\PlatformConfigInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HttpClientFactory. |
15
|
|
|
* |
16
|
|
|
* Helper class the Webservices connector generator with instantiating a PSR-7 curl client. |
17
|
|
|
*/ |
18
|
|
|
class GuzzleClientFactory |
19
|
|
|
{ |
20
|
|
|
use LoggerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PlatformConfigInterface |
24
|
|
|
*/ |
25
|
|
|
private $platform; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* HttpClientFactory constructor. |
29
|
|
|
* |
30
|
|
|
* @param PlatformConfigInterface $platform |
31
|
|
|
*/ |
32
|
7 |
|
public function __construct(PlatformConfigInterface $platform) |
33
|
|
|
{ |
34
|
7 |
|
$this->platform = $platform; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a static instance (LSB) of HttpClientFactory. |
39
|
|
|
* |
40
|
|
|
* @param PlatformConfigInterface $platform |
41
|
|
|
* |
42
|
|
|
* @return GuzzleClientFactory |
43
|
|
|
*/ |
44
|
7 |
|
public static function build(PlatformConfigInterface $platform) |
45
|
|
|
{ |
46
|
7 |
|
return new static($platform); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create and configure a http curl client. |
51
|
|
|
* |
52
|
|
|
* @param array $settings additional settings |
53
|
|
|
* |
54
|
|
|
* @return Client |
55
|
|
|
*/ |
56
|
7 |
|
public function create(array $settings = []) |
57
|
|
|
{ |
58
|
7 |
|
$stack = null; |
59
|
7 |
|
$settings += $this->platform->toArray(); |
60
|
|
|
|
61
|
7 |
|
if ($this->getLogger() instanceof LoggerInterface) { |
62
|
3 |
|
$stack = HandlerStack::create(); |
63
|
3 |
|
$stack->push(Middleware::log($this->getLogger(), new MessageFormatter('{request} - {response}'))); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
7 |
|
return new Client( |
67
|
|
|
[ |
68
|
7 |
|
'base_url' => (string)$settings['url'], |
69
|
7 |
|
'handler' => $stack, |
70
|
7 |
|
'exceptions' => false, |
71
|
7 |
|
'timeout' => (float)$settings['responseTimeout'], |
72
|
7 |
|
'connection_timeout' => (float)$settings['connectionTimeout'], |
73
|
|
|
'headers' => [ |
74
|
7 |
|
'User-Agent' => $settings['userAgent'] |
75
|
7 |
|
] |
76
|
7 |
|
] |
77
|
7 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns this LoggerInterface. |
82
|
|
|
* |
83
|
|
|
* @return LoggerInterface |
84
|
|
|
*/ |
85
|
7 |
|
public function getLogger() |
86
|
|
|
{ |
87
|
7 |
|
return $this->logger; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|