Skip to content

Commit

Permalink
refactored internals of Request::execute()
Browse files Browse the repository at this point in the history
  • Loading branch information
stevleibelt committed Feb 8, 2016
1 parent 0b49e5f commit ff76827
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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*
Expand Down
64 changes: 46 additions & 18 deletions source/Request/Request.php
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit ff76827

Please sign in to comment.