Skip to content

Commit

Permalink
Merge pull request #65 from bobkorinek/master
Browse files Browse the repository at this point in the history
Added Config object
  • Loading branch information
geeshta authored Sep 12, 2024
2 parents fd171e3 + b84e224 commit 51b882f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 16 deletions.
20 changes: 6 additions & 14 deletions factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace GoPay;

function payments(array $userConfig, array $userServices = [])
function payments(array|Config $userConfig, array $userServices = [])
{
$config = $userConfig + [
'scope' => Definition\TokenScope::ALL,
'language' => Definition\Language::ENGLISH,
'timeout' => 30
];
$config = Config::parseUserConfig($userConfig);
$services = $userServices + [
'cache' => new Token\InMemoryTokenCache,
'logger' => new Http\Log\NullLogger
Expand All @@ -21,17 +17,13 @@ function payments(array $userConfig, array $userServices = [])

/**
* @deprecated Supercash payments are no longer supported
* @param array $userConfig
* @param array|Config $userConfig
* @param array $userServices
* @return PaymentsSupercash
*/
function paymentsSupercash(array $userConfig, array $userServices = [])
function paymentsSupercash(array|Config $userConfig, array $userServices = [])
{
$config = $userConfig + [
'scope' => Definition\TokenScope::ALL,
'language' => Definition\Language::ENGLISH,
'timeout' => 30
];
$config = Config::parseUserConfig($userConfig);
$services = $userServices + [
'cache' => new Token\InMemoryTokenCache,
'logger' => new Http\Log\NullLogger
Expand All @@ -45,7 +37,7 @@ function paymentsSupercash(array $userConfig, array $userServices = [])
/** Symfony container needs class for factory :( */
class Api
{
public static function payments(array $config, array $services = [])
public static function payments(array|Config $config, array $services = [])
{
return payments($config, $services);
}
Expand Down
110 changes: 110 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace GoPay;

use GoPay\Definition\Language;
use GoPay\Definition\TokenScope;

class Config
{
/**
* @param string|null $clientId {@see https://doc.gopay.com/#access-token}
* @param string|null $clientSecret {@see https://doc.gopay.com/#access-token}
* @param string|null $goid Default GoPay account used in `createPayment` if `target` is not specified.
* @param string|null $gatewayUrl {@see https://help.gopay.com/en/knowledge-base/integration-of-payment-gateway/integration-of-payment-gateway-1/how-do-i-integrate-the-payment-gateway}
*/
public function __construct(
public ?string $clientId = null,
public ?string $clientSecret = null,
public ?string $goid = null,
public ?string $gatewayUrl = null,
) {
}

/**
* {@see https://doc.gopay.com/#access-token}
*
* @var string
*/
public string $scope = TokenScope::ALL;

/**
* Language used in `createPayment` if `lang` is not specified + used for
* {@see https://doc.gopay.com/#errors localization of errors}.
*
* @var string
*/
public string $language = Language::ENGLISH;

/**
* Browser timeout in seconds.
*
* @var int
*/
public int $timeout = 30;

/**
* @param array|Config $userConfig
* @param bool $defaultArrayValues
* @return array
*/
public static function parseUserConfig(array|Config $userConfig, bool $defaultArrayValues = true): array
{
if (is_array($userConfig)) {
if ($defaultArrayValues) {
return $userConfig + [
'scope' => TokenScope::ALL,
'language' => Language::ENGLISH,
'timeout' => 30
];
}

return $userConfig;
} else {
return $userConfig->toArray();
}
}

/**
* @param array $arr
* @return Config
*/
public static function fromArray(array $arr): Config
{
$config = new Config();

$config->goid = $arr['goid'] ?? null;
$config->clientId = $arr['clientId'] ?? null;
$config->clientSecret = $arr['clientSecret'] ?? null;
$config->gatewayUrl = $arr['gatewayUrl'] ?? null;

// Properties with default values are set only if their value is provided
if (isset($arr['scope'])) {
$config->scope = $arr['scope'];
}
if (isset($arr['language'])) {
$config->language = $arr['language'];
}
if (isset($arr['timeout'])) {
$config->timeout = $arr['timeout'];
}

return $config;
}

/**
* @return array
*/
public function toArray(): array
{
return [
'goid' => $this->goid,
'clientId' => $this->clientId,
'clientSecret' => $this->clientSecret,
'gatewayUrl' => $this->gatewayUrl,
'scope' => $this->scope,
'language' => $this->language,
'timeout' => $this->timeout,
];
}
}
4 changes: 2 additions & 2 deletions src/GoPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class GoPay
private $config;
private $browser;

public function __construct(array $config, JsonBrowser $b)
public function __construct(array|Config $config, JsonBrowser $b)
{
$this->config = $config;
$this->config = Config::parseUserConfig($config, false);
$this->browser = $b;
}

Expand Down

0 comments on commit 51b882f

Please sign in to comment.