Completed
Pull Request — master (#5)
by Ruben
03:48
created

AbstractCoreApiClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 132
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
_doRequest() 0 1 ?
A makeRequest() 0 23 3
A deserialize() 0 4 1
B buildJsonParameters() 0 18 6
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
     * @const string
23
     */
24
    const OPT_VIDEO_MANAGER_ID = 'videoManagerId';
25
26
    /**
27
     * @var ClientInterface The Guzzle HTTP client
28
     */
29
    protected $httpClient;
30
31
    /**
32
     * @var Serializer The JMS Serializer instance
33
     */
34
    protected $serializer;
35
36
    /**
37
     * ApiClient constructor.
38
     *
39
     * @param ClientInterface $httpClient
40
     * @param Serializer      $serializer
41
     */
42 90
    public function __construct(
43
        ClientInterface $httpClient,
44 5
        Serializer $serializer
45
    ) {
46 90
        $this->httpClient = $httpClient;
47 90
        $this->serializer = $serializer;
48 90
    }
49
50
    /**
51
     * Perform the actual request in the implementation classes.
52
     *
53
     * @param string $method
54
     * @param string $uri
55
     * @param array  $options
56
     *
57
     * @return mixed
58
     */
59
    abstract protected function _doRequest($method, $uri, $options);
60
61
    /**
62
     * Make a request to the API and serialize the result according to our
63
     * serialization strategy.
64
     *
65
     * @param string $method
66
     * @param string $uri
67
     * @param array  $options
68
     * @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...
69
     *
70
     * @return object|ResponseInterface
71
     */
72 48
    protected function makeRequest($method, $uri, $options)
73
    {
74 48
        $logger = $this->getLogger();
75
76
        try {
77
            // Automagically pre-pend videoManagerId if the option is present in the
78
            // options for sending the request
79 48
            if (isset($options[self::OPT_VIDEO_MANAGER_ID])) {
80 48
                $uri = sprintf('%d/%s', $options[self::OPT_VIDEO_MANAGER_ID], $uri);
81 40
            }
82
83 48
            $logger->info(sprintf('Making API %s request to %s', $method, $uri), [$uri]);
84
85 48
            $response = $this->_doRequest($method, $uri, $options);
86
87 36
            $logger->debug('Response from HTTP call was status code:', [$response->getStatusCode()]);
88 36
            $logger->debug('Response JSON was:', [$response->getBody()]);
89
90 36
            return $response;
91 12
        } catch (\Exception $e) {
92 12
            throw $e; // Just rethrow for now
93
        }
94
    }
95
96
    /**
97
     * Deserialize a response into an instance of it's associated class.
98
     *
99
     * @param string $data
100
     * @param string $serialisationClass
101
     *
102
     * @return object
103
     */
104 12
    protected function deserialize($data, $serialisationClass)
105
    {
106 12
        return $this->serializer->deserialize($data, $serialisationClass, 'json');
107
    }
108
109
    /**
110
     * Helper method to build the JSON data array for making a request
111
     * with ::makeRequest(). Optional parameters with empty or null value will be
112
     * omitted from the return value.
113
     *
114
     * Examples:
115
     *
116
     * $this->buildJsonParameters(['title' => 'test'], ['description' => '', 'bla' => 'test'])
117
     *
118
     * Would result in:
119
     *
120
     * [
121
     *     'title' => 'test',
122
     *     'bla' => 'test',
123
     * ]
124
     *
125
     * @param array $required
126
     * @param array $optional
127
     *
128
     * @return array
129
     */
130 48
    protected function buildJsonParameters(array $required, array $optional)
131
    {
132 48
        foreach ($required as $key => $value) {
133 48
            if (empty($value)) {
134 18
                throw new Exception(sprintf('Required parameter \'%s\' is missing..', $key));
135
            }
136 30
        }
137
138 36
        $json = $required;
139
140 36
        foreach ($optional as $key => $value) {
141 30
            if (!empty($value) || $value === false) {
142 20
                $json[$key] = $value;
143 15
            }
144 30
        }
145
146 36
        return $json;
147
    }
148
}
149