Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception if required parameters are missing #6

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/MissingParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\BookRetriever;

use function array_filter;
use function array_keys;
use function count;
use function get_class;
use function implode;
use RuntimeException;
use function sprintf;

class MissingParameterException extends RuntimeException
{
/** @var array<string> */
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;
}
}
6 changes: 6 additions & 0 deletions lib/Provider/Amazon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions lib/Provider/DigitEyes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions lib/Provider/Ebay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/Provider/GoodReads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions lib/Provider/LibraryThing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down