Completed
Push — feature/httplug ( f5571c )
by Maxime
10:26
created

ClientFactory::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Knp\FriendlyContexts\Http;
4
5
6
use Http\Client\Common\Plugin;
7
use Http\Client\Common\PluginClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\HttpClientDiscovery;
10
11
final class ClientFactory
12
{
13
    /**
14
     * @var HttpClient
15
     */
16
    private $client;
17
18
    /**
19
     * @var HttpClient
20
     */
21
    private $originalClient;
22
23
    /**
24
     * @var Plugin[]
25
     */
26
    private $plugins;
27
28
    /**
29
     * @var bool
30
     */
31
    private $clientChanged;
32
33
    public function __construct(HttpClient $client = null)
34
    {
35
        $this->originalClient = $client ?: HttpClientDiscovery::find();
36
        $this->client = $this->originalClient;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
        $this->resetPlugins();
38
    }
39
40
    /**
41
     * Drop all registered plugins
42
     */
43
    public function resetPlugins()
44
    {
45
        $this->plugins = [new Plugin\ErrorPlugin()];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
        $this->clientChanged = true;
47
    }
48
49
    /**
50
     * Adds a new httplug plugin
51
     *
52
     * @param Plugin $plugin
53
     * @return ClientFactory
54
     */
55
    public function addPlugin(Plugin $plugin)
56
    {
57
        $this->plugins[] = $plugin;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
        $this->clientChanged = true;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @return HttpClient
65
     */
66
    public function getClient()
67
    {
68
        if (!$this->clientChanged) {
69
            return $this->client;
70
        }
71
72
        return new PluginClient($this->originalClient, $this->plugins);
73
    }
74
}
75