From 58573f60d3e5469a0f9876efeb41252e156de7fe Mon Sep 17 00:00:00 2001 From: ging-dev Date: Tue, 2 Jul 2024 23:47:17 +0700 Subject: [PATCH] wip --- src/Api/AbstractApi.php | 28 +++++++++++++------ src/Api/Auth.php | 2 +- src/Api/{FieldBuilder.php => BodyBuilder.php} | 19 +++---------- src/Api/Session.php | 14 +++++++--- src/Api/SessionInterface.php | 8 ++++++ 5 files changed, 43 insertions(+), 28 deletions(-) rename src/Api/{FieldBuilder.php => BodyBuilder.php} (62%) create mode 100644 src/Api/SessionInterface.php diff --git a/src/Api/AbstractApi.php b/src/Api/AbstractApi.php index 04ca40c..337d07c 100644 --- a/src/Api/AbstractApi.php +++ b/src/Api/AbstractApi.php @@ -8,13 +8,14 @@ use League\ObjectMapper\ObjectMapperUsingReflection; use League\ObjectMapper\ReflectionDefinitionProvider; use Nette\Utils\Json; +use Nette\Utils\Random; abstract class AbstractApi { private ObjectMapper $objectMapper; public function __construct( - protected IPayClient $client, + private IPayClient $client, ) { $this->objectMapper = new ObjectMapperUsingReflection( new ReflectionDefinitionProvider( @@ -33,21 +34,32 @@ protected function post(string $uri, array $data = []): array $response = $this->client->getClient()->post( $uri, [], - $this->configureFieldBuilder( - FieldBuilder::with($data) - )->encrypt() + BodyBuilder::from($data + $this->getExtraParams()) + ->build() + ->encrypt() ); return Json::decode((string) $response->getBody(), true); } - protected function configureFieldBuilder(FieldBuilder $builder): FieldBuilder + public function getObjectMapper(): ObjectMapper { - return $builder->withRequiredFields(); + return $this->objectMapper; } - public function getObjectMapper(): ObjectMapper + public function getClient(): IPayClient { - return $this->objectMapper; + return $this->client; + } + + /** + * @return array + */ + protected function getExtraParams(): array + { + return [ + 'lang' => 'en', + 'requestId' => Random::generate(12, '0-9A-Z').'|'.time(), + ]; } } diff --git a/src/Api/Auth.php b/src/Api/Auth.php index e6dbcde..1ba4d2c 100644 --- a/src/Api/Auth.php +++ b/src/Api/Auth.php @@ -12,6 +12,6 @@ public function login( ): Session { $result = $this->post('/signIn', get_defined_vars()); - return new Session($result['sessionId'], $this->client); + return new Session($result['sessionId'], $this->getClient()); } } diff --git a/src/Api/FieldBuilder.php b/src/Api/BodyBuilder.php similarity index 62% rename from src/Api/FieldBuilder.php rename to src/Api/BodyBuilder.php index f013b4c..18c861e 100644 --- a/src/Api/FieldBuilder.php +++ b/src/Api/BodyBuilder.php @@ -4,12 +4,11 @@ use IPay\Encryption\Encrypter; use Nette\Utils\Json; -use Nette\Utils\Random; /** * @extends \ArrayObject */ -final class FieldBuilder extends \ArrayObject implements \Stringable, \JsonSerializable +final class BodyBuilder extends \ArrayObject implements \Stringable, \JsonSerializable { /** * @param string[] $data @@ -22,17 +21,9 @@ private function __construct(array $data) /** * @param string[] $data */ - public static function with(array $data): static + public static function from(array $data): static { - return new static($data); - } - - public function withRequiredFields(): self - { - $this['lang'] = 'en'; - $this['requestId'] = Random::generate(12, '0-9A-Z').'|'.time(); - - return $this; + return new self($data); } public function build(): self @@ -46,9 +37,7 @@ public function build(): self public function encrypt(): string { - return new static([ - 'encrypted' => Encrypter::encrypt($this->build()), - ]); + return static::from(['encrypted' => Encrypter::encrypt($this)]); } public function __toString(): string diff --git a/src/Api/Session.php b/src/Api/Session.php index 9c67e43..8595899 100644 --- a/src/Api/Session.php +++ b/src/Api/Session.php @@ -5,7 +5,7 @@ use IPay\Entity\Customer; use IPay\IPayClient; -final class Session extends AbstractApi +final class Session extends AbstractApi implements SessionInterface { public function __construct( private string $id, @@ -22,10 +22,16 @@ public function customer(): Customer ); } - protected function configureFieldBuilder(FieldBuilder $builder): FieldBuilder + public function getId(): string { - $builder['sessionId'] = $this->id; + return $this->id; + } - return parent::configureFieldBuilder($builder); + protected function getExtraParams(): array + { + return [ + 'sessionId' => $this->getId(), + ...parent::getExtraParams(), + ]; } } diff --git a/src/Api/SessionInterface.php b/src/Api/SessionInterface.php new file mode 100644 index 0000000..fcd6949 --- /dev/null +++ b/src/Api/SessionInterface.php @@ -0,0 +1,8 @@ +