This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'curl_cleanup' into 'master'
Add php curl as default connection, cleanup See merge request klatys/papi!1
- Loading branch information
Showing
7 changed files
with
152 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
namespace PApi\Connection; | ||
|
||
use PApi\Exception\ConnectionException; | ||
|
||
class CurlConnection extends AbstractConnection | ||
{ | ||
/** @var resource|null */ | ||
private $connection; | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->connection !== null) { | ||
curl_close($this->connection); | ||
} | ||
} | ||
|
||
protected function connect() : void | ||
{ | ||
$this->connection = curl_init(); | ||
} | ||
|
||
/** | ||
* @param mixed[] $config | ||
*/ | ||
public function create(array $config) : ConnectionInterface | ||
{ | ||
return new self($config); | ||
} | ||
|
||
/** | ||
* @param mixed[] $query | ||
*/ | ||
public function execute(string $endPoint, array $query = []) : string | ||
{ | ||
if ($this->connection === null) { | ||
$this->connect(); | ||
} | ||
curl_reset($this->connection); | ||
|
||
$queryParameters = http_build_query($query); | ||
$queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '%5B%5D=', $queryParameters); | ||
$uriParts = $this->getBaseUriParts(); | ||
$uriParts['path'] .= $endPoint; | ||
$uriParts['query'] = $queryString; | ||
$url = $this->buildUrl($uriParts); | ||
|
||
curl_setopt($this->connection, CURLOPT_URL, $url); | ||
curl_setopt($this->connection, CURLOPT_TIMEOUT, $this->config['timeout']); | ||
curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, true); | ||
$curlHeaders = array_map(function ($key, $value) { | ||
return $key . ':' . $value; | ||
}, $this->config['connectionHeaders']); | ||
curl_setopt($this->connection, CURLOPT_HTTPHEADER, $curlHeaders); | ||
|
||
$response = curl_exec($this->connection); | ||
if ($response === false) { | ||
throw new ConnectionException( | ||
sprintf( | ||
'Request GET %s returned with code %d and message `%s`', | ||
$url, | ||
curl_getinfo($this->connection, CURLINFO_RESPONSE_CODE), | ||
curl_error($this->connection) | ||
) | ||
); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param string[] $parts | ||
*/ | ||
private function buildUrl(array $parts) : string | ||
{ | ||
$url = $parts['scheme'] . '://'; | ||
|
||
if (isset($parts['user'])) { | ||
$url .= $parts['user']; | ||
if (isset($parts['pass'])) { | ||
$url .= ':' . $parts['pass']; | ||
} | ||
$url .= '@'; | ||
} | ||
|
||
$url .= $parts['host']; | ||
|
||
if (isset($parts['port'])) { | ||
$url .= ':' . $parts['port']; | ||
} | ||
|
||
if (isset($parts['path'])) { | ||
$url .= $parts['path']; | ||
} | ||
|
||
if (isset($parts['query'])) { | ||
$url .= '?' . $parts['query']; | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function getBaseUriParts() : array | ||
{ | ||
return [ | ||
'scheme' => $this->config['scheme'], | ||
'host' => $this->config['host'], | ||
'port' => $this->config['port'], | ||
'path' => $this->config['path'], | ||
'user' => $this->config['username'], | ||
'pass' => $this->config['password'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters