Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

AbstractClient::request()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 8
nop 1
dl 0
loc 32
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wggithub\Github\Http;
4
5
use XoopsModules\Wggithub\Github;
6
7
8
/**
9
 * Ancestor for HTTP clients. Cares about redirecting and debug events.
10
 *
11
 * @author  Miloslav Hůla (https://github.com/milo)
12
 */
13
abstract class AbstractClient extends Github\Sanity implements IClient
14
{
15
	/** @var int[]  will follow Location header on these response codes */
16
	public $redirectCodes = [
17
		Response::S301_MOVED_PERMANENTLY,
18
		Response::S302_FOUND,
19
		Response::S307_TEMPORARY_REDIRECT,
20
	];
21
22
	/** @var int  maximum redirects per request*/
23
	public $maxRedirects = 5;
24
25
	/** @var callable|NULL */
26
	private $onRequest;
27
28
	/** @var callable|NULL */
29
	private $onResponse;
30
31
32
	/**
33
	 * @see https://developer.github.com/v3/#http-redirects
34
	 *
35
	 * @return Response
36
	 *
37
	 * @throws BadResponseException
38
	 */
39
	public function request(Request $request)
40
	{
41
		$request = clone $request;
42
43
		$counter = $this->maxRedirects;
44
		$previous = NULL;
45
		do {
46
			$this->setupRequest($request);
47
48
			$this->onRequest && call_user_func($this->onRequest, $request);
49
			$response = $this->process($request);
50
			$this->onResponse && call_user_func($this->onResponse, $response);
51
52
			$previous = $response->setPrevious($previous);
53
54
			if ($counter > 0 && in_array($response->getCode(), $this->redirectCodes) && $response->hasHeader('Location')) {
55
				/** @todo Use the same HTTP $method for redirection? Set $content to NULL? */
56
				$request = new Request(
57
					$request->getMethod(),
58
					$response->getHeader('Location'),
59
					$request->getHeaders(),
60
					$request->getContent()
61
				);
62
63
				$counter--;
64
				continue;
65
			}
66
			break;
67
68
		} while (TRUE);
69
70
		return $response;
71
	}
72
73
74
	/**
75
	 * @param  callable|NULL function(Request $request)
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wggithub\Github\Http\function was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
	 * @return self
77
	 */
78
	public function onRequest($callback)
79
	{
80
		$this->onRequest = $callback;
81
		return $this;
82
	}
83
84
85
	/**
86
	 * @param  callable|NULL function(Response $response)
87
	 * @return self
88
	 */
89
	public function onResponse($callback)
90
	{
91
		$this->onResponse = $callback;
92
		return $this;
93
	}
94
95
96
	protected function setupRequest(Request $request)
97
	{
98
		$request->addHeader('Expect', '');
99
	}
100
101
102
	/**
103
	 * @return Response
104
	 *
105
	 * @throws BadResponseException
106
	 */
107
	abstract protected function process(Request $request);
108
109
}
110