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

ClientFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
lcom 0
cbo 11
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 22 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