Completed
Push — http_plug ( 0a2d32 )
by Robert
10:27
created

ClientFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 13
nc 2
nop 2
1
<?php
2
3
namespace Rs\VersionEye\Http;
4
5
use Http\Client\Common\HttpMethodsClient;
6
use Http\Client\Plugin\AuthenticationPlugin;
7
use Http\Client\Plugin\DecoderPlugin;
8
use Http\Client\Plugin\ErrorPlugin;
9
use Http\Client\Plugin\PluginClient;
10
use Http\Client\Plugin\RedirectPlugin;
11
use Http\Client\Plugin\RetryPlugin;
12
use Http\Discovery\HttpClientDiscovery;
13
use Http\Discovery\MessageFactoryDiscovery;
14
use Http\Message\Authentication\QueryParam;
15
16
/**
17
 * Factory for creating Http Client.
18
 *
19
 * @author Robert Schönthal <[email protected]>
20
 */
21
class ClientFactory
22
{
23
    /**
24
     * @param string $url
25
     * @param string $token
26
     *
27
     * @return HttpClient
28
     */
29
    public static function create($url, $token)
30
    {
31
        $plugins = [
32
            new RedirectPlugin(),
33
            new RetryPlugin(['retries' => 5]),
34
            new DecoderPlugin(),
35
            new ErrorPlugin(),
36
        ];
37
38
        if ($token) {
39
            $plugins[] = new AuthenticationPlugin(new QueryParam([
40
                'api_key' => $token,
41
            ]));
42
        }
43
44
        $client = new HttpMethodsClient(
45
            new PluginClient(HttpClientDiscovery::find(), $plugins),
46
            MessageFactoryDiscovery::find()
47
        );
48
49
        return new HttpPlugHttpAdapterClient($client, $url);
50
    }
51
}
52