XApiClientBuilder::setAuth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 ApiClients\Tools\Psr7\Oauth1\Definition\AccessToken;
15
use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerKey;
16
use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerSecret;
17
use ApiClients\Tools\Psr7\Oauth1\Definition\TokenSecret;
18
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
19
use Http\Client\Common\Plugin\AuthenticationPlugin;
20
use Http\Client\Common\PluginClient;
21
use Http\Client\HttpClient;
22
use Http\Discovery\HttpClientDiscovery;
23
use Http\Discovery\MessageFactoryDiscovery;
24
use Http\Message\Authentication\BasicAuth;
25
use Http\Message\RequestFactory;
26
use Xabbuh\Http\Authentication\OAuth1;
27
use Xabbuh\XApi\Client\Request\Handler;
28
use Xabbuh\XApi\Serializer\SerializerFactoryInterface;
29
use Xabbuh\XApi\Serializer\SerializerRegistry;
30
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory;
31
32
/**
33
 * xAPI client builder.
34
 *
35
 * @author Christian Flothmann <[email protected]>
36
 */
37
final class XApiClientBuilder implements XApiClientBuilderInterface
38
{
39
    private $serializerFactory;
40
41
    /**
42
     * @var HttpClient|null
43
     */
44
    private $httpClient;
45
46
    /**
47
     * @var RequestFactory|null
48
     */
49
    private $requestFactory;
50
51
    private $baseUrl;
52
    private $version;
53
    private $username;
54
    private $password;
55
    private $consumerKey;
56
    private $consumerSecret;
57
    private $accessToken;
58
    private $tokenSecret;
59
60
    public function __construct(SerializerFactoryInterface $serializerFactory = null)
61
    {
62
        $this->serializerFactory = $serializerFactory ?: new SerializerFactory();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function setHttpClient(HttpClient $httpClient)
69
    {
70
        $this->httpClient = $httpClient;
71
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setRequestFactory(RequestFactory $requestFactory)
79
    {
80
        $this->requestFactory = $requestFactory;
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function setBaseUrl($baseUrl)
89
    {
90
        $this->baseUrl = $baseUrl;
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function setVersion($version)
99
    {
100
        $this->version = $version;
101
102
        return $this;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function setAuth($username, $password)
109
    {
110
        $this->username = $username;
111
        $this->password = $password;
112
113
        return $this;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $tokenSecret)
120
    {
121
        $this->consumerKey = $consumerKey;
122
        $this->consumerSecret = $consumerSecret;
123
        $this->accessToken = $token;
124
        $this->tokenSecret = $tokenSecret;
125
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function build()
133
    {
134
        if (null === $this->httpClient && class_exists(HttpClientDiscovery::class)) {
135
            try {
136
                $this->httpClient = HttpClientDiscovery::find();
137
            } catch (\Exception $e) {
138
            }
139
        }
140
141
        if (null === $httpClient = $this->httpClient) {
142
            throw new \LogicException('No HTTP client was configured.');
143
        }
144
145
        if (null === $this->requestFactory && class_exists(MessageFactoryDiscovery::class)) {
146
            try {
147
                $this->requestFactory = MessageFactoryDiscovery::find();
148
            } catch (\Exception $e) {
149
            }
150
        }
151
152
        if (null === $this->requestFactory) {
153
            throw new \LogicException('No request factory was configured.');
154
        }
155
156
        if (null === $this->baseUrl) {
157
            throw new \LogicException('Base URI value was not configured.');
158
        }
159
160
        $serializerRegistry = new SerializerRegistry();
161
        $serializerRegistry->setStatementSerializer($this->serializerFactory->createStatementSerializer());
162
        $serializerRegistry->setStatementResultSerializer($this->serializerFactory->createStatementResultSerializer());
163
        $serializerRegistry->setActorSerializer($this->serializerFactory->createActorSerializer());
164
        $serializerRegistry->setDocumentDataSerializer($this->serializerFactory->createDocumentDataSerializer());
165
166
        $plugins = array();
167
168
        if (null !== $this->username && null !== $this->password) {
169
            $plugins[] = new AuthenticationPlugin(new BasicAuth($this->username, $this->password));
170
        }
171
172
        if (null !== $this->consumerKey && null !== $this->consumerSecret && null !== $this->accessToken && null !== $this->tokenSecret) {
173
            if (!class_exists(OAuth1::class)) {
174
                throw new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.');
175
            }
176
177
            $requestSigner = new RequestSigner(new ConsumerKey($this->consumerKey), new ConsumerSecret($this->consumerSecret));
178
            $oauth = new OAuth1($requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret));
179
            $plugins[] = new AuthenticationPlugin($oauth);
180
        }
181
182
        if (!empty($plugins)) {
183
            $httpClient = new PluginClient($httpClient, $plugins);
184
        }
185
186
        $version = null === $this->version ? '1.0.3' : $this->version;
187
        $requestHandler = new Handler($httpClient, $this->requestFactory, $this->baseUrl, $version);
188
189
        return new XApiClient($requestHandler, $serializerRegistry, $this->version);
190
    }
191
}
192