1616use ScriptFUSION \Porter \Provider \Resource \ProviderResource ;
1717use ScriptFUSION \Porter \Specification \ImportSpecification ;
1818use ScriptFUSION \Porter \Transform \Transformer ;
19- use ScriptFUSION \Retry \ExceptionHandler \ExponentialBackoffExceptionHandler ;
2019
2120/**
2221 * Imports data according to an ImportSpecification.
2322 */
2423class Porter
2524{
26- const DEFAULT_FETCH_ATTEMPTS = 5 ;
27-
2825 /**
2926 * @var Provider[]
3027 */
@@ -35,16 +32,6 @@ class Porter
3532 */
3633 private $ providerFactory ;
3734
38- /**
39- * @var int
40- */
41- private $ maxFetchAttempts = self ::DEFAULT_FETCH_ATTEMPTS ;
42-
43- /**
44- * @var callable
45- */
46- private $ fetchExceptionHandler ;
47-
4835 /**
4936 * Imports data according to the design of the specified import specification.
5037 *
@@ -57,7 +44,13 @@ class Porter
5744 public function import (ImportSpecification $ specification )
5845 {
5946 $ specification = clone $ specification ;
60- $ records = $ this ->fetch ($ specification ->getResource (), $ specification ->getCacheAdvice ());
47+
48+ $ records = $ this ->fetch (
49+ $ specification ->getResource (),
50+ $ specification ->getCacheAdvice (),
51+ $ specification ->getMaxFetchAttempts (),
52+ $ specification ->getFetchExceptionHandler ()
53+ );
6154
6255 if (!$ records instanceof ProviderRecords) {
6356 $ records = $ this ->createProviderRecords ($ records , $ specification ->getResource ());
@@ -94,24 +87,24 @@ public function importOne(ImportSpecification $specification)
9487 return $ one ;
9588 }
9689
97- private function fetch (ProviderResource $ resource , CacheAdvice $ cacheAdvice = null )
90+ private function fetch (ProviderResource $ resource , CacheAdvice $ cacheAdvice, $ fetchAttempts , $ fetchExceptionHandler )
9891 {
9992 $ provider = $ this ->getProvider ($ resource ->getProviderClassName (), $ resource ->getProviderTag ());
10093
10194 $ this ->applyCacheAdvice ($ provider , $ cacheAdvice );
10295
10396 if (($ records = \ScriptFUSION \Retry \retry (
104- $ this -> getMaxFetchAttempts () ,
97+ $ fetchAttempts ,
10598 function () use ($ provider , $ resource ) {
10699 return $ provider ->fetch ($ resource );
107100 },
108- function (\Exception $ exception ) {
101+ function (\Exception $ exception ) use ( $ fetchExceptionHandler ) {
109102 // Throw exception if unrecoverable.
110103 if (!$ exception instanceof RecoverableConnectorException) {
111104 throw $ exception ;
112105 }
113106
114- call_user_func ( $ this -> getFetchExceptionHandler (), $ exception );
107+ $ fetchExceptionHandler ( $ exception );
115108 }
116109 )) instanceof \Iterator) {
117110 return $ records ;
@@ -266,44 +259,4 @@ private function getOrCreateProviderFactory()
266259 {
267260 return $ this ->providerFactory ?: $ this ->providerFactory = new ProviderFactory ;
268261 }
269-
270- /**
271- * Gets the maximum number of fetch attempts per import.
272- *
273- * @return int
274- */
275- public function getMaxFetchAttempts ()
276- {
277- return $ this ->maxFetchAttempts ;
278- }
279-
280- /**
281- * Sets the maximum number of fetch attempts per import.
282- *
283- * @param int $attempts Maximum fetch attempts.
284- *
285- * @return $this
286- */
287- public function setMaxFetchAttempts ($ attempts )
288- {
289- $ this ->maxFetchAttempts = max (1 , $ attempts |0 );
290-
291- return $ this ;
292- }
293-
294- /**
295- * @return callable
296- */
297- private function getFetchExceptionHandler ()
298- {
299- return $ this ->fetchExceptionHandler ?: $ this ->fetchExceptionHandler = new ExponentialBackoffExceptionHandler ;
300- }
301-
302- /**
303- * @param callable $fetchExceptionHandler
304- */
305- public function setFetchExceptionHandler (callable $ fetchExceptionHandler )
306- {
307- $ this ->fetchExceptionHandler = $ fetchExceptionHandler ;
308- }
309262}
0 commit comments