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