1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dolibarr\Client; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
6
|
|
|
use Dolibarr\Client\HttpClient\AuthenticateHttpClient; |
7
|
|
|
use Dolibarr\Client\HttpClient\HttpClient; |
8
|
|
|
use Dolibarr\Client\HttpClient\Middleware\AuthenticationMiddleware; |
9
|
|
|
use Dolibarr\Client\Security\Authentication\Authentication; |
10
|
|
|
use Dolibarr\Client\Security\Authentication\DolibarrApiKeyRequester; |
11
|
|
|
use Dolibarr\Client\Service\LoginService; |
12
|
|
|
use GuzzleHttp\HandlerStack; |
13
|
|
|
use JMS\Serializer\Serializer; |
14
|
|
|
use JMS\Serializer\SerializerBuilder; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @package Dolibarr\Api\Client |
19
|
|
|
*/ |
20
|
|
|
final class ClientBuilder |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $baseUri; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Authentication |
30
|
|
|
*/ |
31
|
|
|
private $authentication; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
private $debug; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $baseUri The base uri of the api |
40
|
|
|
* @param Authentication $authentication The authentication to access the api |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
$baseUri, |
44
|
|
|
Authentication $authentication |
45
|
|
|
) { |
46
|
|
|
Assert::stringNotEmpty($baseUri, "The baseUri should not be empty"); |
47
|
|
|
|
48
|
|
|
$this->baseUri = $baseUri; |
49
|
|
|
$this->authentication = $authentication; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Client |
54
|
|
|
*/ |
55
|
|
|
public function build() |
56
|
|
|
{ |
57
|
|
|
return new Client( |
58
|
|
|
$this->createHttpClient(), |
59
|
|
|
$this->createSerializer() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param boolean $debug |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setDebug($debug) |
69
|
|
|
{ |
70
|
|
|
$this->debug = (bool)$debug; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Serializer |
77
|
|
|
*/ |
78
|
|
|
private function createSerializer() |
79
|
|
|
{ |
80
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
81
|
|
|
|
82
|
|
|
return SerializerBuilder::create() |
83
|
|
|
->addDefaultHandlers() |
84
|
|
|
->build(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return HttpClient |
89
|
|
|
*/ |
90
|
|
|
private function createHttpClient() |
91
|
|
|
{ |
92
|
|
|
$httpClient = new AuthenticateHttpClient( |
93
|
|
|
$this->clientOptions(), |
94
|
|
|
$this->defaultKeyRequester() |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return $httpClient; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return DolibarrApiKeyRequester |
102
|
|
|
*/ |
103
|
|
|
private function defaultKeyRequester() |
104
|
|
|
{ |
105
|
|
|
return new DolibarrApiKeyRequester( |
106
|
|
|
$this->loginService(), |
107
|
|
|
$this->authentication |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return LoginService |
113
|
|
|
*/ |
114
|
|
|
private function loginService() |
115
|
|
|
{ |
116
|
|
|
$client = new HttpClient($this->clientOptions()); |
117
|
|
|
|
118
|
|
|
return new LoginService($client, $this->createSerializer()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
private function clientOptions() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
'base_uri' => $this->baseUri, |
128
|
|
|
'debug' => $this->debug |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|