Completed
Pull Request — master (#5)
by Ruben
06:28
created

AbstractCoreApiClient::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use GuzzleHttp\ClientInterface;
6
use JMS\Serializer\Serializer;
7
use MovingImage\Client\VMPro\Exception;
8
use MovingImage\Util\Logging\Traits\LoggerAwareTrait;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Log\LoggerAwareInterface;
11
12
/**
13
 * Class AbstractCoreApiClient.
14
 *
15
 * @author Ruben Knol <[email protected]>
16
 */
17
abstract class AbstractCoreApiClient implements LoggerAwareInterface
18
{
19
    use LoggerAwareTrait;
20
21
    /**
22
     * @var ClientInterface The Guzzle HTTP client
23
     */
24
    protected $httpClient;
25
26
    /**
27
     * @var Serializer The JMS Serializer instance
28
     */
29
    protected $serializer;
30
31
    /**
32
     * ApiClient constructor.
33
     *
34
     * @param ClientInterface $httpClient
35
     * @param Serializer      $serializer
36
     */
37 90
    public function __construct(
38
        ClientInterface $httpClient,
39
        Serializer $serializer
40
    ) {
41 90
        $this->httpClient = $httpClient;
42 90
        $this->serializer = $serializer;
43 90
    }
44
45
    /**
46
     * Perform the actual request in the implementation classes.
47
     *
48
     * @param string $method
49
     * @param string $uri
50
     * @param array  $options
51
     *
52
     * @return mixed
53
     */
54
    abstract protected function _doRequest($method, $uri, $options);
55
56
    /**
57
     * Make a request to the API and serialize the result according to our
58
     * serialization strategy.
59
     *
60
     * @param string $method
61
     * @param string $uri
62
     * @param array  $options
63
     * @param object $serialisationClass
0 ignored issues
show
Bug introduced by
There is no parameter named $serialisationClass. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
     *
65
     * @return object|ResponseInterface
66
     */
67 48
    protected function makeRequest($method, $uri, $options)
68
    {
69 48
        $logger = $this->getLogger();
70
71
        try {
72
            // Automagically replace '%videoManagerId%' with the appropriate
73
            // value if it' present in the options
74 48
            if (strpos($uri, '%videoManagerId%') !== false && isset($options['videoManagerId'])) {
75 48
                $uri = str_replace('%videoManagerId%', $options['videoManagerId'], $uri);
76 32
            }
77
78 48
            $logger->info(sprintf('Making API %s request to %s', $method, $uri), [$uri]);
79
80 48
            $response = $this->_doRequest($method, $uri, $options);
81
82 36
            $logger->debug('Response from HTTP call was status code:', [$response->getStatusCode()]);
83 36
            $logger->debug('Response JSON was:', [$response->getBody()]);
84
85 36
            return $response;
86 12
        } catch (\Exception $e) {
87 12
            throw $e; // Just rethrow for now
88
        }
89
    }
90
91
    /**
92
     * Deserialize a response into an instance of it's associated class.
93
     *
94
     * @param string $data
95
     * @param string $serialisationClass
96
     *
97
     * @return object
98
     */
99 12
    protected function deserialize($data, $serialisationClass)
100
    {
101 12
        return $this->serializer->deserialize($data, $serialisationClass, 'json');
102
    }
103
104
    /**
105
     * Helper method to build the JSON data array for making a request
106
     * with ::makeRequest(). Optional parameters with empty or null value will be
107
     * omitted from the return value.
108
     *
109
     * Examples:
110
     *
111
     * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test'])
112
     *
113
     * Would result in:
114
     *
115
     * [
116
     *     'title' => 'test',
117
     *     'bla' => 'test',
118
     * ]
119
     *
120
     * @param array $required
121
     * @param array $optional
122
     *
123
     * @return array
124
     */
125 48
    protected function buildJsonParameters(array $required, array $optional)
126
    {
127 48
        foreach ($required as $key => $value) {
128 48
            if (empty($value)) {
129 24
                throw new Exception(sprintf('Required parameter \'%s\' is missing..', $key));
130
            }
131 24
        }
132
133 36
        $json = $required;
134
135 36
        foreach ($optional as $key => $value) {
136 30
            if (!empty($value) || $value === false) {
137 22
                $json[$key] = $value;
138 12
            }
139 24
        }
140
141 36
        return $json;
142
    }
143
}
144