Completed
Pull Request — master (#10)
by Christian
03:08 queued 01:07
created

XApiClientBuilder::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function setRequestFactory(RequestFactory $requestFactory)
62
    {
63
        $this->requestFactory = $requestFactory;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function setBaseUrl($baseUrl)
70
    {
71
        $this->baseUrl = $baseUrl;
72
73
        return $this;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function setVersion($version)
80
    {
81
        $this->version = $version;
82
83
        return $this;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function setAuth($username, $password)
90
    {
91
        $this->username = $username;
92
        $this->password = $password;
93
94
        return $this;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
101
    {
102
        $this->oAuthCredentials = array(
103
            'consumer_key' => $consumerKey,
104
            'consumer_secret' => $consumerSecret,
105
            'token' => $token,
106
            'token_secret' => $tokenSecret,
107
        );
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function build()
116
    {
117
        if (null === $httpClient = $this->httpClient) {
118
            throw new \LogicException('No HTTP client was configured.');
119
        }
120
121
        if (null === $this->requestFactory) {
122
            throw new \LogicException('No request factory was configured.');
123
        }
124
125
        if (null === $this->baseUrl) {
126
            throw new \LogicException('Base URI value was not configured.');
127
        }
128
129
        if (is_array($this->oAuthCredentials)) {
130
            $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...
131
        }
132
133
        $serializer = Serializer::createSerializer();
134
        $serializerRegistry = new SerializerRegistry();
135
        $serializerRegistry->setStatementSerializer(new StatementSerializer($serializer));
136
        $serializerRegistry->setStatementResultSerializer(new StatementResultSerializer($serializer));
137
        $serializerRegistry->setActorSerializer(new ActorSerializer($serializer));
138
        $serializerRegistry->setDocumentDataSerializer(new DocumentDataSerializer($serializer));
139
140
        if (null !== $this->username && null !== $this->password) {
141
            $httpClient = new PluginClient($httpClient, array(new AuthenticationPlugin(new BasicAuth($this->username, $this->password))));
142
        }
143
144
        $version = null === $this->version ? '1.0.1' : $this->version;
145
        $requestHandler = new Handler($httpClient, $this->requestFactory, $this->baseUrl, $version);
146
147
        return new XApiClient($requestHandler, $serializerRegistry, $this->version);
148
    }
149
}
150