Extract method to check if Guzzle is installed

This commit is contained in:
Andrea Marco Sartori 2023-02-25 20:38:35 +10:00
parent 308b15a5d8
commit 961b8a8278

View File

@ -2,7 +2,7 @@
namespace Cerbero\JsonParser\Concerns; namespace Cerbero\JsonParser\Concerns;
use Cerbero\JsonParser\Exceptions\SourceException; use Cerbero\JsonParser\Exceptions\GuzzleRequiredException;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
@ -18,15 +18,25 @@ trait GuzzleAware
* Abort if Guzzle is not loaded * Abort if Guzzle is not loaded
* *
* @return void * @return void
* @throws SourceException * @throws GuzzleRequiredException
*/ */
protected function requireGuzzle(): void protected function requireGuzzle(): void
{ {
if (!class_exists(Client::class)) { if (!$this->guzzleIsInstalled()) {
throw SourceException::requireGuzzle(); throw new GuzzleRequiredException();
} }
} }
/**
* Determine whether Guzzle is installed
*
* @return bool
*/
protected function guzzleIsInstalled(): bool
{
return class_exists(Client::class);
}
/** /**
* Retrieve the JSON response of the given URL * Retrieve the JSON response of the given URL
* *