1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of PHP CS Fixer. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* Dariusz Rumiński <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the MIT license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Etrias\EwarehousingConnector\Client; |
14
|
|
|
|
15
|
|
|
use Etrias\EwarehousingConnector\Client\Middleware\AuthenticationMiddleware; |
16
|
|
|
use Etrias\EwarehousingConnector\Services\AuthenticationServiceInterface; |
17
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
18
|
|
|
use GuzzleHttp\HandlerStack; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
class ClientFactory |
22
|
|
|
{ |
23
|
|
|
/** @var HandlerStack */ |
24
|
|
|
protected $handlerStack; |
25
|
|
|
|
26
|
|
|
/** @var AuthenticationServiceInterface */ |
27
|
|
|
protected $authenticationService; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
protected $config = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public static function create() |
36
|
|
|
{ |
37
|
|
|
return new static(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function addDefaultHandlerStack() |
44
|
|
|
{ |
45
|
|
|
$handler = new CurlHandler(); |
46
|
|
|
$this->handlerStack = HandlerStack::create($handler); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param callable $callable |
53
|
|
|
* @param string $name |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function pushToHandlerStack(callable $callable, $name = '') |
58
|
|
|
{ |
59
|
|
|
if ($this->handlerStack === null) { |
60
|
|
|
$this->addDefaultHandlerStack(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->handlerStack->push($callable, $name); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return HandlerStack |
70
|
|
|
*/ |
71
|
|
|
public function getHandlerStack() |
72
|
|
|
{ |
73
|
|
|
if ($this->handlerStack === null) { |
74
|
|
|
$this->addDefaultHandlerStack(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->handlerStack; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param HandlerStack $handlerStack |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function setHandlerStack(HandlerStack $handlerStack) |
86
|
|
|
{ |
87
|
|
|
$this->handlerStack = $handlerStack; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param AuthenticationServiceInterface $authenticationService |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setAuthenticationService(AuthenticationServiceInterface $authenticationService) |
98
|
|
|
{ |
99
|
|
|
$this->authenticationService = $authenticationService; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $config |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setConfig(array $config) |
110
|
|
|
{ |
111
|
|
|
$this->config = $config; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return EwarehousingClient |
118
|
|
|
*/ |
119
|
|
|
public function build() |
120
|
|
|
{ |
121
|
|
|
if ($this->handlerStack === null) { |
122
|
|
|
$this->addDefaultHandlerStack(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->config = array_merge($this->config, ['handler' => $this->handlerStack]); |
126
|
|
|
|
127
|
|
|
if ($this->authenticationService === null) { |
128
|
|
|
throw new RuntimeException('There is no authenticationService set for Ewarehousing connector'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->handlerStack->push(new AuthenticationMiddleware($this->authenticationService)); |
132
|
|
|
|
133
|
|
|
$client = new EwarehousingClient($this->config); |
134
|
|
|
|
135
|
|
|
return $client; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|