Completed
Pull Request — master (#19)
by Yuan
02:02
created

ManagerBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 75
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClient() 0 3 1
A getResponseJson() 0 7 1
B getQueryString() 0 17 5
1
<?php
2
3
namespace Acquia\LiftClient\Manager;
4
5
use GuzzleHttp\ClientInterface;
6
use Psr\Http\Message\RequestInterface;
7
8
abstract class ManagerBase
9
{
10
    /**
11
     * @var array A list of query parameters that the URL could possibly have
12
     *            Format: query_parameter_name => query_parameter_default_value
13
     */
14
    protected $queryParameters = [];
15
16
    /**
17
     * @var \GuzzleHttp\ClientInterface The request client
18
     */
19
    protected $client;
20
21
    /**
22
     * @param \GuzzleHttp\ClientInterface $client The request client
23
     */
24 114
    public function __construct(ClientInterface $client)
25
    {
26 114
        $this->client = $client;
27 114
    }
28
29
    /**
30
     * Get the client.
31
     *
32
     * @return \GuzzleHttp\ClientInterface The request client
33
     */
34 18
    public function getClient() {
35 18
      return $this->client;
36
    }
37
38
    /**
39
     * Make the given Request and return as JSON Decoded PHP object.
40
     *
41
     * @param \Psr\Http\Message\RequestInterface $request
42
     *
43
     * @return mixed the value encoded in <i>json</i> in appropriate
44
     *               PHP type. Values true, false and
45
     *               null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b>
46
     *               and <b>NULL</b> respectively. <b>NULL</b> is returned if the
47
     *               <i>json</i> cannot be decoded or if the encoded
48
     *               data is deeper than the recursion limit
49
     */
50 78
    protected function getResponseJson(RequestInterface $request)
51
    {
52 78
        $response = $this->client->send($request);
53 42
        $body = (string) $response->getBody();
54
55 42
        return json_decode($body, true);
56
    }
57
58
    /**
59
     * Get query string of using the options.
60
     *
61
     * @param $options The options
62
     * @return string  The query string
63
     */
64 18
    protected function getQueryString($options) {
65 18
      $queries = [];
66 18
      foreach ($this->queryParameters as $queryName => $queryDefaultValue) {
67
        // Use user value if possible.
68 18
        if (isset($options[$queryName])) {
69 9
          $queries[] = $queryName . '=' . rawurlencode($options[$queryName]);
70 9
          continue;
71
        }
72
        // Use default value if possible.
73 9
        if (is_string($queryDefaultValue)) {
74
          $queries[] = $queryName . '=' . rawurlencode($queryDefaultValue);
75 3
          continue;
76
        }
77 12
      }
78 18
      $queryString = implode('&', $queries);
79 18
      return empty($queryString) ? '' : '?' . $queryString;
80
    }
81
82
}
83