/**
* This method is used to extend an URL if a location header is set in the HTTP Headers
*
* The method takes the URL that should be extended as a paremter and firstly builds a cacheName by the URL, because
* if caching is activated the library tries to get the result of the method from cache instead of requesting it
* newly every time. If caching is activated the method get_cache of the caching class is used to try to get cache
* data. If cached data is available it is returned. Otherwise a new request is made using the request method of Guzzle.
* At this state it's possible that Exceptions are thrown due to HTTP Stauts codes so a Client and Server Exception
* is caught and thrown again as URLExtenderException. If no exception is thrown and caching isn't activated
* the location parameter (if present) is returned, otherwise the original URL. If caching is activated the location
* is saved to cache and then returned
*
* @see SimpleCache::get_cache
* @see SimpleCache::set_cache
* @see MessageInterface::getHeaderline
* @see ClientException
* @see ServerException
*
* @param $url
* @return bool|string
* @throws URLExtenderException
*/
public function extendURL($url)
{
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
throw new URLExtenderException('Invalid URL');
}
$cacheName = $this->getCacheName($url);
if ($this->usingCache === true) {
$location = $this->cache->get_cache($cacheName);
if ($location !== false) {
return $location;
}
}
try {
$result = $this->request($url);
$location = $result->getHeaderLine('Location') !== '' ? $result->getHeaderLine('Location') : $url;
if ($this->usingCache !== true) {
return $location;
}
$this->cache->set_cache($cacheName, $location);
return $location;
} catch (ClientException $e) {
throw new URLExtenderException($e->getMessage(), $e->getCode());
} catch (ServerException $e) {
throw new URLExtenderException($e->getMessage(), $e->getCode());
}
}