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)) { |
|
|
|
|
66
|
2 |
|
$client = new FlexibleHttpClient($client); |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
return new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.