Completed
Push — master ( f229c5...46e1b3 )
by Christian
04:33
created

XApiClientBuilder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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