|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bangpound\oEmbed; |
|
4
|
|
|
|
|
5
|
|
|
use Bangpound\oEmbed\Exception\UnknownFormatException; |
|
6
|
|
|
use Bangpound\oEmbed\Provider\ProviderInterface; |
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use Negotiation\FormatNegotiatorInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Consumer. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Consumer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ClientInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ProviderInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $provider; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var SerializerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $serializer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var FormatNegotiatorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $negotiator; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ClientInterface $client |
|
39
|
|
|
* @param ProviderInterface $provider |
|
40
|
|
|
* @param SerializerInterface $serializer |
|
41
|
|
|
* @param FormatNegotiatorInterface $negotiator |
|
42
|
|
|
*/ |
|
43
|
12 |
|
public function __construct(ClientInterface $client, ProviderInterface $provider, SerializerInterface $serializer, FormatNegotiatorInterface $negotiator) |
|
44
|
|
|
{ |
|
45
|
12 |
|
$this->client = $client; |
|
46
|
12 |
|
$this->provider = $provider; |
|
47
|
12 |
|
$this->serializer = $serializer; |
|
48
|
12 |
|
$this->negotiator = $negotiator; |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $url |
|
53
|
|
|
* @param array $params |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
12 |
|
public function get($url, $params = array()) |
|
58
|
|
|
{ |
|
59
|
12 |
|
$request = $this->provider->request($url, $params); |
|
60
|
12 |
|
$response = $this->client->send($request); |
|
61
|
|
|
|
|
62
|
12 |
|
$data = $response->getBody()->getContents(); |
|
63
|
12 |
|
$format = $this->getFormat($params, $response); |
|
64
|
|
|
|
|
65
|
8 |
|
return $this->serializer->deserialize($data, null, $format); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $params |
|
70
|
|
|
* @param ResponseInterface $response |
|
71
|
|
|
* |
|
72
|
|
|
* @return null|string |
|
73
|
|
|
* |
|
74
|
|
|
* @throws UnknownFormatException |
|
75
|
|
|
*/ |
|
76
|
12 |
|
private function getFormat(array $params, ResponseInterface $response) |
|
77
|
|
|
{ |
|
78
|
12 |
|
if ($response->hasHeader('content-type')) { |
|
79
|
6 |
|
$header = $response->getHeaderLine('content-type'); |
|
80
|
6 |
|
$format = $this->negotiator->getFormat($header); |
|
81
|
6 |
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
if (isset($params['format'])) { |
|
84
|
4 |
|
$format = $params['format']; |
|
85
|
4 |
|
} |
|
86
|
|
|
|
|
87
|
12 |
|
if (!isset($format)) { |
|
88
|
4 |
|
throw new UnknownFormatException('Unable to figure out the format'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
8 |
|
return $format; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|