2021-10-18 18:24:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
2021-10-21 09:55:32 +09:00
|
|
|
namespace gullevek\AmazonIncentives\Config;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
|
|
|
|
class Config implements ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $endpoint = '';
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $access_key = '';
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $secret_key = '';
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $partner_id = '';
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $currency = '';
|
2021-10-18 18:24:38 +09:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
private $debug = false;
|
2021-10-18 18:24:38 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $key
|
|
|
|
|
* @param string|null $secret
|
|
|
|
|
* @param string|null $partner
|
|
|
|
|
* @param string|null $endpoint
|
|
|
|
|
* @param string|null $currency
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
?string $key,
|
|
|
|
|
?string $secret,
|
|
|
|
|
?string $partner,
|
|
|
|
|
?string $endpoint,
|
|
|
|
|
?string $currency,
|
|
|
|
|
?bool $debug,
|
|
|
|
|
) {
|
2021-10-21 14:57:29 +09:00
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setAccessKey(($key) ?: $this->parseEnv('AWS_GIFT_CARD_KEY'));
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setSecret(($secret) ?: $this->parseEnv('AWS_GIFT_CARD_SECRET'));
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setPartner(($partner) ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID'));
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setEndpoint(($endpoint) ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT'));
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setCurrency(($currency) ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY'));
|
|
|
|
|
/**
|
|
|
|
|
* @psalm-suppress InvalidScalarArgument
|
|
|
|
|
* @phpstan-ignore-next-line
|
|
|
|
|
*/
|
|
|
|
|
$this->setDebug(($debug) ?: $this->parseEnv('AWS_DEBUG'));
|
2021-10-19 09:39:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* string key to search, returns entry from _ENV
|
|
|
|
|
* if not matchin key, returns empty
|
|
|
|
|
*
|
|
|
|
|
* @param string $key To search in _ENV array
|
|
|
|
|
* @return string|bool Returns either string or true/false (DEBUG flag)
|
|
|
|
|
*/
|
|
|
|
|
private function parseEnv(string $key)
|
|
|
|
|
{
|
|
|
|
|
$return = '';
|
|
|
|
|
switch ($key) {
|
|
|
|
|
case 'AWS_DEBUG':
|
|
|
|
|
$return = !empty($_ENV['AWS_DEBUG']) ? true : false;
|
|
|
|
|
break;
|
|
|
|
|
case 'AWS_GIFT_CARD_KEY':
|
|
|
|
|
case 'AWS_GIFT_CARD_SECRET':
|
|
|
|
|
case 'AWS_GIFT_CARD_PARTNER_ID':
|
|
|
|
|
case 'AWS_GIFT_CARD_ENDPOINT':
|
|
|
|
|
case 'AWS_GIFT_CARD_CURRENCY':
|
|
|
|
|
$return = $_ENV[$key] ?? '';
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return $return;
|
2021-10-18 18:24:38 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 14:57:29 +09:00
|
|
|
* @return string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
public function getEndpoint(): string
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->endpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $endpoint
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setEndpoint(string $endpoint): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
// TODO: check valid endpoint + set region
|
2021-10-21 13:29:50 +09:00
|
|
|
$this->endpoint = (parse_url($endpoint, PHP_URL_HOST)) ?: '';
|
2021-10-18 18:24:38 +09:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 14:57:29 +09:00
|
|
|
* @return string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
public function getAccessKey(): string
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->access_key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setAccessKey(string $key): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
$this->access_key = $key;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 14:57:29 +09:00
|
|
|
* @return string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
public function getSecret(): string
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->secret_key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $secret
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setSecret(string $secret): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
$this->secret_key = $secret;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 14:57:29 +09:00
|
|
|
* @return string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
public function getCurrency(): string
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->currency;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $currency
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setCurrency(string $currency): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
// TODO: check currency valid + currenc valid for region
|
|
|
|
|
$this->currency = $currency;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 14:57:29 +09:00
|
|
|
* @return string
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 14:57:29 +09:00
|
|
|
public function getPartner(): string
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->partner_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $partner
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setPartner(string $partner): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
$this->partner_id = $partner;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-21 13:29:50 +09:00
|
|
|
* @return bool
|
2021-10-18 18:24:38 +09:00
|
|
|
*/
|
2021-10-21 13:29:50 +09:00
|
|
|
public function getDebug(): bool
|
2021-10-18 18:24:38 +09:00
|
|
|
{
|
|
|
|
|
return $this->debug;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $debug
|
|
|
|
|
* @return ConfigInterface
|
|
|
|
|
*/
|
|
|
|
|
public function setDebug(bool $debug): ConfigInterface
|
|
|
|
|
{
|
|
|
|
|
$this->debug = $debug;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// __END__
|