Completed
Push — master ( 217056...f710b9 )
by Tobias
12:16
created

PluginClientFactory::createClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 3
crap 3
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\Common\PluginClient;
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Symfony\Component\Stopwatch\Stopwatch;
10
11
/**
12
 * This factory is used as a replacement for Http\Client\Common\PluginClientFactory when profiling is enabled. It
13
 * creates PluginClient instances with all profiling decorators and extra plugins.
14
 *
15
 * @author Fabien Bourigault <[email protected]>
16
 *
17
 * @internal
18
 */
19
final class PluginClientFactory
20
{
21
    /**
22
     * @var Collector
23
     */
24
    private $collector;
25
26
    /**
27
     * @var Formatter
28
     */
29
    private $formatter;
30
31
    /**
32
     * @var Stopwatch
33
     */
34
    private $stopwatch;
35
36
    /**
37
     * @param Collector $collector
38
     * @param Formatter $formatter
39
     * @param Stopwatch $stopwatch
40
     */
41 6
    public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
42
    {
43 6
        $this->collector = $collector;
44 6
        $this->formatter = $formatter;
45 6
        $this->stopwatch = $stopwatch;
46 6
    }
47
48
    /**
49
     * @param HttpClient|HttpAsyncClient $client
50
     * @param Plugin[]                   $plugins
51
     * @param array                      $options {
52
     *
53
     *     @var string $client_name to give client a name which may be used when displaying client information  like in
54
     *         the HTTPlugBundle profiler.
55
     * }
56
     *
57
     * @see PluginClient constructor for PluginClient specific $options.
58
     *
59
     * @return PluginClient
60
     */
61
    public function createClient($client, array $plugins = [], array $options = [])
62
    {
63 4
        $plugins = array_map(function (Plugin $plugin) {
64 4
            return new ProfilePlugin($plugin, $this->collector, $this->formatter);
65 4
        }, $plugins);
66
67 4
        $clientName = isset($options['client_name']) ? $options['client_name'] : 'Default';
68 4
        array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));
69 4
        unset($options['client_name']);
70
71 4
        if (!$client instanceof ProfileClient) {
72 1
            $client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch);
73 1
        }
74
75 4
        return new PluginClient($client, $plugins, $options);
76
    }
77
}
78