Skip to content

Commit

Permalink
Merge pull request #1 from kliker02/at/improvements
Browse files Browse the repository at this point in the history
At/improvements
  • Loading branch information
rustem-kaimolla authored Jul 4, 2022
2 parents c2d8dcc + 8427edd commit bf12db4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
},
"require-dev": {
"phpunit/phpunit": "^8.5"
},
"scripts": {
"test": "phpunit --colors=always tests"
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

$api = new KazPostAPI();

$my_track = $api->get('TRACK_CODE');
$my_track = $api->getEvents('CC016695190KZ');
echo "Статус посылки " .$my_track->status;
echo "Статус код посылки " .$my_track->status_code;
95 changes: 56 additions & 39 deletions src/KazPostAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use RustemKaimolla\KazPost\Exceptions\TrackCodeException;

/**
* @property mixed|null status_code
Expand All @@ -23,7 +24,7 @@ class KazPostAPI
*
* @var
*/
protected string $track_code;
protected string $track_code = '';

/**
* Хранит ответ сервера в виде массива
Expand All @@ -44,14 +45,14 @@ class KazPostAPI
*
* @var array
*/
protected array $last;
protected ?array $last = null;

/**
* Ряд и ячейка
*
* @var array
* @var array|null
*/
protected array $inbox;
protected ?array $inbox = null;

/**
* Экземпляр GuzzleHttp\Client
Expand All @@ -71,44 +72,60 @@ public function __construct()
* @param string $track_code трек код посылки
*
* @return $this
* @throws GuzzleException
*/
public function get(string $track_code):self
{
$this->track_code = $track_code;
try {
$response = $this->client->request('GET', $track_code . '/events');
$this->events = array();
$this->last = null;
$response = $this->client->request('GET', $track_code);

if($response->getStatusCode() === 200){
$this->data = json_decode($response->getBody(), true);
$this->events = $this->data['events'];
$this->last = $this->data['events'][0];
}
if ($response->getStatusCode() === 200) {
$this->data = json_decode($response->getBody(), true);
}
return $this;
}

} catch (GuzzleException $e) {
exit("Ошибка выполнения HTTP запроса");
/**
* Получает данные о посылке
*
* @return $this
* @throws GuzzleException
*/
public function getEvents():self
{
if (!strlen($this->track_code)) {
throw new TrackCodeException("Трек код не определён");
}
$response = $this->client->request('GET', $this->track_code . '/events');

if ($response->getStatusCode() === 200) {
$this->events = $response->getBody()['events'];
if (count($this->data['events'])) {
$this->last = $this->events[0];
}
}
return $this;
}

/**
* Получает ряд и ячейку посылки
*
* @return $this
*/

/**
* Получает ряд и ячейку посылки
*
* @return $this
* @throws GuzzleException
*/
public function inbox(): self
{
try {
$response = $this->client->request(
'GET',
'https://post.kz/mail-app/public/supermarket?barcode=' . $this->track_code . '&index=' . $this->last['activity'][0]['dep_code']);

if($response->getStatusCode() === 200){
$this->inbox = json_decode($response->getBody(), true);
}

} catch (GuzzleException $e) {
exit("Ошибка выполнения HTTP запроса");
}
if ($this->last === null) return $this;

$response = $this->client->request(
'GET',
'https://post.kz/mail-app/public/supermarket?barcode=' . $this->track_code . '&index=' . $this->last['activity'][0]['dep_code']);

if($response->getStatusCode() === 200){
$this->inbox = json_decode($response->getBody(), true);
}
return $this;
}

Expand Down Expand Up @@ -139,7 +156,7 @@ public function getData(): array
*
* @return array
*/
public function getInbox(): array
public function getInbox(): ?array
{
return $this->inbox;
}
Expand All @@ -153,13 +170,13 @@ public function events(): array
{
return $this->events;
}
/**
* Возвращает последнее местонахождение
*
* @return array
*/
public function getLast(): array

/**
* Возвращает последнее местонахождение
*
* @return array|null
*/
public function getLast(): ?array
{
return $this->last;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function testGetInbox(): void
{
$api = new KazPostAPI();
$track_code = "CC016695190KZ";

$this->assertIsArray($api->get($track_code)->inbox()->getInbox());

$inbox = $api->get($track_code)->inbox()->getInbox();
$this->assertTrue(is_array($inbox) || $inbox === null);
}
}
}

0 comments on commit bf12db4

Please sign in to comment.