ConfiguredClientsStrategy::getCandidates()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 3
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Discovery;
6
7
use Http\Client\HttpAsyncClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Discovery\Strategy\DiscoveryStrategy;
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
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 6
    public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null)
35
    {
36 6
        self::$client = $httpClient;
37 6
        self::$asyncClient = $asyncClient;
38 6
        HttpClientDiscovery::clearCache();
39 6
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 16
    public static function getCandidates($type)
45
    {
46 16
        if (HttpClient::class === $type && null !== self::$client) {
47
            return [['class' => function () {
48 4
                return self::$client;
49 4
            }]];
50
        }
51
52 16
        if (HttpAsyncClient::class === $type && null !== self::$asyncClient) {
53
            return [['class' => function () {
54 6
                return self::$asyncClient;
55 6
            }]];
56
        }
57
58 13
        return [];
59
    }
60
}
61