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

XApiClientBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 10
dl 0
loc 103
ccs 0
cts 50
cp 0
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setVersion() 0 6 1
A setHttpClient() 0 4 1
A setRequestFactory() 0 4 1
A setBaseUrl() 0 6 1
A setAuth() 0 7 1
A setOAuthCredentials() 0 11 1
B build() 0 19 5
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