HttpClient.php
5 years ago
HttpClientCache.php
5 years ago
HttpClientInterface.php
5 years ago
OembedClient.php
5 years ago
OembedClient.php
153 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OembedClient.php |
| 4 | * |
| 5 | * @package Embera |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 7 | * @link http://www.michael-pratt.com/ |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | |
| 13 | namespace Embera\Http; |
| 14 | |
| 15 | use Embera\Embera; |
| 16 | use Embera\Http; |
| 17 | use Embera\Provider\ProviderInterface; |
| 18 | |
| 19 | /** |
| 20 | * This class manages requests and responses |
| 21 | * from an Oembed Endpoint. |
| 22 | * |
| 23 | * TODO: Support xml responses |
| 24 | */ |
| 25 | class OembedClient |
| 26 | { |
| 27 | /** @var array Configuration settings */ |
| 28 | protected $config = []; |
| 29 | |
| 30 | /** @var Http\HttpClient */ |
| 31 | protected $http; |
| 32 | |
| 33 | /** |
| 34 | * Construct |
| 35 | * |
| 36 | * @param array $config |
| 37 | * @param Http\HttpClient $http |
| 38 | * @return void |
| 39 | */ |
| 40 | public function __construct($config, HttpClientInterface $http) |
| 41 | { |
| 42 | $this->config = $config; |
| 43 | $this->http = $http; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Gets the response from a given providerObject |
| 48 | * |
| 49 | * @param ProviderInterface $provider |
| 50 | * @return array |
| 51 | */ |
| 52 | public function getResponseFrom(ProviderInterface $provider) |
| 53 | { |
| 54 | if ($this->config['fake_responses'] == Embera::ONLY_FAKE_RESPONSES) { |
| 55 | $response = $this->processFakeResponse($provider->getProviderName(), $provider->getFakeResponse()); |
| 56 | } else { |
| 57 | try { |
| 58 | $response = $this->lookup($provider); |
| 59 | }catch (\Exception $e) { |
| 60 | $response['error'] = $e->getMessage(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return $provider->modifyResponse($response); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Executes a http request to a url created by the $provider |
| 69 | * |
| 70 | * @param ProviderInterface $provider |
| 71 | * @return array |
| 72 | * |
| 73 | * @throws \Exception From the Http object only if there is no way |
| 74 | * to perform the request or if the response from |
| 75 | * the server is empty/invalid. |
| 76 | */ |
| 77 | protected function lookup(ProviderInterface $provider) |
| 78 | { |
| 79 | $url = $this->constructUrl($provider->getEndpoint(), $provider->getParams()); |
| 80 | $response = $this->http->fetch($url); |
| 81 | $json = json_decode($response, true); |
| 82 | |
| 83 | if ($json) { |
| 84 | return array_merge($json, [ |
| 85 | 'embera_using_fake_response' => 0, |
| 86 | 'embera_provider_name' => $provider->getProviderName(), |
| 87 | ]); |
| 88 | } |
| 89 | |
| 90 | if ($this->config['fake_responses'] == Embera::ALLOW_FAKE_RESPONSES) { |
| 91 | return $this->processFakeResponse($provider->getProviderName(), $provider->getFakeResponse()); |
| 92 | } |
| 93 | |
| 94 | return []; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Builds a valid Oembed query string based on the given parameters, |
| 99 | * Since this method uses the http_build_query function, there is no |
| 100 | * need to pass urlencoded parameters, http_build_query already does |
| 101 | * this for us. |
| 102 | * |
| 103 | * @param string $endpoint The Url to the Oembed endpoint |
| 104 | * @param array $params Parameters for the query string |
| 105 | * @return string |
| 106 | */ |
| 107 | protected function constructUrl($endpoint, array $params = array()) |
| 108 | { |
| 109 | return $endpoint . ((strpos($endpoint, '?') === false) ? '?' : '&') . http_build_query(array_filter($params)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Builds the fake response. |
| 114 | * This replaces placeholders that are present in $config['fake'] |
| 115 | * into the response array |
| 116 | * |
| 117 | * @param string $providerName |
| 118 | * @param array $response |
| 119 | * @return array |
| 120 | */ |
| 121 | protected function processFakeResponse($providerName, array $response) |
| 122 | { |
| 123 | $defaultValues = [ |
| 124 | 'version' => '1.0', |
| 125 | 'provider_name' => '', |
| 126 | 'url' => '', |
| 127 | 'title' => '', |
| 128 | 'author_name' => '', |
| 129 | 'author_url' => '', |
| 130 | 'cache_age' => 0, |
| 131 | 'embera_using_fake_response' => 1, |
| 132 | 'embera_provider_name' => $providerName, |
| 133 | ]; |
| 134 | |
| 135 | if (!empty($response['html'])) { |
| 136 | foreach ($this->config as $key => $v) { |
| 137 | if (!is_string($v) && !is_numeric($v)) { |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if (in_array($key, ['maxwidth', 'maxheight'])) { |
| 142 | $key = str_replace('max', '', $key); |
| 143 | } |
| 144 | |
| 145 | $response['html'] = str_replace('{' . $key. '}', $v, $response['html']); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return array_merge($defaultValues, $response); |
| 150 | } |
| 151 | |
| 152 | } |
| 153 |