XApiClientBuilder::setAuth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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 Guzzle\Http\Client;
15
use Guzzle\Plugin\Oauth\OauthPlugin;
16
use Xabbuh\XApi\Client\Request\Handler;
17
use Xabbuh\XApi\Serializer\ActorSerializer;
18
use Xabbuh\XApi\Serializer\DocumentDataSerializer;
19
use Xabbuh\XApi\Serializer\Serializer;
20
use Xabbuh\XApi\Serializer\SerializerRegistry;
21
use Xabbuh\XApi\Serializer\StatementResultSerializer;
22
use Xabbuh\XApi\Serializer\StatementSerializer;
23
24
/**
25
 * xAPI client builder.
26
 *
27
 * @author Christian Flothmann <[email protected]>
28
 */
29
class XApiClientBuilder implements XApiClientBuilderInterface
30
{
31
    private $baseUrl;
32
33
    private $version;
34
35
    private $username;
36
37
    private $password;
38
39
    private $oAuthCredentials;
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function setBaseUrl($baseUrl)
45
    {
46
        $this->baseUrl = $baseUrl;
47
48
        return $this;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function setVersion($version)
55
    {
56
        $this->version = $version;
57
58
        return $this;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function setAuth($username, $password)
65
    {
66
        $this->username = $username;
67
        $this->password = $password;
68
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
76
    {
77
        $this->oAuthCredentials = array(
78
            'consumer_key' => $consumerKey,
79
            'consumer_secret' => $consumerSecret,
80
            'token' => $token,
81
            'token_secret' => $tokenSecret,
82
        );
83
84
        return $this;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function build()
91
    {
92
        $httpClient = new Client($this->baseUrl);
93
94
        if (is_array($this->oAuthCredentials)) {
95
            $httpClient->addSubscriber(new OauthPlugin($this->oAuthCredentials));
96
        }
97
98
        $serializer = Serializer::createSerializer();
99
        $serializerRegistry = new SerializerRegistry();
100
        $serializerRegistry->setStatementSerializer(new StatementSerializer($serializer));
101
        $serializerRegistry->setStatementResultSerializer(new StatementResultSerializer($serializer));
102
        $serializerRegistry->setActorSerializer(new ActorSerializer($serializer));
103
        $serializerRegistry->setDocumentDataSerializer(new DocumentDataSerializer($serializer));
104
105
        $version = null === $this->version ? '1.0.1' : $this->version;
106
        $requestHandler = new Handler($httpClient, $version, $this->username, $this->password);
107
        $xApiClient = new XApiClient($requestHandler, $serializerRegistry, $this->version);
108
109
        return $xApiClient;
110
    }
111
}
112