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\Adapter\Guzzle6\Client; |
15
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
16
|
|
|
use Http\Client\Common\PluginClient; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Message\Authentication\BasicAuth; |
19
|
|
|
use Http\Message\MessageFactory\GuzzleMessageFactory; |
20
|
|
|
use Http\Message\RequestFactory; |
21
|
|
|
use Xabbuh\XApi\Client\Request\Handler; |
22
|
|
|
use Xabbuh\XApi\Serializer\ActorSerializer; |
23
|
|
|
use Xabbuh\XApi\Serializer\DocumentDataSerializer; |
24
|
|
|
use Xabbuh\XApi\Serializer\Serializer; |
25
|
|
|
use Xabbuh\XApi\Serializer\SerializerRegistry; |
26
|
|
|
use Xabbuh\XApi\Serializer\StatementResultSerializer; |
27
|
|
|
use Xabbuh\XApi\Serializer\StatementSerializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* xAPI client builder. |
31
|
|
|
* |
32
|
|
|
* @author Christian Flothmann <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
final class XApiClientBuilder implements XApiClientBuilderInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var HttpClient|null |
38
|
|
|
*/ |
39
|
|
|
private $httpClient; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RequestFactory|null |
43
|
|
|
*/ |
44
|
|
|
private $requestFactory; |
45
|
|
|
|
46
|
|
|
private $baseUrl; |
47
|
|
|
private $version; |
48
|
|
|
private $username; |
49
|
|
|
private $password; |
50
|
|
|
private $oAuthCredentials; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function setHttpClient(HttpClient $httpClient) |
56
|
|
|
{ |
57
|
|
|
$this->httpClient = $httpClient; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function setRequestFactory(RequestFactory $requestFactory) |
64
|
|
|
{ |
65
|
|
|
$this->requestFactory = $requestFactory; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function setBaseUrl($baseUrl) |
72
|
|
|
{ |
73
|
|
|
$this->baseUrl = $baseUrl; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function setVersion($version) |
82
|
|
|
{ |
83
|
|
|
$this->version = $version; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function setAuth($username, $password) |
92
|
|
|
{ |
93
|
|
|
$this->username = $username; |
94
|
|
|
$this->password = $password; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret) |
103
|
|
|
{ |
104
|
|
|
$this->oAuthCredentials = array( |
105
|
|
|
'consumer_key' => $consumerKey, |
106
|
|
|
'consumer_secret' => $consumerSecret, |
107
|
|
|
'token' => $token, |
108
|
|
|
'token_secret' => $tokenSecret, |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function build() |
118
|
|
|
{ |
119
|
|
|
if (null === $this->baseUrl) { |
120
|
|
|
throw new \LogicException('Base URI value was not configured.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (is_array($this->oAuthCredentials)) { |
124
|
|
|
$httpClient->addSubscriber(new OauthPlugin($this->oAuthCredentials)); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$serializer = Serializer::createSerializer(); |
128
|
|
|
$serializerRegistry = new SerializerRegistry(); |
129
|
|
|
$serializerRegistry->setStatementSerializer(new StatementSerializer($serializer)); |
130
|
|
|
$serializerRegistry->setStatementResultSerializer(new StatementResultSerializer($serializer)); |
131
|
|
|
$serializerRegistry->setActorSerializer(new ActorSerializer($serializer)); |
132
|
|
|
$serializerRegistry->setDocumentDataSerializer(new DocumentDataSerializer($serializer)); |
133
|
|
|
|
134
|
|
|
$httpClient = $this->httpClient ?: new Client(); |
135
|
|
|
|
136
|
|
|
if (null !== $this->username && null !== $this->password) { |
137
|
|
|
$httpClient = new PluginClient($httpClient, array(new AuthenticationPlugin(new BasicAuth($this->username, $this->password)))); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$version = null === $this->version ? '1.0.1' : $this->version; |
141
|
|
|
$requestHandler = new Handler($httpClient, $this->requestFactory ?: new GuzzleMessageFactory(), $this->baseUrl, $version); |
142
|
|
|
|
143
|
|
|
return new XApiClient($requestHandler, $serializerRegistry, $this->version); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.