Completed
Push — master ( 8b73f7...9e08cd )
by Ruben
02:59
created

Guzzle6ApiClientFactory::createHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
crap 2
1
<?php
2
3
namespace MovingImage\Client\VMPro\Factory;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\HandlerStack;
8
use MovingImage\Client\VMPro\ApiClient\Guzzle6ApiClient;
9
use MovingImage\Client\VMPro\Entity\ApiCredentials;
10
use MovingImage\Client\VMPro\Manager\TokenManager;
11
use MovingImage\Client\VMPro\Middleware\TokenMiddleware;
12
13
/**
14
 * Class ApiClientFactory.
15
 *
16
 * @author Ruben Knol <[email protected]>
17
 */
18
class Guzzle6ApiClientFactory extends AbstractApiClientFactory
19
{
20
    /**
21
     * Use the Guzzle6-specific API client class.
22
     *
23
     * @return string
24
     */
25 1
    protected function getApiClientClass()
26
    {
27 1
        return Guzzle6ApiClient::class;
28
    }
29
30 1
    protected function getGuzzleBaseUriOptionKey()
31
    {
32 1
        return 'base_uri';
33
    }
34
35
    /**
36
     * Instantiate a TokenMiddleware instance with a TokenManager.
37
     *
38
     * @param TokenManager $tokenManager
39
     *
40
     * @return TokenMiddleware
41
     */
42 2
    public function createTokenMiddleware(TokenManager $tokenManager)
43
    {
44 2
        return new TokenMiddleware($tokenManager);
45
    }
46
47
    /**
48
     * Method to instantiate a HTTP client.
49
     *
50
     * @param string $baseUri
51
     * @param array  $middlewares
52
     * @param array  $options
53
     *
54
     * @return ClientInterface
55
     */
56 1
    public function createHttpClient($baseUri, array $middlewares = [], array $options = [])
57
    {
58 1
        $stack = HandlerStack::create();
59
60 1
        foreach ($middlewares as $middleware) {
61 1
            $stack->push($middleware);
62 1
        }
63
64 1
        return new Client(array_merge([
65 1
            'base_uri' => $baseUri,
66 1
            'handler' => $stack,
67 1
        ], $options));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    public function createSimple($baseUri, ApiCredentials $credentials)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $tokenManager = $this->createTokenManager($baseUri, $credentials);
76
        $tokenMiddleware = $this->createTokenMiddleware($tokenManager);
77
        $httpClient = $this->createHttpClient($baseUri, [$tokenMiddleware]);
78
79
        return $this->create($httpClient, $this->createSerializer());
80
    }
81
}
82