Completed
Push — master ( 57db8a...f718e5 )
by Tobias
06:48
created

ProfileClientFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 5.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 3
loc 53
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A createClient() 3 10 5

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 Http\HttplugBundle\Collector;
4
5
use Http\Client\Common\FlexibleHttpClient;
6
use Http\Client\HttpAsyncClient;
7
use Http\Client\HttpClient;
8
use Http\HttplugBundle\ClientFactory\ClientFactory;
9
use Psr\Http\Client\ClientInterface;
10
use Symfony\Component\Stopwatch\Stopwatch;
11
12
/**
13
 * The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient.
14
 *
15
 * @author Fabien Bourigault <[email protected]>
16
 *
17
 * @internal
18
 */
19
class ProfileClientFactory implements ClientFactory
20
{
21
    /**
22
     * @var ClientFactory|callable
23
     */
24
    private $factory;
25
26
    /**
27
     * @var Collector
28
     */
29
    private $collector;
30
31
    /**
32
     * @var Formatter
33
     */
34
    private $formatter;
35
36
    /**
37
     * @var Stopwatch
38
     */
39
    private $stopwatch;
40
41
    /**
42
     * @param ClientFactory|callable $factory
43
     * @param Collector              $collector
44
     * @param Formatter              $formatter
45
     * @param Stopwatch              $stopwatch
46
     */
47 5
    public function __construct($factory, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
48
    {
49 5
        if (!$factory instanceof ClientFactory && !is_callable($factory)) {
50
            throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
51
        }
52 5
        $this->factory = $factory;
53 5
        $this->collector = $collector;
54 5
        $this->formatter = $formatter;
55 5
        $this->stopwatch = $stopwatch;
56 5
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 5
    public function createClient(array $config = [])
62
    {
63 5
        $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config);
64
65 5 View Code Duplication
        if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66 2
            $client = new FlexibleHttpClient($client);
67
        }
68
69 5
        return new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
70
    }
71
}
72