Completed
Push — master ( 3cf298...59bc3b )
by adam
05:40
created

MiddlewareFactory::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediawiki\Api\Guzzle;
4
5
use GuzzleHttp\Exception\ConnectException;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Middleware;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Psr7\Response;
10
use Psr\Log\LoggerAwareInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
14
/**
15
 * @access private
16
 *
17
 * @author Addshore
18
 */
19
class MiddlewareFactory implements LoggerAwareInterface {
20
21
	/**
22
	 * @var LoggerInterface
23
	 */
24
	private $logger;
25
26 4
	public function __construct() {
27 4
		$this->logger = new NullLogger();
28 4
	}
29
30
	public function setLogger( LoggerInterface $logger ) {
31
		$this->logger = $logger;
32
	}
33
34
	/**
35
	 * @return callable
36
	 */
37 4
	public function retry() {
38 4
		return Middleware::retry( $this->newRetryDecider() );
39
	}
40
41
	/**
42
	 * @return callable
43
	 */
44
	private function newRetryDecider() {
45 4
		return function (
46
			$retries,
47
			Request $request,
48
			Response $response = null,
49
			RequestException $exception = null
50
		) {
51
			// Don't retry if we have run out of retries
52 4
			if ( $retries >= 5 ) {
53 1
				return false;
54
			}
55
56 4
			$shouldRetry = false;
57
58
			// Retry connection exceptions
59 4
			if( $exception instanceof ConnectException ) {
60 2
				$shouldRetry = true;
61 2
			}
62
63 4
			if( $response ) {
64 3
				$headers = $response->getHeaders();
65
66
				// Retry on server errors
67 3
				if( $response->getStatusCode() >= 500 ) {
68 1
					$shouldRetry = true;
69 1
				}
70
71
				// Retry if we have a response with an API error worth retrying
72 3
				if ( array_key_exists( 'mediawiki-api-error', $headers ) ) {
73 1
					foreach( $headers['mediawiki-api-error'] as $mediawikiApiErrorHeader ) {
74 1
						if ( in_array(
75 1
							$mediawikiApiErrorHeader,
76
							array(
77 1
								'ratelimited',
78 1
								'readonly',
79 1
								'internal_api_error_DBQueryError',
80 1
							)
81 1
						) ) {
82 1
							$shouldRetry = true;
83 1
						}
84 1
					}
85 1
				}
86 3
			}
87
88
			// Log if we are retrying
89 4
			if( $shouldRetry ) {
90 4
				$this->logger->warning(
91 4
					sprintf(
92 4
						'Retrying %s %s %s/5, %s',
93 4
						$request->getMethod(),
94 4
						$request->getUri(),
95 4
						$retries + 1,
96 4
						$response ? 'status code: ' . $response->getStatusCode() :
97 2
							$exception->getMessage()
0 ignored issues
show
Bug introduced by
It seems like $exception is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
98 4
					)
99 4
				);
100 4
			}
101
102 4
			return $shouldRetry;
103 4
		};
104
	}
105
106
}
107