From c8da4899d92b9484aa0bb2e343c431e58efb6a84 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 8 Jul 2019 13:24:03 +0300 Subject: [PATCH] first commit --- BaseClient.php | 66 +++++++++++++++++++++++++ Client.php | 121 ++++++++++++++++++++++++++++++++++++++++++++++ FsspException.php | 11 +++++ README.md | 99 +++++++++++++++++++++++++++++++++++++ composer.json | 26 ++++++++++ 5 files changed, 323 insertions(+) create mode 100644 BaseClient.php create mode 100644 Client.php create mode 100644 FsspException.php create mode 100644 README.md create mode 100644 composer.json diff --git a/BaseClient.php b/BaseClient.php new file mode 100644 index 0000000..fa5e2cb --- /dev/null +++ b/BaseClient.php @@ -0,0 +1,66 @@ +token === null) { + throw new InvalidConfigException('Token property must be set'); + } + } + + /** + * @return Client + */ + public function getClient() + { + if ($this->_client) { + return $this->_client; + } + + return new Client([ + 'baseUrl' => self::API_BASE_URL, + 'requestConfig' => [ + 'format' => Client::FORMAT_JSON + ], + 'responseConfig' => [ + 'format' => Client::FORMAT_JSON + ], + ]); + } + + /** + * @param $method + * @param array|null $data + * @param string $http_method + * @return mixed + * @throws InvalidConfigException + */ + public function call($method, array $data = null, $http_method = 'GET') + { + $response = $this->getClient()->createRequest() + ->setMethod($http_method) + ->setUrl($method) + ->setData(array_merge($data, ['token' => $this->token])) + ->send(); + + return $response->data; + } +} diff --git a/Client.php b/Client.php new file mode 100644 index 0000000..5c90ec8 --- /dev/null +++ b/Client.php @@ -0,0 +1,121 @@ +fetch('/search/physical', $data); + return $result; + } + + /** + * @param array $data + * @return mixed + * @throws ErrorException + * @throws FsspException + * @throws \yii\base\InvalidConfigException + */ + public function legal(array $data) + { + $result = $this->fetch('/search/legal', $data); + return $result; + } + + /** + * @param array $data + * @return mixed + * @throws ErrorException + * @throws FsspException + * @throws \yii\base\InvalidConfigException + */ + public function ip(array $data) + { + $result = $this->fetch('/search/ip', $data); + return $result; + } + + /** + * @param array $data + * @return mixed + * @throws ErrorException + * @throws FsspException + * @throws \yii\base\InvalidConfigException + */ + public function group(array $data) + { + $result = $this->fetch('/search/group', $data, true); + return $result; + } + + /** + * @param $method + * @param $data + * @return mixed + * @throws ErrorException + * @throws FsspException + * @throws \yii\base\InvalidConfigException + */ + public function fetch($method, $data, $isPost = false) + { + if ($isPost) { + $response = $this->call($method, $data, 'POST'); + } else { + $response = $this->call($method, $data); + } + + if (isset($response['status']) && $response['status'] == 'error') { + throw new ErrorException($response['exception']); + } + + if (!isset($response['response']['task'])) { + var_dump($response); + throw new ErrorException('Task id not received'); + } + $task = $response['response']['task']; + + $i = 0; + do { + $i++; + sleep($this->retryTime); + $resp_status = $this->call('/status', ['task' => $task]); + $status = $resp_status["response"]["status"]; + + if ($i >= $this->tryCount) { + throw new ErrorException('Timeout exceeded'); + } + } while ($status != 0); + + if ($resp_status['code'] != 0) { + throw new ErrorException('Too many time'); + } + + $response = $this->call('/result', ['task' => $task]); + + if (!isset($response["response"]["result"])) { + throw new FsspException('Assertion error, unexpected result'); + } + + if (count($response["response"]["result"]) == 1) { + $response["response"]["result"]["0"]["result"]; + } + + return $response["response"]["result"]; + } + + +} diff --git a/FsspException.php b/FsspException.php new file mode 100644 index 0000000..41f9ea2 --- /dev/null +++ b/FsspException.php @@ -0,0 +1,11 @@ +fssp->physical()** - запрос на поиск физического лица + +**Yii::$app->fssp->legal()** - запрос на поиск юридического лица + +**Yii::$app->fssp->ip()** - запрос на поиск по номеру исполнительного производства + +**Yii::$app->fssp->group()** - групповой запрос + +Получить подробную информацию можно здесь https://api-ip.fssprus.ru/swagger + +### Installation + +Add the package to your `composer.json`: + + { + "require": { + "ofilin/yii2-fssp": "^0.1" + } + } + +and run `composer update` or alternatively run `composer require ofilin/yii2-fssp:^0.1` + + +add to config file: +``` +'components' => [ + // ... + 'fssp' => [ + 'class' => ofilin\fssp\Client::class, + 'token' => '__YOUR_TOKEN__', // Get free token from https://api-ip.fssprus.ru/ + //'retryTime' => 1, // retry every 1 sec. + //'tryCount' => 20, // 20 counts + ], + // ... +], +``` +### Usage +``` +fssp->physical([ + 'firstname' => 'Иванов', + 'secondname' => 'Иван', + 'lastname' => 'Иванович', + 'birthdate' => '10.10.1980', + 'region' => 78, +]); + +// Search by company +$result = Yii::$app->fssp->legal([ + 'region' => 78, + 'name' => 'ООО Рога и Копыта' +]); + +// Search by number +$result = Yii::$app->fssp->ip([ + 'number' => "1234/12/12345-ИП", +]); + +// Group request returning many arrays!!! +$result = Yii::$app->fssp->group([ + 'request' => [ + [ + 'type' => 1, + 'params' => [ + 'firstname' => 'Иванов', + 'secondname' => 'Иван', + 'lastname' => 'Иванович', + 'birthdate' => '10.10.1980', + 'region' => 78, + ], + ], + [ + 'type' => 2, + 'params' => [ + 'name' => 'ООО Рога и Копыта' + 'address' => 'Ленинская', + 'region' => 78, + ], + ], + [ + 'type' => 3, + 'params' => [ + 'number' => "1234/12/12345-ИП", + ], + ], + ], +]); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..02e9737 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "ofilin/yii2-fssp", + "description": "FSSP component", + "type": "yii2-extension", + "keywords": [ + "yii2", + "fssp" + ], + "license": "MIT", + "minimum-stability": "dev", + "authors": [ + { + "name": "Oleg Filin", + "email": "ofilin86@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0", + "yiisoft/yii2-httpclient": "^2.0" + }, + "autoload": { + "psr-4": { + "ofilin\\fssp\\": "" + } + } +} \ No newline at end of file