ProfileClientFactory::__construct()   A
last analyzed

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