From 93d1485fee5aae5a3e6a9b51f9c268fe74514dc5 Mon Sep 17 00:00:00 2001 From: JhaoDa Date: Fri, 18 Oct 2019 14:01:06 +0700 Subject: [PATCH] =?UTF-8?q?=D0=91=D1=80=D0=BE=D1=81=D0=B0=D0=B5=D0=BC=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5,?= =?UTF-8?q?=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B5=D0=B4=D0=B8=D0=BD=D0=B8?= =?UTF-8?q?=D1=87=D0=BD=D1=8B=D0=B9=20=D1=82=D1=80=D0=B5=D0=BA=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3=20=D0=B2=D0=B5=D1=80=D0=BD=D1=83=D0=BB=20=D0=BF=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B9=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- .../Exceptions/SingleAccessException.php | 19 +++++++++++++++++- src/Tracking/SingleAccessClient.php | 20 ++++++++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) 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); }