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

ManagerBase::getResponseJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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
     */
13
    protected $queryParameters = [];
14
15
    /**
16
     * @var \GuzzleHttp\ClientInterface The request client
17
     */
18
    protected $client;
19
20
    /**
21
     * @param \GuzzleHttp\ClientInterface $client The request client
22
     */
23 114
    public function __construct(ClientInterface $client)
24
    {
25 114
        $this->client = $client;
26 114
    }
27
28
    /**
29
     * Get the client.
30
     *
31
     * @return \GuzzleHttp\ClientInterface The request client
32
     */
33 18
    public function getClient() {
34 18
      return $this->client;
35
    }
36
37
    /**
38
     * Make the given Request and return as JSON Decoded PHP object.
39
     *
40
     * @param \Psr\Http\Message\RequestInterface $request
41
     *
42
     * @return mixed the value encoded in <i>json</i> in appropriate
43
     *               PHP type. Values true, false and
44
     *               null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b>
45
     *               and <b>NULL</b> respectively. <b>NULL</b> is returned if the
46
     *               <i>json</i> cannot be decoded or if the encoded
47
     *               data is deeper than the recursion limit
48
     */
49 78
    protected function getResponseJson(RequestInterface $request)
50
    {
51 78
        $response = $this->client->send($request);
52 42
        $body = (string) $response->getBody();
53
54 42
        return json_decode($body, true);
55
    }
56
57
    /**
58
     * Get query string of using the options.
59
     *
60
     * @param $options The options
61
     * @return string  The query string
62
     */
63 18
    protected function getQueryString($options) {
64 18
      $queries = [];
65 18
      foreach ($this->queryParameters as $queryName => $queryDefaultValue) {
66
        // Use user value if possible.
67 18 View Code Duplication
        if (isset($options[$queryName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 9
          $queries[] = $queryName . '=' . $options[$queryName];
69 9
          continue;
70
        }
71
        // Use default value if possible.
72 9 View Code Duplication
        if (!isset($options[$queryName]) && is_string($queryDefaultValue)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
          $queries[] = $queryName . '=' . $queryDefaultValue;
74 3
          continue;
75
        }
76 12
      }
77 18
      $queryString = implode('&', $queries);
78 18
      return empty($queryString) ? '' : '?' . $queryString;
79
    }
80
81
}
82