1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Check\http\GuzzleHttpService; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
15
|
|
|
use GuzzleHttp\Message\Request as GuzzleRequest; |
|
|
|
|
16
|
|
|
use GuzzleHttp\Message\RequestInterface as GuzzleRequestInterface; |
|
|
|
|
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Psr\Http\Message\RequestInterface as PsrRequestInterface; |
19
|
|
|
use ZendDiagnostics\Result\Failure; |
20
|
|
|
use ZendDiagnostics\Result\Success; |
21
|
|
|
|
22
|
|
|
class GuzzleHttpService extends \ZendDiagnostics\Check\GuzzleHttpService |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
protected $setData = false; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @param string|PsrRequestInterface|GuzzleRequestInterface $requestOrUrl |
|
|
|
|
28
|
|
|
* The absolute url to check, or a |
29
|
|
|
* fully-formed request instance |
30
|
|
|
* @param array $headers An array of headers used to create the |
31
|
|
|
* request |
32
|
|
|
* @param array $options An array of guzzle options to use when |
33
|
|
|
* sending the request |
34
|
|
|
* @param int $statusCode The response status code to check |
35
|
|
|
* @param null $content The response content to check |
|
|
|
|
36
|
|
|
* @param null|GuzzleClientInterface $guzzle Instance of guzzle to use |
37
|
|
|
* @param string $method The method of the request |
38
|
|
|
* @param mixed $body The body of the request (used for POST, PUT |
39
|
|
|
* and DELETE requests) |
40
|
|
|
* @param bool $setData set data to result |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
2 |
|
public function __construct( |
45
|
|
|
$requestOrUrl, |
46
|
|
|
array $headers = [], |
47
|
|
|
array $options = [], |
48
|
|
|
$statusCode = 200, |
49
|
|
|
$content = null, |
50
|
|
|
$guzzle = null, |
51
|
|
|
$method = 'GET', |
52
|
|
|
$body = null, |
53
|
|
|
$setData = false) |
|
|
|
|
54
|
|
|
{ |
|
|
|
|
55
|
2 |
|
parent::__construct($requestOrUrl, $headers, $options, $statusCode, $content, $guzzle, $method, $body); |
56
|
|
|
|
57
|
2 |
|
$this->setData = $setData; |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* @see ZendDiagnostics\CheckInterface::check() |
62
|
|
|
*/ |
|
|
|
|
63
|
2 |
|
public function check() |
64
|
|
|
{ |
65
|
|
|
// GuzzleHttp\Message\RequestInterface only exists in v4 and v5. |
66
|
2 |
|
return class_exists(GuzzleRequest::class) |
67
|
|
|
? $this->performLegacyGuzzleRequest() |
68
|
2 |
|
: $this->performGuzzleRequest(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
73
|
|
|
* |
74
|
|
|
* @return \ZendDiagnostics\Result\ResultInterface |
75
|
|
|
*/ |
76
|
2 |
|
protected function performGuzzleRequest() |
77
|
|
|
{ |
78
|
2 |
|
$response = $this->guzzle->send($this->request, array_merge([ |
|
|
|
|
79
|
2 |
|
'exceptions' => false, |
80
|
2 |
|
], $this->options)); |
|
|
|
|
81
|
|
|
|
82
|
2 |
|
return $this->analyzeResponse($response); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* @param \GuzzleHttp\Message\ResponseInterface|Psr\Http\Message\ResponseInterface $response |
|
|
|
|
87
|
|
|
* |
88
|
|
|
* @return \ZendDiagnostics\Result\ResultInterface |
89
|
|
|
*/ |
90
|
2 |
|
protected function analyzeResponse($response) |
91
|
|
|
{ |
92
|
2 |
|
$result = $this->analyzeStatusCode((int) $response->getStatusCode()); |
93
|
2 |
|
if ($result instanceof Failure) { |
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
$result = $this->analyzeResponseContent((string) $response->getBody()); |
98
|
2 |
|
if ($result instanceof Failure) { |
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$data = null; |
103
|
2 |
|
if ($this->setData) { |
104
|
|
|
$data = (string) $response->getBody(); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return new Success(null, $data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* @param int $statusCode |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return bool|FailureInterface Returns boolean true when successful, and |
|
|
|
|
114
|
|
|
* a FailureInterface instance otherwise |
115
|
|
|
*/ |
116
|
2 |
|
protected function analyzeStatusCode($statusCode) |
117
|
|
|
{ |
118
|
2 |
|
return $this->statusCode === $statusCode |
|
|
|
|
119
|
2 |
|
? true |
120
|
|
|
: new Failure(sprintf( |
|
|
|
|
121
|
|
|
'Status code %s does not match %s in response from %s', |
122
|
|
|
$this->statusCode, |
123
|
|
|
$statusCode, |
124
|
2 |
|
$this->getUri() |
125
|
|
|
)); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
|
|
|
|
129
|
|
|
* @param string $content |
|
|
|
|
130
|
|
|
* |
131
|
|
|
* @return bool|FailureInterface Returns boolean true when successful, and |
132
|
|
|
* a FailureInterface instance otherwise |
133
|
|
|
*/ |
134
|
2 |
|
protected function analyzeResponseContent($content) |
135
|
|
|
{ |
136
|
2 |
|
return !$this->content || false !== mb_strpos($content, $this->content) |
|
|
|
|
137
|
2 |
|
? true |
138
|
|
|
: new Failure(sprintf( |
|
|
|
|
139
|
|
|
'Content %s not found in response from %s', |
140
|
|
|
$this->content, |
141
|
2 |
|
$this->getUri() |
142
|
|
|
)); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
|
|
|
|
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function getUri() |
149
|
|
|
{ |
150
|
|
|
return $this->request instanceof PsrRequestInterface |
151
|
|
|
? (string) $this->request->getUri() // guzzle 6 |
152
|
|
|
: $this->request->getUrl(); // guzzle 4 and 5 |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
|
|
|
|
156
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
157
|
|
|
* |
158
|
|
|
* @return \ZendDiagnostics\Result\ResultInterface |
159
|
|
|
*/ |
160
|
|
|
private function performLegacyGuzzleRequest() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$response = $this->guzzle->send($this->request); |
163
|
|
|
|
164
|
|
|
return $this->analyzeResponse($response); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|