/** * 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()); } }
/** * Get the data from the cache . * Returns null if the data has already expired or cache does not exist. * * @param String $key Unique key to identify the cache value * * @return mixed|null */ public static function get($key) { $cache = new SimpleCache(); if ($data = $cache->get_cache($key)) { return json_decode($data); } return null; }
/** * Gets cached data * * @return $this * @throws DataNotLoadedException */ protected function withCache() { $this->type = 'fromCache'; $data = $this->cache->is_cached($this->label) ? $this->cache->get_cache($this->label) : false; $this->data = $data ? json_decode($data, true) : false; if (!$this->data) { $this->cache->set_cache($this->label, json_encode($this->withoutCache()->get())); } return $this; }