-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ae429b
commit 37de4fe
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace KianKamgar\MoadianPhp\Interfaces; | ||
|
||
interface ModelInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |