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 bb4a5af commit 07ecbd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of RussianPost SDK package.
*
* © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Exceptions;

final class CalculationException extends \DomainException
{
public const ZERO_TOTAL_RATE = 1;

public static function becauseZeroTotalRate(): self
{
return new self('Почта России вернула нулевой расчёт стоимости доставки.', self::ZERO_TOTAL_RATE);
}
}
10 changes: 9 additions & 1 deletion src/Dispatching/Endpoints/Services/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Requests\NormalizeFioRequest;
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Requests\NormalizePhoneRequest;
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Requests\CheckRecipientRequest;
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Exceptions\CalculationException;
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Services\Requests\NormalizeAddressRequest;

final class Services
Expand Down Expand Up @@ -90,7 +91,14 @@ public function normalizeAddress(NormalizeAddressRequest $request): iterable
*/
public function calculate(CalculationRequest $request): Calculation
{
return $this->client->post('/1.0/tariff', $request, Calculation::class);
/** @var Calculation $calculation */
$calculation = $this->client->post('/1.0/tariff', $request, Calculation::class);

if ($calculation->getTotal()->getRate() === 0) {
throw CalculationException::becauseZeroTotalRate();
}

return $calculation;
}

/**
Expand Down

0 comments on commit 07ecbd4

Please sign in to comment.