Completed
Push — master ( e81e8a...e81e8a )
by Claude
01:07
created

Builder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 63.04%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 10
dl 0
loc 140
ccs 29
cts 46
cp 0.6304
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A setHttpClient() 0 4 1
A setRequestFactory() 0 4 1
A setParameters() 0 4 1
A resolveParameters() 0 8 1
A getRequestFactory() 0 4 1
A addPlugin() 0 5 1
A getHttpClient() 0 22 2
A removePlugin() 0 6 2
1
<?php
2
3
/*
4
 * A php library for using the Emarsys API.
5
 *
6
 * @link      https://github.com/quitoque/emarsys-php-client
7
 * @package   emarsys-php-client
8
 * @license   MIT
9
 * @copyright Copyright (c) 2017 Quitoque <[email protected]>
10
 */
11
12
namespace Emarsys\HttpClient;
13
14
use Emarsys\HttpClient\Message\Authentication\Wsse;
15
use Http\Client\Common\Plugin;
16
use Http\Client\Common\PluginClient;
17
use Http\Client\HttpAsyncClient;
18
use Http\Discovery\HttpAsyncClientDiscovery;
19
use Http\Discovery\MessageFactoryDiscovery;
20
use Http\Discovery\UriFactoryDiscovery;
21
use Http\Message\MessageFactory;
22
use Http\Message\RequestFactory;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
/**
26
 * Class Builder.
27
 *
28
 * @author Claude Khedhiri <[email protected]>
29
 */
30
class Builder
31
{
32
    /**
33
     * @var HttpAsyncClient
34
     */
35
    private $httpClient;
36
37
    /**
38
     * @var array
39
     */
40
    private $parameters;
41
42
    /**
43
     * @var MessageFactory
44
     */
45
    private $requestFactory;
46
47
    /**
48
     * A HTTP client with all our plugins.
49
     *
50
     * @var PluginClient
51
     */
52
    private $pluginClient;
53
54
    /**
55
     * True if we should create a new Plugin client at next request.
56
     *
57
     * @var bool
58
     */
59
    private $httpClientModified = true;
60
61
    /**
62
     * @var Plugin[]
63
     */
64
    private $plugins = array();
65
66
    /**
67
     * @param array                $parameters
68
     * @param HttpAsyncClient|null $httpAsyncClient
69
     * @param RequestFactory|null  $requestFactory
70
     */
71 4
    public function __construct(
72
        array $parameters = array(),
0 ignored issues
show
Unused Code introduced by
The parameter $parameters 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...
73
        HttpAsyncClient $httpAsyncClient = null,
74
        RequestFactory $requestFactory = null
75
    ) {
76 4
        $this->httpClient = $httpAsyncClient ?: HttpAsyncClientDiscovery::find();
77 4
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestFactory ?: \Http...actoryDiscovery::find() can also be of type object<Http\Message\RequestFactory>. However, the property $requestFactory is declared as type object<Http\Message\MessageFactory>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
78 4
    }
79
80
    /**
81
     * @param HttpAsyncClient $httpAsyncClient
82
     */
83
    public function setHttpClient(HttpAsyncClient $httpAsyncClient)
84
    {
85
        $this->httpClient = $httpAsyncClient;
86
    }
87
88
    /**
89
     * @param MessageFactory $requestFactory
90
     */
91
    public function setRequestFactory($requestFactory)
92
    {
93
        $this->requestFactory = $requestFactory;
94
    }
95
96
    /**
97
     * @param array $parameters
98
     */
99 2
    public function setParameters(array $parameters)
100
    {
101 2
        $this->parameters = $parameters;
102 2
    }
103
104
    public function resolveParameters()
105
    {
106
        $resolver = new OptionsResolver();
107
        $resolver->setRequired('username');
108
        $resolver->setRequired('secret');
109
110
        $this->parameters = $resolver->resolve($this->parameters);
111
    }
112
113
    /**
114
     * @return PluginClient
115
     */
116 3
    public function getHttpClient()
117
    {
118 3
        if ($this->httpClientModified) {
119 3
            $this->addPlugin(new Plugin\AuthenticationPlugin(new Wsse(
120 3
                    $this->parameters['username'],
121 3
                    $this->parameters['secret'])
122 3
            ));
123
124 3
            $this->addPlugin(new Plugin\BaseUriPlugin(
125 3
                    UriFactoryDiscovery::find()->createUri('https://api.emarsys.net/api/v2'))
126 3
            );
127
128 3
            $this->addPlugin(new Plugin\HeaderDefaultsPlugin(array(
129 3
                'User-Agent' => 'emarsys-php-client (https://github.com/ck-developer/emarsys-php-client)',
130 3
            )));
131
132 3
            $this->httpClientModified = false;
133 3
            $this->pluginClient = new PluginClient($this->httpClient, $this->plugins);
134 3
        }
135
136 3
        return $this->pluginClient;
137
    }
138
139
    /**
140
     * @return MessageFactory
141
     */
142 2
    public function getRequestFactory()
143
    {
144 2
        return $this->requestFactory;
145
    }
146
147
    /**
148
     * Add a new plugin to the end of the plugin chain.
149
     *
150
     * @param Plugin $plugin
151
     */
152 3
    public function addPlugin(Plugin $plugin)
153
    {
154 3
        $this->plugins[get_class($plugin)] = $plugin;
155 3
        $this->httpClientModified = true;
156 3
    }
157
158
    /**
159
     * Remove a plugin by its fully qualified class name.
160
     *
161
     * @param string $class
162
     */
163
    public function removePlugin($class)
164
    {
165
        if (array_key_exists($class, $this->plugins)) {
166
            unset($this->plugins[$class]);
167
        }
168
    }
169
}
170