Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ging-dev committed Oct 17, 2024
1 parent b09fab3 commit 9c01109
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@

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

/**
* @psalm-type ValueType = string|int
* @psalm-type ParametersType = array<string, ValueType>
*/
final class BodyBuilder implements \Stringable, \JsonSerializable
final class RequestBodyBuilder implements \Stringable, \JsonSerializable
{
/**
* @param ParametersType $parameters
*/
public function __construct(
private function __construct(
private array $parameters = [],
) {
}

public function setSessionId(string $value): void
public static function new(): self
{
$this->parameters['sessionId'] = $value;
return new self();
}

/**
* @param ParametersType $parameters
*/
public function enhance(array $parameters): self
{
return new self($parameters);
}

/**
* @param ParametersType $parameters
*/
public function build(array $parameters = []): self
{
$data = array_merge([
'lang' => 'en',
'requestId' => Random::generate(12, '0-9A-Z').'|'.time(),
], $this->parameters, $parameters);
$data = array_merge($this->parameters, $parameters);
ksort($data);
$data['signature'] = md5(http_build_query($data));

Expand Down
2 changes: 1 addition & 1 deletion src/Builders/TransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use IPay\ValueObjects\Transaction;

/**
* @psalm-import-type ParametersType from BodyBuilder
* @psalm-import-type ParametersType from RequestBodyBuilder
*
* @implements \IteratorAggregate<int,Transaction>
*/
Expand Down
57 changes: 38 additions & 19 deletions src/IPayClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Http\Client\Common\PluginClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use IPay\Builders\BodyBuilder;
use IPay\Builders\RequestBodyBuilder;
use IPay\Builders\TransactionBuilder;
use IPay\Captcha\CaptchaSolver;
use IPay\Contracts\AbstractApi;
Expand All @@ -26,7 +26,7 @@
use Symfony\Component\VarExporter\LazyGhostTrait;

/**
* @phpstan-import-type ParametersType from BodyBuilder
* @phpstan-import-type ParametersType from RequestBodyBuilder
*/
final class IPayClient extends AbstractApi
{
Expand All @@ -37,9 +37,7 @@ final class IPayClient extends AbstractApi
*/
public static function fromCredentials(string $username, string $password): AbstractApi
{
return new self(
$username,
$password,
$client = (new self(
new HttpMethodsClient(
new PluginClient(Psr18ClientDiscovery::find(), [
new BaseUriPlugin(
Expand All @@ -52,30 +50,37 @@ public static function fromCredentials(string $username, string $password): Abst
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory(),
),
);
))->login($username, $password);

return $client;
}

/**
* @param ParametersType $authenticatedParameters
*/
private function __construct(
string $username,
string $password,
private HttpMethodsClientInterface $client,
private BodyBuilder $bodyBuilder = new BodyBuilder(),
private array $authenticatedParameters = [],
private ObjectMapper $objectMapper = new ObjectMapperUsingReflection(
new DefinitionProvider(
keyFormatter: new KeyFormatterWithoutConversion(),
),
),
) {
if ($authenticatedParameters) {
self::createLazyGhost(
initializer: $this->populateLazyProperties(...),
instance: $this,
);
}
}

private function login(string $userName, string $accessCode): self
{
/** @var array{sessionId: string, ...} */
$response = $this->post('signIn', [
'userName' => $username,
'accessCode' => $password,
] + $this->bypassCaptcha());
$bodyBuilder->setSessionId($response['sessionId']);
self::createLazyGhost(
initializer: $this->populateLazyProperties(...),
instance: $this,
);
$response = $this->post('signIn', get_defined_vars() + $this->bypassCaptcha());

return new self($this->client, ['sessionId' => $response['sessionId']]);
}

private function populateLazyProperties(): void
Expand Down Expand Up @@ -143,12 +148,26 @@ private function post(string $uri, array $parameters = []): array
$response = $this->client->post(
sprintf('ipay/wa/%s', $uri),
[],
$this->bodyBuilder->build($parameters)->encrypt(),
RequestBodyBuilder::new()
->enhance($this->getRequiredParameters())
->build($parameters)
->encrypt(),
);

return Json::decode((string) $response->getBody(), true);
}

/**
* @return ParametersType
*/
private function getRequiredParameters(): array
{
return array_merge([
'lang' => 'en',
'requestId' => Random::generate(12, '0-9A-Z').'|'.time(),
], $this->authenticatedParameters);
}

/**
* @return array{captchaId:string,captchaCode:string}
*/
Expand Down

0 comments on commit 9c01109

Please sign in to comment.