Completed
Push — master ( 3a2c71...4d4890 )
by Tobias
05:46
created

ConfiguredClientsStrategy::getCandidates()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
c 3
b 1
f 0
cc 5
eloc 8
nc 3
nop 1
crap 5
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 17
    public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
35
    {
36 17
        self::$client = $httpClient;
37 17
        self::$asyncClient = $asyncClient;
38 17
        HttpClientDiscovery::clearCache();
39 17
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 18
    public static function getCandidates($type)
45
    {
46 18
        if (HttpClient::class === $type && null !== self::$client) {
47
            return [['class' => function () {
48 10
                return self::$client;
49 10
            }]];
50
        }
51
52 18
        if (HttpAsyncClient::class === $type && null !== self::$asyncClient) {
53 10
            return [['class' => function () {
54 10
                return self::$asyncClient;
55 10
            }]];
56
        }
57
58 14
        return [];
59
    }
60
61
    /**
62
     * Make sure to use our custom strategy.
63
     *
64
     * @param Event $e
65
     */
66 13
    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 13
        HttpClientDiscovery::prependStrategy(self::class);
69 13
    }
70
71
    /**
72
     * Whenever these events occur we make sure to add our strategy to the discovery.
73
     *
74
     * {@inheritdoc}
75
     */
76 6 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78
        return [
79 6
            'kernel.request' => ['onEvent', 1024],
80
            'console.command' => ['onEvent', 1024],
81
        ];
82
    }
83
}
84