diff --git a/lib/MissingParameterException.php b/lib/MissingParameterException.php new file mode 100644 index 0000000..3b28e9f --- /dev/null +++ b/lib/MissingParameterException.php @@ -0,0 +1,84 @@ + */ + private $parameters; + + /** @var string */ + private $details; + + /** @var ProviderInterface */ + private $provider; + + public function __construct(ProviderInterface $provider, array $missingParametersName, string $details) + { + $this->parameters = $missingParametersName; + $this->provider = $provider; + $this->details = $details; + parent::__construct($this->getParametersMessage().\PHP_EOL.$details); + } + + public static function throwIfMissing(ProviderInterface $provider, array $parameters, string $details): void + { + $missing = array_filter($parameters, function ($value) { + return null === $value || empty($value); + }); + if (0 === count($missing)) { + return; + } + + throw new static($provider, array_keys($missing), $details); + } + + public function getParametersMessage(): string + { + return sprintf( + 'The provider %s have required parameters.'.\PHP_EOL.'Missing parameters are: %s', + get_class($this->provider), + implode(', ', $this->parameters) + ); + } + + public function getParameters(): array + { + return $this->parameters; + } + + public function getDetails(): string + { + return $this->details; + } + + public function getProvider(): ProviderInterface + { + return $this->provider; + } +} diff --git a/lib/Provider/Amazon.php b/lib/Provider/Amazon.php index a737f1e..fa87821 100644 --- a/lib/Provider/Amazon.php +++ b/lib/Provider/Amazon.php @@ -32,6 +32,7 @@ use MacFJA\BookRetriever\Helper\ConfigurableInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareTrait; +use MacFJA\BookRetriever\MissingParameterException; use MacFJA\BookRetriever\ProviderInterface; // @phan-suppress-next-line PhanUnreferencedUseNormal use MacFJA\BookRetriever\SearchResult\SearchResultBuilder; @@ -180,6 +181,11 @@ protected function oneCriteria(string $field, $value): array */ private function doRequest(string $queryUrl): array { + MissingParameterException::throwIfMissing($this, [ + 'access_key' => $this->accessKey, + 'associated_tag' => $this->associateTag, + ], 'You need an account to use this provider: https://docs.aws.amazon.com/AWSECommerceService/latest/DG/becomingAssociate.html'); + $client = $this->getHttpClient(); $results = []; foreach (self::COUNTRY_DOMAINS as $domain) { diff --git a/lib/Provider/DigitEyes.php b/lib/Provider/DigitEyes.php index 01ef9c4..097e693 100644 --- a/lib/Provider/DigitEyes.php +++ b/lib/Provider/DigitEyes.php @@ -26,6 +26,7 @@ use MacFJA\BookRetriever\Helper\ConfigurableInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareTrait; +use MacFJA\BookRetriever\MissingParameterException; use MacFJA\BookRetriever\ProviderInterface; use MacFJA\BookRetriever\SearchResult\SearchResultBuilder; use function sprintf; @@ -94,6 +95,12 @@ public static function getLabel(): string public function searchIsbn(string $isbn): array { + MissingParameterException::throwIfMissing($this, [ + 'api_key' => $this->apiKey, + 'app_code' => $this->appCode, + 'language' => $this->language, + ], 'You need an account to use this provider: https://www.digit-eyes.com/cgi-bin/digiteyes.cgi?action=signup'); + $client = $this->getHttpClient(); $response = $client->sendRequest( $this->createHttpRequest('GET', sprintf( diff --git a/lib/Provider/Ebay.php b/lib/Provider/Ebay.php index d756afa..98dbcf3 100644 --- a/lib/Provider/Ebay.php +++ b/lib/Provider/Ebay.php @@ -27,6 +27,7 @@ use DTS\eBaySDK\Finding\Types\ProductId; use function in_array; use MacFJA\BookRetriever\Helper\ConfigurableInterface; +use MacFJA\BookRetriever\MissingParameterException; use MacFJA\BookRetriever\ProviderInterface; use MacFJA\BookRetriever\SearchResult\SearchResultBuilder; use function strtoupper; @@ -132,6 +133,12 @@ protected function oneCriteriaSearch(string $field, $value): array private function getResponse(string $field, string $value): BaseFindingServiceResponse { + MissingParameterException::throwIfMissing($this, [ + 'app_id' => $this->appId, + 'cert_id' => $this->certId, + 'dev_id' => $this->devId, + ], 'You need an account to use this provider: https://developer.ebay.com/signin'); + $service = new FindingService(['credentials' => [ 'devId' => $this->devId, 'appId' => $this->appId, diff --git a/lib/Provider/GoodReads.php b/lib/Provider/GoodReads.php index 82ae041..fdeaad7 100644 --- a/lib/Provider/GoodReads.php +++ b/lib/Provider/GoodReads.php @@ -22,6 +22,7 @@ use DateTime; use function is_array; use MacFJA\BookRetriever\Helper\ConfigurableInterface; +use MacFJA\BookRetriever\MissingParameterException; use MacFJA\BookRetriever\ProviderInterface; use MacFJA\BookRetriever\SearchResult\SearchResultBuilder; use function ob_clean; @@ -181,6 +182,10 @@ protected function getByISBN(string $isbn): array private function getResponse(string $field, string $value): array { + MissingParameterException::throwIfMissing($this, [ + 'api_key' => $this->apiKey, + ], 'You need an account to use this provider: https://www.goodreads.com/user/new'); + $goodReads = new \Nicat\GoodReads\GoodReads($this->apiKey); ob_start(); diff --git a/lib/Provider/LibraryThing.php b/lib/Provider/LibraryThing.php index 07f9548..b608b38 100644 --- a/lib/Provider/LibraryThing.php +++ b/lib/Provider/LibraryThing.php @@ -23,6 +23,7 @@ use MacFJA\BookRetriever\Helper\ConfigurableInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareInterface; use MacFJA\BookRetriever\Helper\HttpClientAwareTrait; +use MacFJA\BookRetriever\MissingParameterException; use MacFJA\BookRetriever\ProviderInterface; use MacFJA\BookRetriever\SearchResult\SearchResultBuilder; use function reset; @@ -77,6 +78,10 @@ public static function getLabel(): string public function searchIsbn(string $isbn): array { + MissingParameterException::throwIfMissing($this, [ + 'api_key' => $this->apiKey, + ], 'You need an account to use this provider: https://www.librarything.com/services/keys.php'); + $client = $this->getHttpClient(); $response = $client->sendRequest($this->createHttpRequest( 'GET',