Completed
Pull Request — master (#40)
by Tobias
03:36
created

PluginClientBuilder::replacePlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\HttpClient;
6
7
/**
8
 * A builder that help you build a PluginClient.
9
 *
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class PluginClientBuilder implements PluginClientBuilderInterface
13
{
14
    /**
15
     * @var Plugin[]
16
     */
17
    protected $plugins = [];
18
19
    /**
20
     * @var bool
21
     */
22
    protected $rebuildClient = true;
23
24
    /**
25
     * The last created client with the plugins
26
     *
27
     * @var PluginClient
28
     */
29
    private $pluginClient;
30
31
    /**
32
     * The object that sends HTTP messages
33
     *
34
     * @var HttpClient
35
     */
36
    private $httpClient;
37
38
    /**
39
     *
40
     * @param HttpClient $httpClient
41
     */
42
    public function __construct(HttpClient $httpClient = null)
43
    {
44
        $this->httpClient = $httpClient;
45
    }
46
47
    /**
48
     * Add a new plugin to the end of the plugin chain.
49
     *
50
     * @param Plugin $plugin
51
     */
52
    public function addPlugin(Plugin $plugin)
53
    {
54
        $this->plugins[] = $plugin;
55
        $this->rebuildClient = true;
56
    }
57
58
    /**
59
     * Remove a plugin by its fully qualified class name (FQCN).
60
     *
61
     * @param string $fqcn
62
     */
63
    public function removePlugin($fqcn)
64
    {
65
        foreach ($this->plugins as $idx => $plugin) {
66
            if ($plugin instanceof $fqcn) {
67
                unset($this->plugins[$idx]);
68
                $this->rebuildClient = true;
69
            }
70
        }
71
    }
72
73
    /**
74
     * Alias for removePlugin and addPlugin
75
     *
76
     * @param Plugin $plugin
77
     */
78
    public function replacePlugin(Plugin $plugin)
79
    {
80
        $this->removePlugin(get_class($plugin));
81
        $this->addPlugin($plugin);
82
    }
83
84
    /**
85
     * Get all plugins
86
     *
87
     * @return Plugin[]
88
     */
89
    public function getPlugins()
90
    {
91
        return $this->plugins;
92
    }
93
94
    /**
95
     * This will overwrite all existing plugins
96
     *
97
     * @param Plugin[] $plugins
98
     *
99
     * @return PluginClientBuilder
100
     */
101
    public function setPlugins($plugins)
102
    {
103
        $this->plugins = $plugins;
104
        $this->rebuildClient = true;
105
    }
106
107
    /**
108
     * @return PluginClient
109
     */
110
    public function buildClient()
111
    {
112
        if (!$this->httpClient) {
113
            throw new \RuntimeException('No HTTP client were provided to the PluginBuilder.');
114
        }
115
116
        if ($this->rebuildClient) {
117
            $this->rebuildClient = false;
118
            $this->pushBackCachePlugin();
119
120
            $this->pluginClient = new PluginClient($this->httpClient, $this->plugins);
121
        }
122
123
        return $this->pluginClient;
124
    }
125
126
    /**
127
     * @return boolean
128
     */
129
    public function isModified()
130
    {
131
        return $this->rebuildClient;
132
    }
133
134
    /**
135
     * @param HttpClient $httpClient
136
     */
137
    public function setHttpClient(HttpClient $httpClient)
138
    {
139
        $this->rebuildClient = true;
140
        $this->httpClient = $httpClient;
141
    }
142
143
    /**
144
     * @return HttpClient
145
     */
146
    public function getHttpClient()
147
    {
148
        return $this->httpClient;
149
    }
150
151
    /**
152
     * Make sure to move the cache plugin to the end of the chain
153
     */
154
    private function pushBackCachePlugin()
155
    {
156
        $cachePlugin = null;
0 ignored issues
show
Unused Code introduced by
$cachePlugin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
157
        foreach ($this->plugins as $i => $plugin) {
158
            if ($plugin instanceof Plugin\CachePlugin) {
0 ignored issues
show
Bug introduced by
The class Http\Client\Common\Plugin\CachePlugin does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
159
                $cachePlugin = $plugin;
160
                unset($this->plugins[$i]);
161
162
                $this->plugins[] = $cachePlugin;
163
164
                return;
165
            }
166
        }
167
    }
168
}
169