Skip to content

Commit

Permalink
Add RandomChallenge
Browse files Browse the repository at this point in the history
  • Loading branch information
kiankamgar committed Apr 12, 2024
1 parent 7ae429b commit 37de4fe
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Consts/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace KianKamgar\MoadianPhp\Consts;

class Url
{
private const BASE_URL = 'https://tp.tax.gov.ir/requestsmanager/api/v2/';
protected const RANDOM_CHALLENGE_URL = self::BASE_URL . 'nonce';
}
8 changes: 8 additions & 0 deletions src/Interfaces/ModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace KianKamgar\MoadianPhp\Interfaces;

interface ModelInterface
{

}
31 changes: 31 additions & 0 deletions src/Models/RandomChallengeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace KianKamgar\MoadianPhp\Models;

use KianKamgar\MoadianPhp\Interfaces\ModelInterface;

class RandomChallengeResponse implements ModelInterface
{
private string $nonce;
private string $expDate;

public function getNonce(): string
{
return $this->nonce;
}

public function setNonce(string $nonce): void
{
$this->nonce = $nonce;
}

public function getExpDate(): string
{
return $this->expDate;
}

public function setExpDate(string $expDate): void
{
$this->expDate = $expDate;
}
}
27 changes: 27 additions & 0 deletions src/Services/RandomChallenge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace KianKamgar\MoadianPhp\Services;

use GuzzleHttp\Exception\GuzzleException;
use KianKamgar\MoadianPhp\Consts\Url;
use KianKamgar\MoadianPhp\Models\RandomChallengeResponse;

class RandomChallenge extends Url
{
private int $timeToLive = 30;

public function setTimeToLive(int $timeToLive): RandomChallenge
{
$this->timeToLive = $timeToLive;
return $this;
}

/**
* @throws GuzzleException
*/
public function request(): RandomChallengeResponse
{
return (new Request(self::RANDOM_CHALLENGE_URL, RandomChallengeResponse::class))
->get(['timeToLive' => $this->timeToLive]);
}
}
93 changes: 93 additions & 0 deletions src/Services/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace KianKamgar\MoadianPhp\Services;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use KianKamgar\MoadianPhp\Interfaces\ModelInterface;
use Psr\Http\Message\ResponseInterface;

class Request
{
private string $url;
private string $model;
private ?string $token = null;
private Client $client;
private ResponseInterface $response;

public function __construct(string $url, string $model)
{
$this->url = $url;
$this->model = $model;
$this->init();
}

/**
* @throws GuzzleException
*/
public function get(array $requestParams = []): ModelInterface
{
$this->response = $this->client->request('GET', $this->url, [
'query' => $requestParams,
'headers' => $this->getAuthorizationHeader()
]);

if (!$this->isOk()) {

return (new $this->model());
}

return $this->getDecodedResponseArray();
}

public function setToken(?string $token): Request
{
$this->token = $token;
return $this;
}

public function isOk(): bool
{
return !($this->response->getStatusCode() < 200 || $this->response->getStatusCode() >= 300);
}

public function getResponse(): ResponseInterface
{
return $this->response;
}

public function getDecodedResponseArray(): ModelInterface
{
$responseArray = json_decode($this->getResponse()->getBody()->getContents(), true);
$model = new $this->model();

foreach ($responseArray as $key => $value) {

$method = 'set' . ucfirst($key);

if (! method_exists($model, $method)) {

continue;
}

$model->$method($value);
}

return $model;
}

private function init(): void
{
$this->client = new Client();
}

private function getAuthorizationHeader(): array
{
if (empty($this->token)) {

return [];
}

return ['Authorization' => 'Brear ' . $this->token];
}
}

0 comments on commit 37de4fe

Please sign in to comment.