|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MovingImage\Client\VMPro\ApiClient; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use JMS\Serializer\Serializer; |
|
7
|
|
|
use MovingImage\Client\VMPro\Entity\Channel; |
|
8
|
|
|
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; |
|
9
|
|
|
use MovingImage\Util\Logging\Traits\LoggerAwareTrait; |
|
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractApiClient. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Ruben Knol <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractApiClient implements |
|
18
|
|
|
ApiClientInterface, |
|
19
|
|
|
LoggerAwareInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use LoggerAwareTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ClientInterface The Guzzle HTTP client |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $httpClient; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Serializer The JMS Serializer instance |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $serializer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ApiClient constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ClientInterface $httpClient |
|
37
|
|
|
* @param Serializer $serializer |
|
38
|
|
|
*/ |
|
39
|
24 |
|
public function __construct( |
|
40
|
|
|
ClientInterface $httpClient, |
|
41
|
|
|
Serializer $serializer |
|
42
|
4 |
|
) { |
|
43
|
24 |
|
$this->httpClient = $httpClient; |
|
44
|
24 |
|
$this->serializer = $serializer; |
|
45
|
24 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Perform the actual request in the implementation classes. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $method |
|
51
|
|
|
* @param string $uri |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
* |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
|
|
abstract protected function _doRequest($method, $uri, $options); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Make a request to the API and serialize the result according to our |
|
60
|
|
|
* serialization strategy. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $method |
|
63
|
|
|
* @param string $uri |
|
64
|
|
|
* @param array $options |
|
65
|
|
|
* @param object $serialisationClass |
|
66
|
|
|
* |
|
67
|
|
|
* @return array|\JMS\Serializer\scalar|mixed|object |
|
68
|
|
|
*/ |
|
69
|
6 |
|
protected function makeRequest($method, $uri, $options, $serialisationClass = null) |
|
70
|
|
|
{ |
|
71
|
6 |
|
$logger = $this->getLogger(); |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
|
|
// Automagically replace '%videoManagerId%' with the appropriate |
|
75
|
|
|
// value if it' present in the options |
|
76
|
6 |
|
if (strpos($uri, '%videoManagerId%') !== false && isset($options['videoManagerId'])) { |
|
77
|
6 |
|
$uri = str_replace('%videoManagerId%', $options['videoManagerId'], $uri); |
|
78
|
4 |
|
} |
|
79
|
|
|
|
|
80
|
6 |
|
$logger->info(sprintf('Making API %s request to %s', $method, $uri), [$uri]); |
|
81
|
|
|
|
|
82
|
6 |
|
$response = $this->_doRequest($method, $uri, $options); |
|
83
|
|
|
|
|
84
|
6 |
|
$logger->debug('Response from HTTP call was status code:', [$response->getStatusCode()]); |
|
85
|
6 |
|
$logger->debug('Response JSON was:', [$response->getBody()]); |
|
86
|
|
|
|
|
87
|
6 |
|
if (!is_null($serialisationClass)) { |
|
88
|
6 |
|
return $this->serializer->deserialize($response->getBody(), $serialisationClass, 'json'); |
|
|
|
|
|
|
89
|
|
|
} else { |
|
90
|
|
|
return \json_decode($response->getBody()); |
|
91
|
|
|
} |
|
92
|
|
|
} catch (\Exception $e) { |
|
93
|
|
|
throw $e; // Just rethrow for now |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function getChannels($videoManagerId) |
|
101
|
|
|
{ |
|
102
|
6 |
|
return $this->makeRequest('GET', '%videoManagerId%/channels', [ |
|
103
|
6 |
|
'videoManagerId' => $videoManagerId, |
|
104
|
6 |
|
], Channel::class); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: