Completed
Pull Request — master (#10)
by Christian
02:47
created

XApiClientBuilder::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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\ActorSerializer;
21
use Xabbuh\XApi\Serializer\DocumentDataSerializer;
22
use Xabbuh\XApi\Serializer\Serializer;
23
use Xabbuh\XApi\Serializer\SerializerRegistry;
24
use Xabbuh\XApi\Serializer\StatementResultSerializer;
25
use Xabbuh\XApi\Serializer\StatementSerializer;
26
27
/**
28
 * xAPI client builder.
29
 *
30
 * @author Christian Flothmann <[email protected]>
31
 */
32
final class XApiClientBuilder implements XApiClientBuilderInterface
33
{
34
    /**
35
     * @var HttpClient|null
36
     */
37
    private $httpClient;
38
39
    /**
40
     * @var RequestFactory|null
41
     */
42
    private $requestFactory;
43
44
    private $baseUrl;
45
    private $version;
46
    private $username;
47
    private $password;
48
    private $oAuthCredentials;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function setHttpClient(HttpClient $httpClient)
54
    {
55
        $this->httpClient = $httpClient;
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function setRequestFactory(RequestFactory $requestFactory)
64
    {
65
        $this->requestFactory = $requestFactory;
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function setBaseUrl($baseUrl)
74
    {
75
        $this->baseUrl = $baseUrl;
76
77
        return $this;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function setVersion($version)
84
    {
85
        $this->version = $version;
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function setAuth($username, $password)
94
    {
95
        $this->username = $username;
96
        $this->password = $password;
97
98
        return $this;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
105
    {
106
        $this->oAuthCredentials = array(
107
            'consumer_key' => $consumerKey,
108
            'consumer_secret' => $consumerSecret,
109
            'token' => $token,
110
            'token_secret' => $tokenSecret,
111
        );
112
113
        return $this;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function build()
120
    {
121
        if (null === $httpClient = $this->httpClient) {
122
            throw new \LogicException('No HTTP client was configured.');
123
        }
124
125
        if (null === $this->requestFactory) {
126
            throw new \LogicException('No request factory was configured.');
127
        }
128
129
        if (null === $this->baseUrl) {
130
            throw new \LogicException('Base URI value was not configured.');
131
        }
132
133
        if (is_array($this->oAuthCredentials)) {
134
            $httpClient->addSubscriber(new OauthPlugin($this->oAuthCredentials));
0 ignored issues
show
Bug introduced by
The method addSubscriber() does not seem to exist on object<Http\Client\HttpClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        }
136
137
        $serializer = Serializer::createSerializer();
138
        $serializerRegistry = new SerializerRegistry();
139
        $serializerRegistry->setStatementSerializer(new StatementSerializer($serializer));
140
        $serializerRegistry->setStatementResultSerializer(new StatementResultSerializer($serializer));
141
        $serializerRegistry->setActorSerializer(new ActorSerializer($serializer));
142
        $serializerRegistry->setDocumentDataSerializer(new DocumentDataSerializer($serializer));
143
144
        if (null !== $this->username && null !== $this->password) {
145
            $httpClient = new PluginClient($httpClient, array(new AuthenticationPlugin(new BasicAuth($this->username, $this->password))));
146
        }
147
148
        $version = null === $this->version ? '1.0.1' : $this->version;
149
        $requestHandler = new Handler($httpClient, $this->requestFactory, $this->baseUrl, $version);
150
151
        return new XApiClient($requestHandler, $serializerRegistry, $this->version);
152
    }
153
}
154