5
5
6
6
namespace ZendDiagnostics \Check ;
7
7
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 ;
10
12
use ZendDiagnostics \Result \Failure ;
11
13
use ZendDiagnostics \Result \Success ;
12
14
@@ -21,27 +23,33 @@ class GuzzleHttpService extends AbstractCheck
21
23
protected $ guzzle ;
22
24
23
25
/**
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
32
36
*/
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 )
34
38
{
35
39
$ this ->url = $ url ;
36
40
$ this ->headers = $ headers ;
37
41
$ this ->options = $ options ;
38
- $ this ->statusCode = $ statusCode ;
42
+ $ this ->statusCode = ( int ) $ statusCode ;
39
43
$ this ->content = $ content ;
40
44
$ this ->method = $ method ;
41
45
$ this ->body = $ body ;
42
46
43
47
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" ' );
45
53
}
46
54
47
55
$ this ->guzzle = $ guzzle ;
@@ -52,16 +60,97 @@ public function __construct($url, array $headers = array(), array $options = arr
52
60
*/
53
61
public function check ()
54
62
{
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 ();
56
82
57
83
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 );
59
85
}
60
86
61
87
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 ();
63
116
}
64
117
65
118
return new Success ();
66
119
}
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
+ }
67
156
}
0 commit comments