Skip to content

Commit

Permalink
Бросаем исключение, если единичный трекинг вернул пустой массив событий
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaoda committed Oct 18, 2019
1 parent 07ecbd4 commit 93d1485
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ $this->app->singleton('appwilio.russianpost.logger', function () {

### Единичный доступ

#### Конфигурация
```php
$tracker = new SingleAccessClient($login, $password);
```

> Если инфрмации по ШПИ (трек-комеру) не найдено, то выбрасывается исключение
>`Appwilio\RussianPostSDK\Tracking\Exceptions\SingleAccessException` с соответствующим сообщением.
#### Получение данных по ШПИ (трек-комеру)
```php
$response = $tracker->getTrackingEvents('29014562148754');
Expand Down Expand Up @@ -160,7 +162,6 @@ foreach ($response as $event) {

### Пакетный доступ

#### Конфигурация
```php
$tracker = new PacketAccessClient($login, $password);
```
Expand Down
19 changes: 18 additions & 1 deletion src/Tracking/Exceptions/SingleAccessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,22 @@

class SingleAccessException extends \Exception
{
//
public const EMPTY_TRACKING_RESPONSE = 1;
public const EMPTY_COD_RESPONSE = 2;

public static function becauseEmptyTrackingResponse(string $trackingNumber): self
{
return new self(
"Почта России не смогла найти событий трекинга для РПО #{$trackingNumber}.",
self::EMPTY_TRACKING_RESPONSE
);
}

public static function becauseEmptyCODResponse(string $trackingNumber): self
{
return new self(
"Почта России не смогла найти событий обработки наложенного платежа для РПО #{$trackingNumber}.",
self::EMPTY_COD_RESPONSE
);
}
}
20 changes: 17 additions & 3 deletions src/Tracking/SingleAccessClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,34 @@ public function getTrackingEvents(
string $language = self::LANG_RUS,
int $type = self::HISTORY_OPERATIONS
): TrackingEventsResponse {
return $this->callSoapMethod(
/** @var TrackingEventsResponse $response */
$response = $this->callSoapMethod(
'getOperationHistory',
$this->assembleTrackingRequestArguments($track, $language, $type)
);

if (\count($response->getEvents()) === 0) {
throw SingleAccessException::becauseEmptyTrackingResponse($track);
}

return $response;
}

public function getCashOnDeliveryEvents(
string $track,
string $language = self::LANG_RUS
): CashOnDeliveryEventsResponse {
return $this->callSoapMethod(
/** @var CashOnDeliveryEventsResponse $response */
$response = $this->callSoapMethod(
'PostalOrderEventsForMail',
$this->assembleCashOnDeliveryRequestArguments($track, $language)
);

if (\count($response->getEvents()) === 0) {
throw SingleAccessException::becauseEmptyCODResponse($track);
}

return $response;
}

protected function getClient(): \SoapClient
Expand All @@ -116,7 +130,7 @@ private function callSoapMethod(string $method, \SoapVar $arguments)
return $this->getClient()->__soapCall($method, [$arguments]);
} catch (\SoapFault $e) {
if (\property_exists($e, 'detail')) {
$detail = \get_object_vars($e->{'detail'});
$detail = \get_object_vars($e->detail);

throw new SingleAccessException(\key($detail).': '.\current($detail), $e->getCode(), $e);
}
Expand Down

0 comments on commit 93d1485

Please sign in to comment.