2121use Circle \DoctrineRestDriver \Annotations \RoutingTable ;
2222use Circle \DoctrineRestDriver \Enums \HttpMethods ;
2323use Circle \DoctrineRestDriver \Exceptions \Exceptions ;
24- use Circle \DoctrineRestDriver \Factory \RestClientFactory ;
2524use Circle \DoctrineRestDriver \Security \AuthStrategy ;
2625use Circle \DoctrineRestDriver \Transformers \MysqlToRequest ;
2726use Circle \DoctrineRestDriver \Types \Authentication ;
4039 * @copyright 2015 TeeAge-Beatz UG
4140 *
4241 * @SuppressWarnings("PHPMD.TooManyPublicMethods")
43- * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
4442 */
4543class Statement implements \IteratorAggregate, StatementInterface {
4644
@@ -60,9 +58,9 @@ class Statement implements \IteratorAggregate, StatementInterface {
6058 private $ params = [];
6159
6260 /**
63- * @var \Circle\DoctrineRestDriver\Factory\RestClientFactory
61+ * @var RestClient
6462 */
65- private $ restClientFactory ;
63+ private $ restClient ;
6664
6765 /**
6866 * @var array
@@ -118,7 +116,7 @@ public function __construct($query, array $options, RoutingTable $routings) {
118116 $ this ->query = $ query ;
119117 $ this ->routings = $ routings ;
120118 $ this ->mysqlToRequest = new MysqlToRequest ($ options , $ this ->routings );
121- $ this ->restClientFactory = new RestClientFactory ();
119+ $ this ->restClient = new RestClient ();
122120
123121 $ this ->authStrategy = Authentication::create ($ options );
124122 $ this ->formatter = Format::create ($ options );
@@ -157,17 +155,20 @@ public function errorInfo() {
157155
158156 /**
159157 * {@inheritdoc}
158+ *
159+ * @SuppressWarnings("PHPMD.StaticAccess")
160160 */
161161 public function execute ($ params = null ) {
162162 $ rawRequest = $ this ->mysqlToRequest ->transform ($ this ->query , $ this ->params );
163163 $ request = $ this ->authStrategy ->transformRequest ($ rawRequest );
164- $ restClient = $ this ->restClientFactory ->createOne ($ request ->getCurlOptions ());
165164
166- $ method = strtolower ($ request ->getMethod ());
167- $ response = $ method === HttpMethods::GET || $ method === HttpMethods::DELETE ? $ restClient ->$ method ($ request ->getUrlAndQuery ()) : $ restClient ->$ method ($ request ->getUrlAndQuery (), $ request ->getPayload ());
168- $ statusCode = $ response ->getStatusCode ();
165+ $ response = $ this ->restClient ->send ($ request );
169166
170- return $ statusCode === $ request ->getExpectedStatusCode () ? $ this ->onSuccess ($ response , $ method ) : $ this ->onError ($ request , $ response );
167+ $ this ->result = Result::create ($ this ->query , $ this ->formatter ->decode ($ response ->getContent ()));
168+ $ this ->id = !empty ($ this ->result ['id ' ]) ? $ this ->result ['id ' ] : null ;
169+ krsort ($ this ->result );
170+
171+ return true ;
171172 }
172173
173174 /**
@@ -248,37 +249,4 @@ public function getIterator() {
248249 public function getId () {
249250 return $ this ->id ;
250251 }
251-
252- /**
253- * Handles the statement if the execution succeeded
254- *
255- * @param Response $response
256- * @param string $method
257- * @return bool
258- *
259- * @SuppressWarnings("PHPMD.StaticAccess")
260- */
261- private function onSuccess (Response $ response , $ method ) {
262- $ this ->result = Result::create ($ this ->query , $ this ->formatter ->decode ($ response ->getContent ()));
263- $ this ->id = $ method === HttpMethods::POST ? $ this ->result ['id ' ] : null ;
264- krsort ($ this ->result );
265-
266- return true ;
267- }
268-
269- /**
270- * Handles the statement if the execution failed
271- *
272- * @param Request $request
273- * @param Response $response
274- * @throws \Exception
275- *
276- * @SuppressWarnings("PHPMD.StaticAccess")
277- */
278- private function onError (Request $ request , Response $ response ) {
279- $ this ->errorCode = $ response ->getStatusCode ();
280- $ this ->errorMessage = $ response ->getContent ();
281-
282- return Exceptions::RequestFailedException ($ request , $ response ->getStatusCode (), $ response ->getContent ());
283- }
284252}
0 commit comments