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
|
|
|
|
15
|
|
|
use Adlogix\ConfluenceClient\Exception\ExceptionWrapper; |
16
|
|
|
use Adlogix\ConfluenceClient\HttpClient\HttpClientInterface; |
17
|
|
|
use Adlogix\ConfluenceClient\Service\AuthenticationService; |
18
|
|
|
use Adlogix\ConfluenceClient\Service\ContentService; |
19
|
|
|
use Adlogix\ConfluenceClient\Service\DescriptorService; |
20
|
|
|
use Adlogix\ConfluenceClient\Service\ServiceInterface; |
21
|
|
|
use Adlogix\ConfluenceClient\Service\SpaceService; |
22
|
|
|
use GuzzleHttp\Exception\RequestException; |
23
|
|
|
use JMS\Serializer\SerializerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Client |
27
|
|
|
* @package Adlogix\ConfluenceClient |
28
|
|
|
* @author Cedric Michaux <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @method SpaceService spaces |
31
|
|
|
* @method ContentService pages |
32
|
|
|
* @method AuthenticationService authentication |
33
|
|
|
*/ |
34
|
|
|
class Client |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var HttpClientInterface |
39
|
|
|
*/ |
40
|
|
|
private $httpClient; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SerializerInterface |
44
|
|
|
*/ |
45
|
|
|
private $serializer; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Client constructor. |
50
|
|
|
* |
51
|
|
|
* @param HttpClientInterface $httpClient |
52
|
|
|
* @param SerializerInterface $serializer |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
HttpClientInterface $httpClient, |
56
|
|
|
SerializerInterface $serializer |
57
|
|
|
) { |
58
|
|
|
$this->httpClient = $httpClient; |
59
|
|
|
$this->serializer = $serializer; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
* @param $args |
65
|
|
|
* |
66
|
|
|
* @return ServiceInterface |
67
|
|
|
*/ |
68
|
|
|
public function __call($name, $args) |
69
|
|
|
{ |
70
|
|
|
return $this->service($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $name |
75
|
|
|
* |
76
|
|
|
* @return ServiceInterface |
77
|
|
|
*/ |
78
|
|
|
public function service($name) |
79
|
|
|
{ |
80
|
|
|
switch ($name) { |
81
|
|
|
case 'space': |
82
|
|
|
case 'spaces': |
83
|
|
|
return new SpaceService($this->serializer, $this->httpClient); |
84
|
|
|
|
85
|
|
|
case 'content': |
86
|
|
|
case 'contents': |
87
|
|
|
return new ContentService($this->serializer, $this->httpClient); |
88
|
|
|
|
89
|
|
|
default: |
90
|
|
|
throw new \InvalidArgumentException(sprintf('Undefined service instance called: %s', $name)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $method |
96
|
|
|
* @param string $uri |
97
|
|
|
* @param null $json |
98
|
|
|
* @param array $options |
99
|
|
|
* |
100
|
|
|
* @return \GuzzleHttp\Psr7\Response |
101
|
|
|
*/ |
102
|
|
|
public function sendRawApiRequest($method, $uri, $json = null, array $options = []) |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
return $this->httpClient->apiRequest($method, $uri, $json, $options); |
106
|
|
|
|
107
|
|
|
} catch (RequestException $exception) { |
108
|
|
|
throw ExceptionWrapper::wrap($exception, $this->serializer); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $url |
115
|
|
|
* |
116
|
|
|
* @param array $options |
117
|
|
|
* |
118
|
|
|
* @return \GuzzleHttp\Psr7\Response |
119
|
|
|
*/ |
120
|
|
|
public function downloadAttachment($url, $options = []) |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$url = str_replace(" ", "%20", $url); |
124
|
|
|
return $this->httpClient->attachmentRequest($url, $options); |
125
|
|
|
} catch (RequestException $exception) { |
126
|
|
|
throw ExceptionWrapper::wrap($exception, $this->serializer); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|