55
66namespace ZendDiagnostics \Check ;
77
8- use Guzzle \Http \Client ;
9- use Guzzle \Http \ClientInterface ;
8+ use Guzzle \Http \Client as Guzzle3Client ;
9+ use Guzzle \Http \ClientInterface as Guzzle3ClientInterface ;
10+ use GuzzleHttp \Client as Guzzle4And5Client ;
11+ use GuzzleHttp \ClientInterface as Guzzle4And5ClientInterface ;
1012use ZendDiagnostics \Result \Failure ;
1113use ZendDiagnostics \Result \Success ;
1214
@@ -21,27 +23,33 @@ class GuzzleHttpService extends AbstractCheck
2123 protected $ guzzle ;
2224
2325 /**
24- * @param string $url The absolute url to check
25- * @param array $headers An array of headers used to create the request
26- * @param array $options An array of guzzle options used to create the request
27- * @param int $statusCode The response status code to check
28- * @param null $content The response content to check
29- * @param ClientInterface $guzzle Instance of guzzle to use
30- * @param string $method The method of the request
31- * @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
26+ * @param string $url The absolute url to check
27+ * @param array $headers An array of headers used to create the request
28+ * @param array $options An array of guzzle options used to create the request
29+ * @param int $statusCode The response status code to check
30+ * @param null $content The response content to check
31+ * @param \Guzzle\Http\Client|\GuzzleHttp\Client $guzzle Instance of guzzle to use
32+ * @param string $method The method of the request
33+ * @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
34+ *
35+ * @throws \InvalidArgumentException
3236 */
33- public function __construct ($ url , array $ headers = array (), array $ options = array (), $ statusCode = 200 , $ content = null , ClientInterface $ guzzle = null , $ method = 'GET ' , $ body = null )
37+ public function __construct ($ url , array $ headers = array (), array $ options = array (), $ statusCode = 200 , $ content = null , $ guzzle = null , $ method = 'GET ' , $ body = null )
3438 {
3539 $ this ->url = $ url ;
3640 $ this ->headers = $ headers ;
3741 $ this ->options = $ options ;
38- $ this ->statusCode = $ statusCode ;
42+ $ this ->statusCode = ( int ) $ statusCode ;
3943 $ this ->content = $ content ;
4044 $ this ->method = $ method ;
4145 $ this ->body = $ body ;
4246
4347 if (!$ guzzle ) {
44- $ guzzle = new Client ();
48+ $ guzzle = $ this ->createGuzzleClient ();
49+ }
50+
51+ if ((!$ guzzle instanceof Guzzle3ClientInterface) && (!$ guzzle instanceof Guzzle4And5ClientInterface)) {
52+ throw new \InvalidArgumentException ('Parameter "guzzle" must be an instance of "\Guzzle\Http\ClientInterface" or "\GuzzleHttp\ClientInterface" ' );
4553 }
4654
4755 $ this ->guzzle = $ guzzle ;
@@ -52,16 +60,97 @@ public function __construct($url, array $headers = array(), array $options = arr
5260 */
5361 public function check ()
5462 {
55- $ response = $ this ->guzzle ->createRequest ($ this ->method , $ this ->url , $ this ->headers , $ this ->body , $ this ->options )->send ();
63+ if ($ this ->guzzle instanceof Guzzle3ClientInterface) {
64+ return $ this ->guzzle3Check ();
65+ }
66+
67+ return $ this ->guzzle4And5Check ();
68+ }
69+
70+ /**
71+ * @return Failure|Success
72+ */
73+ private function guzzle3Check ()
74+ {
75+ $ response = $ this ->guzzle ->createRequest (
76+ $ this ->method ,
77+ $ this ->url ,
78+ $ this ->headers ,
79+ $ this ->body ,
80+ array_merge (array ('exceptions ' => false ), $ this ->options )
81+ )->send ();
5682
5783 if ($ this ->statusCode !== $ statusCode = $ response ->getStatusCode ()) {
58- return new Failure ( " Status code { $ this ->statusCode } does not match { $ statusCode} in response from { $ this -> url }" );
84+ return $ this ->createStatusCodeFailure ( $ statusCode );
5985 }
6086
6187 if ($ this ->content && (false === strpos ($ response ->getBody (true ), $ this ->content ))) {
62- return new Failure ("Content {$ this ->content } not found in response from {$ this ->url }" );
88+ return $ this ->createContentFailure ();
89+ }
90+
91+ return new Success ();
92+ }
93+
94+ /**
95+ * @return Failure|Success
96+ */
97+ private function guzzle4And5Check ()
98+ {
99+ $ request = $ this ->guzzle ->createRequest (
100+ $ this ->method ,
101+ $ this ->url ,
102+ array_merge (
103+ array ('headers ' => $ this ->headers , 'body ' => $ this ->body , 'exceptions ' => false ),
104+ $ this ->options
105+ )
106+ );
107+
108+ $ response = $ this ->guzzle ->send ($ request );
109+
110+ if ($ this ->statusCode !== $ statusCode = (int ) $ response ->getStatusCode ()) {
111+ return $ this ->createStatusCodeFailure ($ statusCode );
112+ }
113+
114+ if ($ this ->content && (false === strpos ((string ) $ response ->getBody (), $ this ->content ))) {
115+ return $ this ->createContentFailure ();
63116 }
64117
65118 return new Success ();
66119 }
120+
121+ /**
122+ * @param int $statusCode
123+ *
124+ * @return Failure
125+ */
126+ private function createStatusCodeFailure ($ statusCode )
127+ {
128+ return new Failure ("Status code {$ this ->statusCode } does not match {$ statusCode } in response from {$ this ->url }" );
129+ }
130+
131+ /**
132+ * @return Failure
133+ */
134+ private function createContentFailure ()
135+ {
136+ return new Failure ("Content {$ this ->content } not found in response from {$ this ->url }" );
137+ }
138+
139+ /**
140+ * @return \Guzzle\Http\Client|\GuzzleHttp\Client
141+ *
142+ * @throws \Exception
143+ */
144+ private function createGuzzleClient ()
145+ {
146+ if (class_exists ('GuzzleHttp\Client ' )) {
147+ return new Guzzle4And5Client ();
148+ }
149+
150+ if (!class_exists ('Guzzle\Http\Client ' )) {
151+ throw new \Exception ('Guzzle is required. ' );
152+ }
153+
154+ return new Guzzle3Client ();
155+ }
67156}
0 commit comments