Completed
Push — master ( 720695...de9ede )
by Fabien
14:35
created

ConfiguredClientsStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 66
ccs 21
cts 21
cp 1
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getCandidates() 0 16 5
A onEvent() 0 4 1
A getSubscribedEvents() 0 7 1
1
<?php
2
3
namespace Http\HttplugBundle\Discovery;
4
5
use Http\Client\HttpClient;
6
use Http\Client\HttpAsyncClient;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\Strategy\DiscoveryStrategy;
9
use Symfony\Component\EventDispatcher\Event;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
14
 * we can use the web debug toolbar for clients found with the discovery.
15
 *
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
19
{
20
    /**
21
     * @var HttpClient
22
     */
23
    private static $client;
24
25
    /**
26
     * @var HttpAsyncClient
27
     */
28
    private static $asyncClient;
29
30
    /**
31
     * @param HttpClient      $httpClient
32
     * @param HttpAsyncClient $asyncClient
33
     */
34 12
    public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
35
    {
36 12
        self::$client = $httpClient;
37 12
        self::$asyncClient = $asyncClient;
38 12
        HttpClientDiscovery::clearCache();
39 12
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 13
    public static function getCandidates($type)
45
    {
46 13
        if ($type === HttpClient::class && self::$client !== null) {
47
            return [['class' => function () {
48 5
                return self::$client;
49 5
            }]];
50
        }
51
52 13
        if ($type === HttpAsyncClient::class && self::$asyncClient !== null) {
53 5
            return [['class' => function () {
54 5
                return self::$asyncClient;
55 5
            }]];
56
        }
57
58 11
        return [];
59
    }
60
61
    /**
62
     * Make sure to use our custom strategy.
63
     *
64
     * @param Event $e
65
     */
66 8
    public function onEvent(Event $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68 8
        HttpClientDiscovery::prependStrategy(self::class);
69 8
    }
70
71
    /**
72
     * Whenever these events occur we make sure to add our strategy to the discovery.
73
     *
74
     * {@inheritdoc}
75
     */
76 2
    public static function getSubscribedEvents()
77
    {
78
        return [
79 2
            'kernel.request' => ['onEvent', 1024],
80 2
            'console.command' => ['onEvent', 1024],
81 2
        ];
82
    }
83
}
84