1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[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 Xabbuh\XApi\Client; |
13
|
|
|
|
14
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
15
|
|
|
use Http\Client\Common\PluginClient; |
16
|
|
|
use Http\Client\HttpClient; |
17
|
|
|
use Http\Message\Authentication\BasicAuth; |
18
|
|
|
use Http\Message\RequestFactory; |
19
|
|
|
use Xabbuh\XApi\Client\Request\Handler; |
20
|
|
|
use Xabbuh\XApi\Serializer\SerializerFactoryInterface; |
21
|
|
|
use Xabbuh\XApi\Serializer\SerializerRegistry; |
22
|
|
|
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* xAPI client builder. |
26
|
|
|
* |
27
|
|
|
* @author Christian Flothmann <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class XApiClientBuilder implements XApiClientBuilderInterface |
30
|
|
|
{ |
31
|
|
|
private $serializerFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var HttpClient|null |
35
|
|
|
*/ |
36
|
|
|
private $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var RequestFactory|null |
40
|
|
|
*/ |
41
|
|
|
private $requestFactory; |
42
|
|
|
|
43
|
|
|
private $baseUrl; |
44
|
|
|
private $version; |
45
|
|
|
private $username; |
46
|
|
|
private $password; |
47
|
|
|
private $oAuthCredentials; |
48
|
|
|
|
49
|
|
|
public function __construct(SerializerFactoryInterface $serializerFactory = null) |
50
|
|
|
{ |
51
|
|
|
$this->serializerFactory = $serializerFactory ?: new SerializerFactory(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function setHttpClient(HttpClient $httpClient) |
58
|
|
|
{ |
59
|
|
|
$this->httpClient = $httpClient; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function setRequestFactory(RequestFactory $requestFactory) |
68
|
|
|
{ |
69
|
|
|
$this->requestFactory = $requestFactory; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function setBaseUrl($baseUrl) |
78
|
|
|
{ |
79
|
|
|
$this->baseUrl = $baseUrl; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function setVersion($version) |
88
|
|
|
{ |
89
|
|
|
$this->version = $version; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function setAuth($username, $password) |
98
|
|
|
{ |
99
|
|
|
$this->username = $username; |
100
|
|
|
$this->password = $password; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
|
|
public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret) |
109
|
|
|
{ |
110
|
|
|
$this->oAuthCredentials = array( |
111
|
|
|
'consumer_key' => $consumerKey, |
112
|
|
|
'consumer_secret' => $consumerSecret, |
113
|
|
|
'token' => $token, |
114
|
|
|
'token_secret' => $tokenSecret, |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritDoc} |
122
|
|
|
*/ |
123
|
|
|
public function build() |
124
|
|
|
{ |
125
|
|
|
if (null === $httpClient = $this->httpClient) { |
126
|
|
|
throw new \LogicException('No HTTP client was configured.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (null === $this->requestFactory) { |
130
|
|
|
throw new \LogicException('No request factory was configured.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (null === $this->baseUrl) { |
134
|
|
|
throw new \LogicException('Base URI value was not configured.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$serializerRegistry = new SerializerRegistry(); |
138
|
|
|
$serializerRegistry->setStatementSerializer($this->serializerFactory->createStatementSerializer()); |
139
|
|
|
$serializerRegistry->setStatementResultSerializer($this->serializerFactory->createStatementResultSerializer()); |
140
|
|
|
$serializerRegistry->setActorSerializer($this->serializerFactory->createActorSerializer()); |
141
|
|
|
$serializerRegistry->setDocumentDataSerializer($this->serializerFactory->createDocumentDataSerializer()); |
142
|
|
|
|
143
|
|
|
if (null !== $this->username && null !== $this->password) { |
144
|
|
|
$httpClient = new PluginClient($httpClient, array(new AuthenticationPlugin(new BasicAuth($this->username, $this->password)))); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$version = null === $this->version ? '1.0.1' : $this->version; |
148
|
|
|
$requestHandler = new Handler($httpClient, $this->requestFactory, $this->baseUrl, $version); |
149
|
|
|
|
150
|
|
|
return new XApiClient($requestHandler, $serializerRegistry, $this->version); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|