From ff76827fc02325a1ee88194203d5369c1e4c10dc Mon Sep 17 00:00:00 2001 From: stevleibelt Date: Mon, 8 Feb 2016 12:36:55 +0100 Subject: [PATCH] refactored internals of Request::execute() --- README.md | 3 ++ source/Request/Request.php | 64 +++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2220104..ae12ee1 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,10 @@ If you want to change this, you either have to extend the existing *Request* or * add RequestModifier * e.g. for adding the JsonModifier which converts the data into a json, adds the fitting ContentType etc. * replace current dispatcher and logging dispatcher strategy with an event driven approach (currently only needed for logging)? + * create Request Data Domain Object + * create Request Options Domain Object * refactored internals of Builder::andFetchTheResponse() + * refactored internals of Request::execute() * refactored internals of RequestFactory::create() * [0.10.0](https://github.com/bazzline/php_component_curl/tree/0.10.0) - released at 08.02.2016 * added public *overwriteRequestFactory()* to *BuilderFactory* diff --git a/source/Request/Request.php b/source/Request/Request.php index 08e0574..b49480a 100644 --- a/source/Request/Request.php +++ b/source/Request/Request.php @@ -205,25 +205,13 @@ public function reset($alsoTheDefaults = false) } /** - * @param string $url - * @param string $method - * @param null|array $parameters - * @param null|string|array $data - * @return Response + * @param array $options + * @param mixed $data + * @return array */ - private function execute($url, $method, array $parameters = array(), $data = null) + private function addDataToTheOptionsIfDataIsValid(array $options, $data) { - $areParametersProvided = (!empty($parameters)); - $dispatcher = $this->dispatcher; - $merge = $this->merge; - $headerLines = $merge($this->headerLines, $this->defaultHeaderLines); - $isDataProvided = (!is_null($data)); - $options = $merge($this->options, $this->defaultOptions); - $headerLines[] = 'X-HTTP-Method-Override: ' . $method; //@see: http://tr.php.net/curl_setopt#109634 - - $options[CURLOPT_CUSTOMREQUEST] = $method; //@see: http://tr.php.net/curl_setopt#109634 - $options[CURLOPT_HTTPHEADER] = $headerLines; - //@todo what about binary transfer? + $isDataProvided = (!is_null($data)); if ($isDataProvided) { $dataIsNotFromTypeScalar = (!is_scalar($data)); @@ -239,6 +227,18 @@ private function execute($url, $method, array $parameters = array(), $data = nul } } + return $options; + } + + /** + * @param array $parameters + * @param string $url + * @return string + */ + private function addParametersToTheUrlIfParametersAreProvided(array $parameters, $url) + { + $areParametersProvided = (!empty($parameters)); + if ($areParametersProvided) { $parametersAsString = http_build_query($parameters); $isParameterStringValid = (strlen($parametersAsString) > 0); @@ -252,7 +252,35 @@ private function execute($url, $method, array $parameters = array(), $data = nul $urlWithParameters = $url; } - $response = $dispatcher->dispatch($urlWithParameters, $options); + return $urlWithParameters; + } + + /** + * @param string $url + * @param string $method + * @param null|array $parameters + * @param null|string|array $data + * @return Response + */ + private function execute($url, $method, array $parameters = array(), $data = null) + { + //begin of dependencies + $dispatcher = $this->dispatcher; + $merge = $this->merge; + $headerLines = $merge($this->headerLines, $this->defaultHeaderLines); + $options = $merge($this->options, $this->defaultOptions); + $headerLines[] = 'X-HTTP-Method-Override: ' . $method; //@see: http://tr.php.net/curl_setopt#109634 + //end of dependencies + + //begin of business logic + $options[CURLOPT_CUSTOMREQUEST] = $method; //@see: http://tr.php.net/curl_setopt#109634 + $options[CURLOPT_HTTPHEADER] = $headerLines; + //@todo what about binary transfer? + + $options = $this->addDataToTheOptionsIfDataIsValid($options, $data); + $urlWithParameters = $this->addParametersToTheUrlIfParametersAreProvided($parameters, $url); + $response = $dispatcher->dispatch($urlWithParameters, $options); + //end of business logic return $response; }