Add PHPstan and fix all checks for max level
This commit is contained in:
@@ -26,6 +26,9 @@ class AWS
|
||||
public const CANCEL_GIFT_CARD_SERVICE = 'CancelGiftCard';
|
||||
public const GET_AVAILABLE_FUNDS_SERVICE = 'GetAvailableFunds';
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
@@ -162,7 +165,7 @@ class AWS
|
||||
* @param string $authorization_value
|
||||
* @param string $date_time_string
|
||||
* @param string $service_target
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function buildHeaders(
|
||||
string $payload,
|
||||
@@ -323,7 +326,6 @@ class AWS
|
||||
*/
|
||||
public function getGiftCardPayload(float $amount, ?string $creation_id = null): string
|
||||
{
|
||||
$amount = trim($amount);
|
||||
$payload = [
|
||||
'creationRequestId' => $creation_id ?: uniqid($this->config->getPartner() . '_'),
|
||||
'partnerId' => $this->config->getPartner(),
|
||||
@@ -333,7 +335,7 @@ class AWS
|
||||
'amount' => (float)$amount
|
||||
]
|
||||
];
|
||||
return json_encode($payload);
|
||||
return (json_encode($payload)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -343,13 +345,12 @@ class AWS
|
||||
*/
|
||||
public function getCancelGiftCardPayload(string $creation_request_id, string $gift_card_id): string
|
||||
{
|
||||
$gift_card_response_id = trim($gift_card_id);
|
||||
$payload = [
|
||||
'creationRequestId' => $creation_request_id,
|
||||
'partnerId' => $this->config->getPartner(),
|
||||
'gcId' => $gift_card_response_id
|
||||
'gcId' => $gift_card_id
|
||||
];
|
||||
return json_encode($payload);
|
||||
return (json_encode($payload)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -360,7 +361,7 @@ class AWS
|
||||
$payload = [
|
||||
'partnerId' => $this->config->getPartner(),
|
||||
];
|
||||
return json_encode($payload);
|
||||
return (json_encode($payload)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -394,7 +395,7 @@ class AWS
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
* @return string
|
||||
*/
|
||||
public function getTimestamp()
|
||||
{
|
||||
@@ -413,7 +414,7 @@ class AWS
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|string
|
||||
* @return string
|
||||
*/
|
||||
public function getDateString()
|
||||
{
|
||||
|
||||
@@ -2,36 +2,7 @@
|
||||
|
||||
/*
|
||||
* Amazon Incentive Code
|
||||
*
|
||||
* # settings:
|
||||
* aws endpoint (also sets region)
|
||||
* aws key
|
||||
* aws secret
|
||||
* aws partner id
|
||||
* money type (set as default, override in call)
|
||||
* money value (set as default, override in call)
|
||||
*
|
||||
* # checks
|
||||
* endpoint + region must match
|
||||
*
|
||||
* # calls:
|
||||
* create gift card: CreateGiftCard
|
||||
* cancel gift card: CancelGiftCard
|
||||
*
|
||||
* activate gift card: ActivateGiftCard
|
||||
* deactivate gift card: DeactivateGiftCard
|
||||
*
|
||||
* gift card status: ActivationStatusCheck
|
||||
*
|
||||
* check available funds: GetAvailablefunds
|
||||
*
|
||||
* api server health check
|
||||
*
|
||||
* # sub classes
|
||||
* config reader/checker
|
||||
* API v4 encrypter
|
||||
* submitter/data getter
|
||||
* error handler/retry
|
||||
* Amazon Gift Code on Demand
|
||||
*/
|
||||
|
||||
namespace gullevek\AmazonIncentives;
|
||||
@@ -41,8 +12,11 @@ use gullevek\AmazonIncentives\Config\Config;
|
||||
use gullevek\AmazonIncentives\Exceptions\AmazonErrors;
|
||||
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||
|
||||
class AmazonIncentives
|
||||
final class AmazonIncentives
|
||||
{
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
@@ -112,7 +86,7 @@ class AmazonIncentives
|
||||
}
|
||||
|
||||
/**
|
||||
* AmazonGiftCode make own client.
|
||||
* AmazonIncentives make own client.
|
||||
*
|
||||
* @param string|null $key
|
||||
* @param string|null $secret
|
||||
@@ -120,7 +94,7 @@ class AmazonIncentives
|
||||
* @param string|null $endpoint
|
||||
* @param string|null $currency
|
||||
* @param bool|null $debug
|
||||
* @return AmazonGiftCode
|
||||
* @return AmazonIncentives
|
||||
*/
|
||||
public static function make(
|
||||
string $key = null,
|
||||
@@ -139,7 +113,7 @@ class AmazonIncentives
|
||||
* message (Amazon returned error message string)
|
||||
*
|
||||
* @param string $message Exception message json string
|
||||
* @return array Decoded with code, type, message fields
|
||||
* @return array<mixed> Decoded with code, type, message fields
|
||||
*/
|
||||
public static function decodeExceptionMessage(string $message): array
|
||||
{
|
||||
@@ -162,6 +136,9 @@ class AmazonIncentives
|
||||
// PUBLIC TEST METHODS
|
||||
// *********************************************************************
|
||||
|
||||
/**
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function checkMe(): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
@@ -13,15 +13,25 @@ class Client implements ClientInterface
|
||||
/**
|
||||
*
|
||||
* @param string $url The URL being requested, including domain and protocol
|
||||
* @param array $headers Headers to be used in the request
|
||||
* @param array|string $params Can be nested for arrays and hashes
|
||||
* @param array<mixed> $headers Headers to be used in the request
|
||||
* @param array<mixed>|string $params Can be nested for arrays and hashes
|
||||
*
|
||||
*
|
||||
* @return String
|
||||
* @return string
|
||||
*/
|
||||
public function request(string $url, array $headers, $params): string
|
||||
{
|
||||
$handle = curl_init($url);
|
||||
if ($handle === false) {
|
||||
// throw Error here with all codes
|
||||
throw AmazonErrors::getError(
|
||||
'FAILURE',
|
||||
'C001',
|
||||
'CurlInitError',
|
||||
'Failed to init curl with url: ' . $url,
|
||||
0
|
||||
);
|
||||
}
|
||||
curl_setopt($handle, CURLOPT_POST, true);
|
||||
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
|
||||
// curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
||||
@@ -39,7 +49,7 @@ class Client implements ClientInterface
|
||||
$err = curl_errno($handle);
|
||||
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
|
||||
// extract all the error codes from Amazon
|
||||
$result_ar = json_decode($result, true);
|
||||
$result_ar = json_decode((string)$result, true);
|
||||
// if message is 'Rate exceeded', set different error
|
||||
if (($result_ar['message'] ?? '') == 'Rate exceeded') {
|
||||
$error_status = 'RESEND';
|
||||
@@ -62,14 +72,14 @@ class Client implements ClientInterface
|
||||
$err
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
return (string)$result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $errno
|
||||
* @param int $errno
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
@@ -97,7 +107,7 @@ class Client implements ClientInterface
|
||||
// throw an error like in the normal reqeust, but set to CURL error
|
||||
throw AmazonErrors::getError(
|
||||
'FAILURE',
|
||||
'C001',
|
||||
'C002',
|
||||
'CurlError',
|
||||
$message,
|
||||
$errno
|
||||
|
||||
@@ -5,9 +5,10 @@ namespace gullevek\AmazonIncentives\Client;
|
||||
interface ClientInterface
|
||||
{
|
||||
/**
|
||||
* @param string $url The URL being requested, including domain and protocol
|
||||
* @param array $headers Headers to be used in the request
|
||||
* @param array|string $params Can be nested for arrays and hashes
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array<mixed> $headers Headers to be used in the request
|
||||
* @param array<mixed>|string $params Can be nested for arrays and hashes
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
|
||||
@@ -44,12 +44,12 @@ class Config implements ConfigInterface
|
||||
?string $currency,
|
||||
?bool $debug,
|
||||
) {
|
||||
$this->setAccessKey($key ?: $this->parseEnv('AWS_GIFT_CARD_KEY'));
|
||||
$this->setSecret($secret ?: $this->parseEnv('AWS_GIFT_CARD_SECRET'));
|
||||
$this->setPartner($partner ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID'));
|
||||
$this->setEndpoint($endpoint ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT'));
|
||||
$this->setCurrency($currency ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY'));
|
||||
$this->setDebug($debug ?: $this->parseEnv('AWS_DEBUG'));
|
||||
$this->setAccessKey(($key) ?: $this->parseEnv('AWS_GIFT_CARD_KEY')); /** @phpstan-ignore-line */
|
||||
$this->setSecret(($secret) ?: $this->parseEnv('AWS_GIFT_CARD_SECRET')); /** @phpstan-ignore-line */
|
||||
$this->setPartner(($partner) ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID')); /** @phpstan-ignore-line */
|
||||
$this->setEndpoint(($endpoint) ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT')); /** @phpstan-ignore-line */
|
||||
$this->setCurrency(($currency) ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY')); /** @phpstan-ignore-line */
|
||||
$this->setDebug(($debug) ?: $this->parseEnv('AWS_DEBUG')); /** @phpstan-ignore-line */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +94,7 @@ class Config implements ConfigInterface
|
||||
public function setEndpoint(string $endpoint): ConfigInterface
|
||||
{
|
||||
// TODO: check valid endpoint + set region
|
||||
$this->endpoint = parse_url($endpoint, PHP_URL_HOST);
|
||||
$this->endpoint = (parse_url($endpoint, PHP_URL_HOST)) ?: '';
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -177,9 +177,9 @@ class Config implements ConfigInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|null
|
||||
* @return bool
|
||||
*/
|
||||
public function getDebug(): ?bool
|
||||
public function getDebug(): bool
|
||||
{
|
||||
return $this->debug;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,17 @@ namespace gullevek\AmazonIncentives\Debug;
|
||||
|
||||
class AmazonDebug
|
||||
{
|
||||
/**
|
||||
* @var array<mixed>
|
||||
*/
|
||||
private static $log = [];
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private static $debug = false;
|
||||
/**
|
||||
* @var string|null;
|
||||
*/
|
||||
private static $id = null;
|
||||
|
||||
/**
|
||||
@@ -74,7 +83,7 @@ class AmazonDebug
|
||||
* Will be pushed as new array entry int log
|
||||
* Main key is the set Id for this run
|
||||
*
|
||||
* @param array $data Any array data to store in the log
|
||||
* @param array<mixed> $data Any array data to store in the log
|
||||
* @return void
|
||||
*/
|
||||
public static function writeLog(array $data): void
|
||||
@@ -91,7 +100,7 @@ class AmazonDebug
|
||||
*
|
||||
* @param string|null $id If set returns only this id logs
|
||||
* or empty array if not found
|
||||
* @return array Always array, empty if not data or not found
|
||||
* @return array<mixed> Always array, empty if not data or not found
|
||||
*/
|
||||
public static function getLog(?string $id = null): array
|
||||
{
|
||||
|
||||
@@ -5,14 +5,14 @@ namespace gullevek\AmazonIncentives\Exceptions;
|
||||
use RuntimeException;
|
||||
use gullevek\AmazonIncentives\Debug\AmazonDebug;
|
||||
|
||||
class AmazonErrors extends RuntimeException
|
||||
final class AmazonErrors extends RuntimeException
|
||||
{
|
||||
/**
|
||||
* @param string $error_status agcodResponse->status from Amazon
|
||||
* @param string $error_code errorCode from Amazon
|
||||
* @param string $error_type errorType from Amazon
|
||||
* @param string $message
|
||||
* @param string $_error_code
|
||||
* @param int $_error_code
|
||||
* @return AmazonErrors
|
||||
*/
|
||||
public static function getError(
|
||||
@@ -20,11 +20,11 @@ class AmazonErrors extends RuntimeException
|
||||
string $error_code,
|
||||
string $error_type,
|
||||
string $message,
|
||||
string $_error_code
|
||||
int $_error_code
|
||||
): self {
|
||||
// NOTE: if xdebug.show_exception_trace is set to 1 this will print ERRORS
|
||||
return new static(
|
||||
json_encode([
|
||||
(json_encode([
|
||||
'status' => $error_status,
|
||||
'code' => $error_code,
|
||||
'type' => $error_type,
|
||||
@@ -32,7 +32,7 @@ class AmazonErrors extends RuntimeException
|
||||
// atach log data if exists
|
||||
'log_id' => AmazonDebug::getId(),
|
||||
'log' => AmazonDebug::getLog(),
|
||||
]),
|
||||
])) ?: 'AmazonErrors: json encode problem: ' . $message,
|
||||
$_error_code
|
||||
);
|
||||
}
|
||||
|
||||
@@ -28,31 +28,26 @@ class CancelResponse
|
||||
/**
|
||||
* Amazon Gift Card Raw JSON
|
||||
*
|
||||
* @var string
|
||||
* @var array<mixed>
|
||||
*/
|
||||
protected $raw_json;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $log;
|
||||
|
||||
/**
|
||||
* Response constructor.
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
*/
|
||||
public function __construct(array $json_response)
|
||||
{
|
||||
$this->raw_json = $json_response;
|
||||
$this->log = AmazonDebug::getLog(AmazonDebug::getId());
|
||||
$this->parseJsonResponse($json_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function getLog(): array
|
||||
{
|
||||
return $this->log;
|
||||
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,11 +79,11 @@ class CancelResponse
|
||||
*/
|
||||
public function getRawJson(): string
|
||||
{
|
||||
return json_encode($this->raw_json);
|
||||
return (json_encode($this->raw_json)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
* @return CancelResponse
|
||||
*/
|
||||
public function parseJsonResponse(array $json_response): self
|
||||
|
||||
@@ -34,32 +34,27 @@ class CreateBalanceResponse
|
||||
/**
|
||||
* Amazon Gift Card Raw JSON
|
||||
*
|
||||
* @var string
|
||||
* @var array<mixed>
|
||||
*/
|
||||
protected $raw_json;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $log;
|
||||
|
||||
/**
|
||||
* Response constructor.
|
||||
*
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
*/
|
||||
public function __construct(array $json_response)
|
||||
{
|
||||
$this->raw_json = $json_response;
|
||||
$this->log = AmazonDebug::getLog(AmazonDebug::getId());
|
||||
$this->parseJsonResponse($json_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function getLog(): array
|
||||
{
|
||||
return $this->log;
|
||||
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,13 +94,13 @@ class CreateBalanceResponse
|
||||
*/
|
||||
public function getRawJson(): string
|
||||
{
|
||||
return json_encode($this->raw_json);
|
||||
return (json_encode($this->raw_json)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
* @return CreateBalanceResponse
|
||||
*/
|
||||
public function parseJsonResponse(array $json_response): self
|
||||
|
||||
@@ -62,31 +62,26 @@ class CreateResponse
|
||||
/**
|
||||
* Amazon Gift Card Raw JSON
|
||||
*
|
||||
* @var string
|
||||
* @var array<mixed>
|
||||
*/
|
||||
protected $raw_json;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $log;
|
||||
|
||||
/**
|
||||
* Response constructor.
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
*/
|
||||
public function __construct(array $json_response)
|
||||
{
|
||||
$this->raw_json = $json_response;
|
||||
$this->log = AmazonDebug::getLog(AmazonDebug::getId());
|
||||
$this->parseJsonResponse($json_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function getLog(): array
|
||||
{
|
||||
return $this->log;
|
||||
return AmazonDebug::getLog(AmazonDebug::getId());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,9 +109,9 @@ class CreateResponse
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return float
|
||||
*/
|
||||
public function getValue(): string
|
||||
public function getValue(): float
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
@@ -159,11 +154,11 @@ class CreateResponse
|
||||
*/
|
||||
public function getRawJson(): string
|
||||
{
|
||||
return json_encode($this->raw_json);
|
||||
return (json_encode($this->raw_json)) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $json_response
|
||||
* @param array<mixed> $json_response
|
||||
* @return CreateResponse
|
||||
*/
|
||||
public function parseJsonResponse(array $json_response): self
|
||||
|
||||
Reference in New Issue
Block a user