Completed
Pull Request — master (#10)
by Christian
08:22 queued 06:26
created

XApiClientBuilder::setVersion()   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
dl 0
loc 6
c 0
b 0
f 0
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\Adapter\Guzzle6\Client;
15
use Http\Client\HttpClient;
16
use Http\Message\MessageFactory\GuzzleMessageFactory;
17
use Http\Message\RequestFactory;
18
use Xabbuh\XApi\Client\Request\Handler;
19
use Xabbuh\XApi\Serializer\ActorSerializer;
20
use Xabbuh\XApi\Serializer\DocumentDataSerializer;
21
use Xabbuh\XApi\Serializer\Serializer;
22
use Xabbuh\XApi\Serializer\SerializerRegistry;
23
use Xabbuh\XApi\Serializer\StatementResultSerializer;
24
use Xabbuh\XApi\Serializer\StatementSerializer;
25
26
/**
27
 * xAPI client builder.
28
 *
29
 * @author Christian Flothmann <[email protected]>
30
 */
31
final class XApiClientBuilder implements XApiClientBuilderInterface
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
    /**
50
     * {@inheritdoc}
51
     */
52
    public function setHttpClient(HttpClient $httpClient)
53
    {
54
        $this->httpClient = $httpClient;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setRequestFactory(RequestFactory $requestFactory)
61
    {
62
        $this->requestFactory = $requestFactory;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function setBaseUrl($baseUrl)
69
    {
70
        $this->baseUrl = $baseUrl;
71
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function setVersion($version)
79
    {
80
        $this->version = $version;
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function setAuth($username, $password)
89
    {
90
        $this->username = $username;
91
        $this->password = $password;
92
93
        return $this;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
100
    {
101
        $this->oAuthCredentials = array(
102
            'consumer_key' => $consumerKey,
103
            'consumer_secret' => $consumerSecret,
104
            'token' => $token,
105
            'token_secret' => $tokenSecret,
106
        );
107
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function build()
115
    {
116
        if (is_array($this->oAuthCredentials)) {
117
            $httpClient->addSubscriber(new OauthPlugin($this->oAuthCredentials));
0 ignored issues
show
Bug introduced by
The variable $httpClient does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
118
        }
119
120
        $serializer = Serializer::createSerializer();
121
        $serializerRegistry = new SerializerRegistry();
122
        $serializerRegistry->setStatementSerializer(new StatementSerializer($serializer));
123
        $serializerRegistry->setStatementResultSerializer(new StatementResultSerializer($serializer));
124
        $serializerRegistry->setActorSerializer(new ActorSerializer($serializer));
125
        $serializerRegistry->setDocumentDataSerializer(new DocumentDataSerializer($serializer));
126
127
        $version = null === $this->version ? '1.0.1' : $this->version;
128
        $requestHandler = new Handler($this->httpClient ?: new Client(), $this->requestFactory ?: new GuzzleMessageFactory(), $this->baseUrl, $version, $this->username, $this->password);
129
        $xApiClient = new XApiClient($requestHandler, $serializerRegistry, $this->version);
130
131
        return $xApiClient;
132
    }
133
}
134