diff --git a/README.md b/README.md index 4df4df9..1adeb68 100644 --- a/README.md +++ b/README.md @@ -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'); @@ -160,7 +162,6 @@ foreach ($response as $event) { ### Пакетный доступ -#### Конфигурация ```php $tracker = new PacketAccessClient($login, $password); ``` diff --git a/src/Tracking/Exceptions/SingleAccessException.php b/src/Tracking/Exceptions/SingleAccessException.php index a3f52d4..a2c9c70 100644 --- a/src/Tracking/Exceptions/SingleAccessException.php +++ b/src/Tracking/Exceptions/SingleAccessException.php @@ -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 + ); + } } diff --git a/src/Tracking/SingleAccessClient.php b/src/Tracking/SingleAccessClient.php index d51a30f..8e42086 100644 --- a/src/Tracking/SingleAccessClient.php +++ b/src/Tracking/SingleAccessClient.php @@ -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 @@ -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); }