|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 4 | + */ |
| 5 | + |
| 6 | +namespace ZendDiagnostics\Check; |
| 7 | + |
| 8 | +use Guzzle\Http\Client; |
| 9 | +use Guzzle\Http\ClientInterface; |
| 10 | +use ZendDiagnostics\Result\Failure; |
| 11 | +use ZendDiagnostics\Result\Success; |
| 12 | + |
| 13 | +class GuzzleHttpService extends AbstractCheck |
| 14 | +{ |
| 15 | + protected $url; |
| 16 | + protected $headers; |
| 17 | + protected $statusCode; |
| 18 | + protected $content; |
| 19 | + protected $guzzle; |
| 20 | + |
| 21 | + /** |
| 22 | + * @param string $url The absolute url to check |
| 23 | + * @param array $headers An array of headers used to create the request |
| 24 | + * @param array $options An array of guzzle options used to create the request |
| 25 | + * @param int $statusCode The response status code to check |
| 26 | + * @param null $content The response content to check |
| 27 | + * @param ClientInterface $guzzle Instance of guzzle to use |
| 28 | + */ |
| 29 | + public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, ClientInterface $guzzle = null) |
| 30 | + { |
| 31 | + $this->url = $url; |
| 32 | + $this->headers = $headers; |
| 33 | + $this->options = $options; |
| 34 | + $this->statusCode = $statusCode; |
| 35 | + $this->content = $content; |
| 36 | + |
| 37 | + if (!$guzzle) { |
| 38 | + $guzzle = new Client(); |
| 39 | + } |
| 40 | + |
| 41 | + $this->guzzle = $guzzle; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @see ZendDiagnostics\CheckInterface::check() |
| 46 | + */ |
| 47 | + public function check() |
| 48 | + { |
| 49 | + $response = $this->guzzle->get($this->url, $this->headers, $this->options)->send(); |
| 50 | + |
| 51 | + if ($this->statusCode !== $response->getStatusCode()) { |
| 52 | + return new Failure("Status code {$this->statusCode} does not match response from {$this->url}"); |
| 53 | + } |
| 54 | + |
| 55 | + if ($this->content && !strpos($response->getBody(true), $this->content)) { |
| 56 | + return new Failure("Content {$this->content} not found in response from {$this->url}"); |
| 57 | + } |
| 58 | + |
| 59 | + return new Success(); |
| 60 | + } |
| 61 | +} |
0 commit comments