Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
177 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crunz\HttpClient; | ||
|
||
use Crunz\Logger\ConsoleLoggerInterface; | ||
|
||
final class FallbackHttpClient implements HttpClientInterface | ||
{ | ||
/** @var StreamHttpClient */ | ||
private $streamHttpClient; | ||
/** @var CurlHttpClient */ | ||
private $curlHttpClient; | ||
/** @var HttpClientInterface|null */ | ||
private $httpClient; | ||
/** @var ConsoleLoggerInterface */ | ||
private $consoleLogger; | ||
|
||
public function __construct( | ||
StreamHttpClient $streamHttpClient, | ||
CurlHttpClient $curlHttpClient, | ||
ConsoleLoggerInterface $consoleLogger | ||
) { | ||
$this->streamHttpClient = $streamHttpClient; | ||
$this->curlHttpClient = $curlHttpClient; | ||
$this->consoleLogger = $consoleLogger; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* | ||
* @throws HttpClientException | ||
*/ | ||
public function ping($url) | ||
{ | ||
$httpClient = $this->chooseHttpClient(); | ||
$httpClient->ping($url); | ||
} | ||
|
||
/** | ||
* @return HttpClientInterface | ||
* | ||
* @throws HttpClientException | ||
*/ | ||
private function chooseHttpClient() | ||
{ | ||
if (null !== $this->httpClient) { | ||
return $this->httpClient; | ||
} | ||
|
||
$this->consoleLogger | ||
->debug('Choosing HttpClient implementation.'); | ||
|
||
if (\function_exists('curl_exec')) { | ||
$this->httpClient = $this->curlHttpClient; | ||
|
||
$this->consoleLogger | ||
->debug('cURL available, use <info>CurlHttpClient</info>.'); | ||
|
||
return $this->httpClient; | ||
} | ||
|
||
if ('1' === \ini_get('allow_url_fopen')) { | ||
$this->httpClient = $this->streamHttpClient; | ||
|
||
$this->consoleLogger | ||
->debug("'allow_url_fopen' enabled, use <info>StreamHttpClient</info>"); | ||
|
||
return $this->httpClient; | ||
} | ||
|
||
$this->consoleLogger | ||
->debug('<error>Choosing HttpClient implementation failed.</error>'); | ||
|
||
throw new HttpClientException( | ||
"Unable to choose HttpClient. Enable cURL extension (preffered) or turn on 'allow_url_fopen' in php.ini." | ||
); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crunz\HttpClient; | ||
|
||
final class StreamHttpClient implements HttpClientInterface | ||
{ | ||
/** | ||
* @param string $url | ||
* | ||
* @throws HttpClientException | ||
*/ | ||
public function ping($url) | ||
{ | ||
$context = \stream_context_create( | ||
[ | ||
'http' => [ | ||
'user_agent' => 'Crunz StreamHttpClient', | ||
'timeout' => 5, | ||
], | ||
] | ||
); | ||
$resource = @\fopen( | ||
$url, | ||
'rb', | ||
false, | ||
$context | ||
); | ||
|
||
if (false === $resource) { | ||
$error = \error_get_last(); | ||
$errorMessage = $error['message'] ?? 'Unknown error'; | ||
|
||
throw new HttpClientException("Ping failed with message: \"{$errorMessage}\"."); | ||
} | ||
|
||
if ($resource) { | ||
\fclose($resource); | ||
} | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Crunz\Tests\Unit\HttpClient; | ||
|
||
use Crunz\HttpClient\HttpClientException; | ||
use Crunz\HttpClient\StreamHttpClient; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class StreamHttpClientTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function pingFailWithInvalidAddress() | ||
{ | ||
$this->expectException(HttpClientException::class); | ||
$this->expectExceptionMessage( | ||
'Ping failed with message: "fopen(http://www.wrong-address.tld): failed to open stream: php_network_getaddresses: getaddrinfo failed:' | ||
); | ||
|
||
$client = new StreamHttpClient(); | ||
$client->ping('http://www.wrong-address.tld'); | ||
} | ||
} |