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\Connector\Client\ClientFactoryInterface; |
12
|
|
|
use WebservicesNl\Platform\PlatformConfigInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HttpClientFactory. |
16
|
|
|
* Helper class the Webservices connector generator with instantiating a PSR-7 curl client. |
17
|
|
|
*/ |
18
|
|
|
class GuzzleClientFactory implements ClientFactoryInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PlatformConfigInterface |
24
|
|
|
*/ |
25
|
|
|
private $platform; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* HttpClientFactory constructor. |
29
|
|
|
* |
30
|
|
|
* @param PlatformConfigInterface $platform |
31
|
|
|
* @param LoggerInterface|null $logger |
32
|
|
|
*/ |
33
|
|
|
public function __construct(PlatformConfigInterface $platform, LoggerInterface $logger = null) |
34
|
|
|
{ |
35
|
|
|
$this->platform = $platform; |
36
|
|
|
$this->logger = $logger; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a static instance (LSB) of HttpClientFactory. |
41
|
|
|
* |
42
|
|
|
* @param PlatformConfigInterface $platform |
43
|
|
|
* @param LoggerInterface $logger |
44
|
|
|
* |
45
|
|
|
* @return GuzzleClientFactory |
46
|
|
|
*/ |
47
|
|
|
public static function build(PlatformConfigInterface $platform, LoggerInterface $logger = null) |
48
|
|
|
{ |
49
|
|
|
return new static($platform, $logger); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create and configure a http curl client. |
54
|
|
|
* |
55
|
|
|
* @param array $settings additional settings |
56
|
|
|
* |
57
|
|
|
* @return Client |
58
|
|
|
*/ |
59
|
|
|
public function create(array $settings = []) |
60
|
|
|
{ |
61
|
|
|
$stack = null; |
62
|
|
|
$settings = $this->platform->toArray() + $settings; |
63
|
|
|
|
64
|
|
|
if ($this->getLogger() instanceof LoggerInterface) { |
65
|
|
|
$stack = HandlerStack::create(); |
66
|
|
|
$stack->push(Middleware::log($this->getLogger(), new MessageFormatter('{request} - {response}'))); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new Client( |
|
|
|
|
70
|
|
|
[ |
71
|
|
|
'base_url' => (string)$settings['url'], |
72
|
|
|
'handler' => $stack, |
73
|
|
|
'exceptions' => (bool)$settings['exceptions'], |
74
|
|
|
'timeout' => (float)$settings['responseTimeout'], |
75
|
|
|
'connection_timeout' => (float)$settings['connectionTimeout'], |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns this LoggerInterface |
82
|
|
|
* |
83
|
|
|
* @return LoggerInterface |
84
|
|
|
*/ |
85
|
|
|
public function getLogger() |
86
|
|
|
{ |
87
|
|
|
return $this->logger; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.