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

Guzzle6ApiClientFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 8
loc 64
ccs 15
cts 20
cp 0.75
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiClientClass() 0 4 1
A getGuzzleBaseUriOptionKey() 0 4 1
A createTokenMiddleware() 0 4 1
A createHttpClient() 0 13 2
A createSimple() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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