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

ProfileClientFactory::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 4
crap 3.0175
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