Passed
Branch development (d7e242)
by Jonathan
06:53
created

SetGetTrait::setTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use GuzzleHttp\Client;
17
use GuzzleHttp\RequestOptions;
18
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19
20
trait SetGetTrait
21
{
22
    /**
23
     * Backend API key
24
     *
25
     * @var string
26
     */
27
    protected $apiKey;
28
29
    /**
30
     * Backend username
31
     *
32
     * @var string
33
     */
34
    protected $username;
35
36
    /**
37
     * Backend password
38
     *
39
     * @var string
40
     */
41
    protected $password;
42
43
    /**
44
     * When true, backend prints "TEST MODE" water mark into output document, and API call does not count against quota
45
     *
46
     * @var bool
47
     */
48
    protected $test;
49
50
    /**
51
     * Backend base URI
52
     *
53
     * @var string
54
     */
55
    protected $baseUri;
56
57
    /**
58
     * Backend version string
59
     *
60
     * @var string
61
     */
62
    protected $version;
63
64
    /**
65
     * Backend timeout in seconds
66
     *
67
     * @var int
68
     */
69
    protected $timeout;
70
71
    /**
72
     * REST client to backend
73
     *
74
     * @var Client
75
     */
76
    protected $client;
77
78
    /**
79
     * Debug flag of REST client
80
     *
81
     * @var bool
82
     */
83
    protected $debug;
84
85
    /**
86
     * Return the REST client of the backend web service
87
     *
88
     * @return \GuzzleHttp\Client
89
     */
90 24
    public function getClient()
91
    {
92 24
        if (null === $this->client) {
93
94 24
            $authorization = call_user_func(function () {
95
96 24
                if (null !== $this->getApiKey()) {
97 1
                    return sprintf('ReportingCloud-APIKey %s', $this->getApiKey());
98
                }
99
100 24
                if (null !== $this->getUsername() && null !== $this->getPassword()) {
101 23
                    $value = sprintf('%s:%s', $this->getUsername(), $this->getPassword());
102 23
                    return sprintf('Basic %s', base64_encode($value));
103
                }
104
105 1
                $message = 'Either the API key, or username and password must be set for authorization';
106 1
                throw new InvalidArgumentException($message);
107 24
            });
108
109
            $options = [
110 23
                'base_uri'              => $this->getBaseUri(),
111 23
                RequestOptions::TIMEOUT => $this->getTimeout(),
112 23
                RequestOptions::DEBUG   => $this->getDebug(),
113 23
                RequestOptions::HEADERS => [
114 23
                    'Authorization' => $authorization,
115 23
                ],
116 23
            ];
117
118 23
            $client = new Client($options);
119
120 23
            $this->setClient($client);
121 23
        }
122
123 23
        return $this->client;
124
    }
125
126
    /**
127
     * Set the REST client of the backend web service
128
     *
129
     * @param Client $client REST client
130
     *
131
     * @return ReportingCloud
132
     */
133 23
    public function setClient(Client $client)
134
    {
135 23
        $this->client = $client;
136
137 23
        return $this;
138
    }
139
140
141
    /**
142
     * Return the API key
143
     *
144
     * @return string
145
     */
146 24
    public function getApiKey()
147
    {
148 24
        return $this->apiKey;
149
    }
150
151
    /**
152
     * Set the API key
153
     *
154
     * @param string $apiKey API key
155
     *
156
     * @return ReportingCloud
157
     */
158 2
    public function setApiKey($apiKey)
159
    {
160 2
        $this->apiKey = $apiKey;
161
162 2
        return $this;
163
    }
164
165
    /**
166
     * Return the username
167
     *
168
     * @return string
169
     */
170 27
    public function getUsername()
171
    {
172 27
        return $this->username;
173
    }
174
175
    /**
176
     * Set the username
177
     *
178
     * @param string $username Username
179
     *
180
     * @return ReportingCloud
181
     */
182 66
    public function setUsername($username)
183
    {
184 66
        $this->username = $username;
185
186 66
        return $this;
187
    }
188
189
    /**
190
     * Return the password
191
     *
192
     * @return string
193
     */
194 26
    public function getPassword()
195
    {
196 26
        return $this->password;
197
    }
198
199
    /**
200
     * Set the password
201
     *
202
     * @param string $password Password
203
     *
204
     * @return ReportingCloud
205
     */
206 66
    public function setPassword($password)
207
    {
208 66
        $this->password = $password;
209
210 66
        return $this;
211
    }
212
213
    /**
214
     * Return the base URI of the backend web service
215
     *
216
     * @return string
217
     */
218 27
    public function getBaseUri()
219
    {
220 27
        if (null === $this->baseUri) {
221 25
            $this->setBaseUri(self::DEFAULT_BASE_URI);
222 25
        }
223
224 27
        return $this->baseUri;
225
    }
226
227
    /**
228
     * Set the base URI of the backend web service
229
     *
230
     * @param string $baseUri Base URI
231
     *
232
     * @return ReportingCloud
233
     */
234 27
    public function setBaseUri($baseUri)
235
    {
236 27
        $this->baseUri = $baseUri;
237
238 27
        return $this;
239
    }
240
241
    /**
242
     * Get the timeout (in seconds) of the backend web service
243
     *
244
     * @return int
245
     */
246 26
    public function getTimeout()
247
    {
248 26
        if (null === $this->timeout) {
249 24
            $this->setTimeout(self::DEFAULT_TIMEOUT);
250 24
        }
251
252 26
        return $this->timeout;
253
    }
254
255
    /**
256
     * Set the timeout (in seconds) of the backend web service
257
     *
258
     * @param int $timeout Timeout
259
     *
260
     * @return ReportingCloud
261
     */
262 26
    public function setTimeout($timeout)
263
    {
264 26
        $this->timeout = (int) $timeout;
265
266 26
        return $this;
267
    }
268
269
    /**
270
     * Return the debug flag
271
     *
272
     * @return mixed
273
     */
274 26
    public function getDebug()
275
    {
276 26
        if (null === $this->debug) {
277 24
            $this->setDebug(self::DEFAULT_DEBUG);
278 24
        }
279
280 26
        return $this->debug;
281
    }
282
283
    /**
284
     * Set the debug flag
285
     *
286
     * @param bool $debug Debug flag
287
     *
288
     * @return ReportingCloud
289
     */
290 26
    public function setDebug($debug)
291
    {
292 26
        $this->debug = (bool) $debug;
293
294 26
        return $this;
295
    }
296
297
    /**
298
     * Return the test flag
299
     *
300
     * @return mixed
301
     */
302 24
    public function getTest()
303
    {
304 24
        if (null === $this->test) {
305 22
            $this->setTest(self::DEFAULT_TEST);
306 22
        }
307
308 24
        return $this->test;
309
    }
310
311
    /**
312
     * Set the test flag
313
     *
314
     * @param bool $test Test flag
315
     *
316
     * @return ReportingCloud
317
     */
318 24
    public function setTest($test)
319
    {
320 24
        $this->test = (bool) $test;
321
322 24
        return $this;
323
    }
324
325
    /**
326
     * Get the version string of the backend web service
327
     *
328
     * @return string
329
     */
330 26
    public function getVersion()
331
    {
332 26
        if (null === $this->version) {
333 24
            $this->version = self::DEFAULT_VERSION;
334 24
        }
335
336 26
        return $this->version;
337
    }
338
339
    /**
340
     * Set the version string of the backend web service
341
     *
342
     * @param string $version Version string
343
     *
344
     * @return ReportingCloud
345
     */
346 2
    public function setVersion($version)
347
    {
348 2
        $this->version = $version;
349
350 2
        return $this;
351
    }
352
}
353