Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ging-dev committed Jul 2, 2024
1 parent 0ce0755 commit 58573f6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
28 changes: 20 additions & 8 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<string,string>
*/
protected function getExtraParams(): array
{
return [
'lang' => 'en',
'requestId' => Random::generate(12, '0-9A-Z').'|'.time(),
];
}
}
2 changes: 1 addition & 1 deletion src/Api/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
19 changes: 4 additions & 15 deletions src/Api/FieldBuilder.php → src/Api/BodyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use IPay\Encryption\Encrypter;
use Nette\Utils\Json;
use Nette\Utils\Random;

/**
* @extends \ArrayObject<string,string>
*/
final class FieldBuilder extends \ArrayObject implements \Stringable, \JsonSerializable
final class BodyBuilder extends \ArrayObject implements \Stringable, \JsonSerializable
{
/**
* @param string[] $data
Expand All @@ -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
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/Api/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
];
}
}
8 changes: 8 additions & 0 deletions src/Api/SessionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace IPay\Api;

interface SessionInterface
{
public function getId(): string;
}

0 comments on commit 58573f6

Please sign in to comment.