Example #1
0
 public function fetchFreshData($source, EncapsulatedOptions $options = null)
 {
     if ($options && !$options instanceof SoapOptions) {
         throw new \InvalidArgumentException('Options must be an instance of SoapOptions.');
     }
     $params = array_merge($this->options->getParameters(), $options ? $options->getParameters() : []);
     return ObjectType::toArray(\ScriptFUSION\Retry\retry(5, function () use($source, $params) {
         return $this->getOrCreateClient()->{$source}($params);
     }, new ExponentialBackoffErrorHandler()));
 }
Example #2
0
 public function fetch($source, EncapsulatedOptions $options = null)
 {
     $optionsCopy = $options ? $options->copy() : [];
     if ($this->isCacheEnabled()) {
         ksort($optionsCopy);
         $hash = $this->hash([$source, $optionsCopy]);
         if ($this->cache->hasItem($hash)) {
             return $this->cache->getItem($hash)->get();
         }
     }
     $data = $this->fetchFreshData($source, $options);
     isset($hash) && $this->cache->save($this->cache->getItem($hash)->set($data));
     return $data;
 }
Example #3
0
 /**
  * {@inheritdoc}
  *
  * @param string $source Source.
  * @param EncapsulatedOptions|null $options Optional. Options.
  *
  * @return string Response.
  *
  * @throws \InvalidArgumentException Options is not an instance of HttpOptions.
  * @throws HttpConnectionException Failed to connect to source.
  * @throws HttpServerException Server sent an error code.
  */
 public function fetchFreshData($source, EncapsulatedOptions $options = null)
 {
     if ($options && !$options instanceof HttpOptions) {
         throw new \InvalidArgumentException('Options must be an instance of HttpOptions.');
     }
     return \ScriptFUSION\Retry\retry($this->getTries(), function () use($source, $options) {
         if (false === ($response = @file_get_contents($this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : [], $this->getBaseUrl()), false, stream_context_create(['http' => ['ignore_errors' => true] + array_merge($this->options->extractHttpContextOptions(), $options ? $options->extractHttpContextOptions() : [])])))) {
             $error = error_get_last();
             throw new HttpConnectionException($error['message'], $error['type']);
         }
         $code = explode(' ', $http_response_header[0], 3)[1];
         if ($code < 200 || $code >= 400) {
             throw new HttpServerException("HTTP server responded with error: \"{$http_response_header['0']}\".\n\n{$response}");
         }
         return $response;
     }, new ExponentialBackoffErrorHandler());
 }