1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2017-04-09 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Fetcher; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use Net\Bazzline\Component\Curl\Builder\Builder; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
class HttpFetcher extends AbstractFetcher |
14
|
|
|
{ |
15
|
|
|
const STATUS_CODE_HIGHER_THAN_ALLOWED_EXCEPTION_MESSAGE = 'Returned response code >>%d<< is above the limit of >>%d<<. Dumping the response >>%s<<.'; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $highestAllowedStatusCode; |
19
|
|
|
|
20
|
|
|
/** @var Builder */ |
21
|
|
|
private $requestBuilder; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $url; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* HttpFetcher constructor. |
30
|
|
|
* |
31
|
|
|
* @param Builder $builder |
32
|
|
|
* @param int $highestAllowedStatusCode |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
Builder $builder, |
36
|
|
|
$highestAllowedStatusCode = 400 |
37
|
|
|
) { |
38
|
|
|
$this->requestBuilder = $builder; |
39
|
|
|
$this->highestAllowedStatusCode = $highestAllowedStatusCode; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $url |
44
|
|
|
*/ |
45
|
|
|
public function setUrl($url) |
46
|
|
|
{ |
47
|
|
|
$this->url = $url; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
* @throws RuntimeException |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
protected function fetchContentAsStringOrThrowRuntimeException() |
58
|
|
|
{ |
59
|
|
|
//begin of dependencies |
60
|
|
|
$highestAllowedStatusCode = $this->highestAllowedStatusCode; |
61
|
|
|
$requestBuilder = $this->requestBuilder; |
62
|
|
|
$url = $this->url; |
63
|
|
|
//end of dependencies |
64
|
|
|
|
65
|
|
|
//begin of business logic |
66
|
|
|
$requestBuilder->useGet(); |
67
|
|
|
$requestBuilder->onTheUrl($url . '?notable'); |
68
|
|
|
|
69
|
|
|
$response = $requestBuilder->andFetchTheResponse(); |
70
|
|
|
|
71
|
|
|
if ($response->statusCode() >= $highestAllowedStatusCode) { |
72
|
|
|
throw new RuntimeException( |
73
|
|
|
sprintf( |
74
|
|
|
self::STATUS_CODE_HIGHER_THAN_ALLOWED_EXCEPTION_MESSAGE, |
75
|
|
|
$response->statusCode(), |
76
|
|
|
$highestAllowedStatusCode, |
77
|
|
|
implode( |
78
|
|
|
', ', |
79
|
|
|
$response->convertIntoAnArray() |
80
|
|
|
) |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $response->content(); |
86
|
|
|
//end of business logic |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|