44use ScriptFUSION \Porter \Connector \CachingConnector ;
55use ScriptFUSION \Porter \Net \UrlBuilder ;
66use ScriptFUSION \Porter \Options \EncapsulatedOptions ;
7- use ScriptFUSION \Retry \ErrorHandler \ExponentialBackoffErrorHandler ;
87
98/**
109 * Fetches data from an HTTP server via the PHP wrapper.
1413 */
1514class HttpConnector extends CachingConnector
1615{
17- const DEFAULT_TRIES = 5 ;
18-
1916 /** @var HttpOptions */
2017 private $ options ;
2118
@@ -25,9 +22,6 @@ class HttpConnector extends CachingConnector
2522 /** @var string */
2623 private $ baseUrl ;
2724
28- /** @var int */
29- private $ tries = self ::DEFAULT_TRIES ;
30-
3125 public function __construct (HttpOptions $ options = null )
3226 {
3327 parent ::__construct ();
@@ -53,34 +47,32 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
5347 throw new \InvalidArgumentException ('Options must be an instance of HttpOptions. ' );
5448 }
5549
56- return \ScriptFUSION \Retry \retry ($ this ->getTries (), function () use ($ source , $ options ) {
57- if (false === $ response = @file_get_contents (
58- $ this ->getOrCreateUrlBuilder ()->buildUrl (
59- $ source ,
60- $ options ? $ options ->getQueryParameters () : [],
61- $ this ->getBaseUrl ()
50+ if (false === $ response = @file_get_contents (
51+ $ this ->getOrCreateUrlBuilder ()->buildUrl (
52+ $ source ,
53+ $ options ? $ options ->getQueryParameters () : [],
54+ $ this ->getBaseUrl ()
55+ ),
56+ false ,
57+ stream_context_create ([
58+ 'http ' => ['ignore_errors ' => true ] + array_merge (
59+ $ this ->options ->extractHttpContextOptions (),
60+ $ options ? $ options ->extractHttpContextOptions () : []
6261 ),
63- false ,
64- stream_context_create ([
65- 'http ' => ['ignore_errors ' => true ] + array_merge (
66- $ this ->options ->extractHttpContextOptions (),
67- $ options ? $ options ->extractHttpContextOptions () : []
68- ),
69- ])
70- )) {
71- $ error = error_get_last ();
72- throw new HttpConnectionException ($ error ['message ' ], $ error ['type ' ]);
73- }
62+ ])
63+ )) {
64+ $ error = error_get_last ();
65+ throw new HttpConnectionException ($ error ['message ' ], $ error ['type ' ]);
66+ }
7467
75- $ code = explode (' ' , $ http_response_header [0 ], 3 )[1 ];
76- if ($ code < 200 || $ code >= 400 ) {
77- throw new HttpServerException (
78- "HTTP server responded with error: \"$ http_response_header [0 ]\". \n\n$ response "
79- );
80- }
68+ $ code = explode (' ' , $ http_response_header [0 ], 3 )[1 ];
69+ if ($ code < 200 || $ code >= 400 ) {
70+ throw new HttpServerException (
71+ "HTTP server responded with error: \"$ http_response_header [0 ]\". \n\n$ response "
72+ );
73+ }
8174
82- return $ response ;
83- }, new ExponentialBackoffErrorHandler );
75+ return $ response ;
8476 }
8577
8678 private function getOrCreateUrlBuilder ()
@@ -107,28 +99,4 @@ public function setBaseUrl($baseUrl)
10799
108100 return $ this ;
109101 }
110-
111- /**
112- * Gets the maximum number of fetch attempts
113- *
114- * @return int
115- */
116- public function getTries ()
117- {
118- return $ this ->tries ;
119- }
120-
121- /**
122- * Sets the maximum number of fetch attempts.
123- *
124- * @param int $tries Maximum fetch attempts.
125- *
126- * @return $this
127- */
128- public function setTries ($ tries )
129- {
130- $ this ->tries = max (1 , $ tries |0 );
131-
132- return $ this ;
133- }
134102}
0 commit comments