AbstractService   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 162
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 4 1
A getParameter() 0 4 1
A setParameter() 0 6 1
A setUsername() 0 4 1
A getUsername() 0 4 1
A setPassword() 0 4 1
A getPassword() 0 4 1
A initialize() 0 16 3
A getDefaultHttpClient() 0 4 1
A __construct() 0 6 3
A getDefaultParameters() 0 7 1
A getDefaultHttpRequest() 0 4 1
A createRequest() 0 6 1
1
<?php
2
3
namespace IBM\Watson\Common;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Client as HttpClient;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
use Symfony\Component\HttpFoundation\Request as HttpRequest;
9
10
/**
11
 * Base Watson service class
12
 *
13
 * This abstract class should be extended by all Watson services.
14
 *
15
 * @see ServiceInterface
16
 */
17
abstract class AbstractService implements ServiceInterface
18
{
19
    /**
20
     * @var \GuzzleHttp\ClientInterface
21
     */
22
    protected $httpClient;
23
24
    /**
25
     * @var \Symfony\Component\HttpFoundation\Request
26
     */
27
    protected $httpRequest;
28
29
    /**
30
     * @var \Symfony\Component\HttpFoundation\ParameterBag
31
     */
32
    protected $parameters;
33
34
    /**
35
     * AbstractService constructor.
36
     * @param ClientInterface $httpClient
37
     * @param HttpRequest $httpRequest
38
     */
39 6
    public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
40
    {
41 6
        $this->httpClient = $httpClient ?: $this->getDefaultHttpClient();
42 6
        $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest();
43 6
        $this->initialize();
44 6
    }
45
46
    /**
47
     * Initialize service with default parameters
48
     *
49
     * @param array $parameters
50
     * @return $this
51
     */
52 21
    public function initialize(array $parameters = [])
53
    {
54 21
        $this->parameters = new ParameterBag;
55
56 21
        foreach ($this->getDefaultParameters() as $key => $value) {
57 21
            if (is_array($value)) {
58 3
                $this->parameters->set($key, reset($value));
59 2
            } else {
60 21
                $this->parameters->set($key, $value);
61
            }
62 14
        }
63
64 21
        Helper::initialize($this, $parameters);
65
66 21
        return $this;
67
    }
68
69
70
    /**
71
     * @return array
72
     */
73 21
    public function getDefaultParameters()
74
    {
75
        return [
76 21
            'username' => null,
77
            'password' => null
78 14
        ];
79
    }
80
81
    /**
82
     * @return array
83
     */
84 9
    public function getParameters()
85
    {
86 9
        return $this->parameters->all();
87
    }
88
89
    /**
90
     * @param string $key
91
     * @return mixed
92
     */
93 6
    public function getParameter($key)
94
    {
95 6
        return $this->parameters->get($key);
96
    }
97
98
    /**
99
     * @param string $key
100
     * @param mixed $value
101
     * @return $this
102
     */
103 9
    public function setParameter($key, $value)
104
    {
105 9
        $this->parameters->set($key, $value);
106
107 9
        return $this;
108
    }
109
110
    /**
111
     * @param string $value
112
     * @return $this
113
     */
114 6
    public function setUsername($value)
115
    {
116 6
        return $this->setParameter('username', $value);
117
    }
118
119
    /**
120
     * @return string
121
     */
122 3
    public function getUsername()
123
    {
124 3
        return $this->getParameter('username');
125
    }
126
127
    /**
128
     * @param string $value
129
     * @return $this
130
     */
131 6
    public function setPassword($value)
132
    {
133 6
        return $this->setParameter('password', $value);
134
    }
135
136
    /**
137
     * @return string
138
     */
139 3
    public function getPassword()
140
    {
141 3
        return $this->getParameter('password');
142
    }
143
144
    /**
145
     * Get default HTTP client
146
     *
147
     * @return HttpClient
148
     */
149 6
    protected function getDefaultHttpClient()
150
    {
151 6
        return new HttpClient(['curl.options' => [CURLOPT_CONNECTTIMEOUT => 60]]);
152
    }
153
154
    /**
155
     * Get default HTTP request from globals
156
     *
157
     * @return HttpRequest
158
     */
159 6
    protected function getDefaultHttpRequest()
160
    {
161 6
        return HttpRequest::createFromGlobals();
162
    }
163
164
    /**
165
     * Create and initialize a request object
166
     *
167
     * @param string $class The request class name
168
     * @param array $parameters
169
     *
170
     * @return \IBM\Watson\Common\Message\AbstractRequest
171
     */
172 3
    protected function createRequest($class, array $parameters)
173
    {
174 3
        $obj = new $class($this->httpClient, $this->httpRequest);
175
176 3
        return $obj->initialize(array_replace($this->getParameters(), $parameters));
177
    }
178
}
179