Service.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Service.php |
| 4 | * |
| 5 | * @package Adapters |
| 6 | * @author Michael Pratt <pratt@hablarmierda.net> |
| 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\Adapters; |
| 14 | |
| 15 | /** |
| 16 | * This is an abstract class and all Service/Providers should extend it. |
| 17 | */ |
| 18 | abstract class Service |
| 19 | { |
| 20 | /** |
| 21 | * @var mixed Instance of \Embera\Url |
| 22 | * (Marking this as mixed instead of object because scrutinizer complains about type check on preg_match) |
| 23 | */ |
| 24 | protected $url; |
| 25 | |
| 26 | /** @var object Instance of \Embera\Oembed */ |
| 27 | protected $oembed = null; |
| 28 | |
| 29 | /** @var array Associative array with config/parameters to be sent to the oembed provider */ |
| 30 | protected $config = array(); |
| 31 | |
| 32 | /** @var array Array with all the errors */ |
| 33 | protected $errors = array(); |
| 34 | |
| 35 | /** @var string The api url for the current service */ |
| 36 | protected $apiUrl = null; |
| 37 | |
| 38 | /** |
| 39 | * Validates that the url belongs to this service. |
| 40 | * Should be implemented on all children and should |
| 41 | * return a boolean (preg_match returns 0 or 1 that |
| 42 | * is why I'm also allowing 'int' as a return type). |
| 43 | * |
| 44 | * The current url is made available via $this->url |
| 45 | * |
| 46 | * @return bool|int |
| 47 | */ |
| 48 | abstract protected function validateUrl(); |
| 49 | |
| 50 | /** |
| 51 | * Construct |
| 52 | * |
| 53 | * @param string $url |
| 54 | * @param array $config |
| 55 | * @param object $oembed |
| 56 | * @return void |
| 57 | * |
| 58 | * @throws InvalidArgumentException when the given url doesnt match the current service |
| 59 | */ |
| 60 | public function __construct($url, array $config = array(), \Embera\Oembed $oembed) |
| 61 | { |
| 62 | $this->url = new \Embera\Url($url); |
| 63 | $this->normalizeUrl(); |
| 64 | |
| 65 | if (!$this->validateUrl()) { |
| 66 | throw new \InvalidArgumentException('Url ' . $this->url . ' seems to be invalid for the ' . get_class($this) . ' service'); |
| 67 | } |
| 68 | |
| 69 | $this->config = array_replace_recursive(array( |
| 70 | 'params' => array( |
| 71 | 'maxwidth' => 0, |
| 72 | 'maxheight' => 0, |
| 73 | ) |
| 74 | ), $config); |
| 75 | |
| 76 | $this->oembed = $oembed; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets the information from an Oembed provider |
| 81 | * when this fails, it tries to provide a fakeResponse. |
| 82 | * Returns an associative array with a (common) Oembed response. |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | public function getInfo() |
| 87 | { |
| 88 | try { |
| 89 | |
| 90 | if ($res = $this->oembed->getResourceInfo($this->config['oembed'], $this->apiUrl, (string) $this->url, $this->config['params'])) { |
| 91 | return $this->modifyResponse($res); |
| 92 | } |
| 93 | |
| 94 | } catch (\Exception $e) { |
| 95 | $this->errors[] = $e->getMessage(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Use fakeResponses when the oembed setting is null or false |
| 100 | * If the oembed config is true, the user strictly wants real responses |
| 101 | */ |
| 102 | if (!$this->config['oembed'] && $response = $this->fakeResponse()) { |
| 103 | $fakeResponse = new \Embera\FakeResponse($this->config, $response); |
| 104 | return $this->modifyResponse($fakeResponse->buildResponse()); |
| 105 | } |
| 106 | |
| 107 | return array(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Appends custom parameters for the oembed request |
| 112 | * |
| 113 | * @param array $params |
| 114 | * @return void |
| 115 | */ |
| 116 | public function appendParams(array $params = array()) |
| 117 | { |
| 118 | $this->config['params'] = array_merge($this->config['params'], $params); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the url |
| 123 | * |
| 124 | * @return string |
| 125 | */ |
| 126 | public function getUrl() |
| 127 | { |
| 128 | return (string) $this->url; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns an array with all the parameters for the oembed request |
| 133 | * |
| 134 | * @return array |
| 135 | */ |
| 136 | public function getParams() |
| 137 | { |
| 138 | return $this->config['params']; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns an array with found errors |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public function getErrors() |
| 147 | { |
| 148 | return $this->errors; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * This method fakes a Oembed response. |
| 153 | * |
| 154 | * It should be overwritten by the service |
| 155 | * itself if the service is capable to determine |
| 156 | * an html embed code based on the url or by other methods. |
| 157 | * |
| 158 | * @return array with data that the oembed response should have |
| 159 | * @codeCoverageIgnore |
| 160 | */ |
| 161 | public function fakeResponse() |
| 162 | { |
| 163 | return array(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Normalizes a url. |
| 168 | * This method should be overwritten by the |
| 169 | * service itself, if needed. |
| 170 | * |
| 171 | * Use the $this->url property to do the job |
| 172 | * |
| 173 | * @return void |
| 174 | */ |
| 175 | protected function normalizeUrl() {} |
| 176 | |
| 177 | /** |
| 178 | * Gives the ability to modify the response/fake-response array |
| 179 | * from an oembed provider. |
| 180 | * |
| 181 | * It should be overwritten by the service when needed |
| 182 | * |
| 183 | * @param array $response |
| 184 | * @return array |
| 185 | */ |
| 186 | protected function modifyResponse(array $response = array()) |
| 187 | { |
| 188 | return $response; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | ?> |
| 193 |