1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Adlogix package. |
4
|
|
|
* |
5
|
|
|
* (c) Allan Segebarth <[email protected]> |
6
|
|
|
* (c) Jean-Jacques Courtens <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Adlogix\ConfluenceClient; |
13
|
|
|
|
14
|
|
|
use Adlogix\ConfluenceClient\HttpClient\HttpClient; |
15
|
|
|
use Adlogix\GuzzleAtlassianConnect\Middleware\ConnectMiddleware; |
16
|
|
|
use Adlogix\GuzzleAtlassianConnect\Security\AuthenticationInterface; |
17
|
|
|
use GuzzleHttp\HandlerStack; |
18
|
|
|
use JMS\Serializer\SerializerBuilder; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ClientBuilder |
23
|
|
|
* @package Adlogix\ConfluenceClient |
24
|
|
|
* @author Cedric Michaux <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ClientBuilder |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $baseUri; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AuthenticationInterface |
36
|
|
|
*/ |
37
|
|
|
private $authentication; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var SerializerInterface |
41
|
|
|
*/ |
42
|
|
|
private $serializer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
private $debug; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $baseUri |
52
|
|
|
* @param AuthenticationInterface $authenticationMethod |
53
|
|
|
* |
54
|
|
|
* @return ClientBuilder |
55
|
|
|
*/ |
56
|
|
|
public static function create($baseUri, AuthenticationInterface $authenticationMethod) |
57
|
|
|
{ |
58
|
|
|
return new static($baseUri, $authenticationMethod); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* ClientBuilder constructor. |
63
|
|
|
* |
64
|
|
|
* @param $baseUri |
65
|
|
|
* @param AuthenticationInterface $authentication |
66
|
|
|
*/ |
67
|
|
|
private function __construct($baseUri, AuthenticationInterface $authentication) |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
if (empty($baseUri)) { |
71
|
|
|
throw new \InvalidArgumentException("The baseUri cannot be empty"); |
72
|
|
|
} |
73
|
|
|
$this->baseUri = $baseUri; |
74
|
|
|
$this->authentication = $authentication; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Client |
79
|
|
|
*/ |
80
|
|
|
public function build() |
81
|
|
|
{ |
82
|
|
|
$this->serializer = $this->serializer ?: $this->buildDefaultSerializer(); |
83
|
|
|
$stack = HandlerStack::create(); |
84
|
|
|
$stack->push(new ConnectMiddleware($this->authentication, $this->baseUri)); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
return new Client( |
88
|
|
|
new HttpClient([ |
89
|
|
|
'base_uri' => $this->baseUri, |
90
|
|
|
'handler' => $stack, |
91
|
|
|
'debug' => $this->debug |
92
|
|
|
]), |
93
|
|
|
$this->serializer |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param SerializerInterface $serializer |
99
|
|
|
*/ |
100
|
|
|
public function setSerializer(SerializerInterface $serializer) |
101
|
|
|
{ |
102
|
|
|
$this->serializer = $serializer; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param boolean $debug |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setDebug($debug) |
112
|
|
|
{ |
113
|
|
|
$this->debug = (bool)$debug; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function buildDefaultSerializer() |
118
|
|
|
{ |
119
|
|
|
return SerializerBuilder::create() |
120
|
|
|
->addMetadataDir(__DIR__ . '/Resources/serializer', __NAMESPACE__) |
121
|
|
|
->addDefaultHandlers() |
122
|
|
|
->build(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|