Skip to content

Commit

Permalink
renamed some services and added additional conditions for carrier ret…
Browse files Browse the repository at this point in the history
…rieve
  • Loading branch information
mandan2 committed Sep 26, 2023
1 parent d51bc3c commit 036e783
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/Handler/Order/OrderCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ private function createRecurringOrderEntity(Order $order, string $method): void
'Failed to create recurring order',
[
'Exception message' => $exception->getMessage(),
'Exception code' => $exception->getCode(),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,61 @@

namespace Mollie\Subscription\Exception;

class CouldNotProvideCarrierDeliveryPrice extends MollieSubscriptionException
class CouldNotProvideSubscriptionCarrierDeliveryPrice extends MollieSubscriptionException
{
public static function failedToFindSelectedCarrierForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice
public static function failedToFindSelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find selected carrier for subscription order',
ExceptionCode::ORDER_FAILED_TO_FIND_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER
'Failed to find selected carrier',
ExceptionCode::ORDER_FAILED_TO_FIND_SELECTED_CARRIER
);
}

public static function failedToFindOrderCart(): CouldNotProvideCarrierDeliveryPrice
public static function failedToFindOrderCart(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order cart',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_CART
);
}

public static function failedToFindOrderCustomer(): CouldNotProvideCarrierDeliveryPrice
public static function failedToFindOrderCustomer(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order customer',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_CUSTOMER
);
}

public static function failedToApplySelectedCarrierForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice
public static function failedToApplySelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to apply selected carrier for subscription order',
ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER
'Failed to apply selected carrier',
ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER
);
}

public static function failedToFindOrderDeliveryAddress(): CouldNotProvideCarrierDeliveryPrice
public static function failedToFindOrderDeliveryAddress(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order delivery address',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS
);
}

public static function failedToFindOrderDeliveryCountry(): CouldNotProvideCarrierDeliveryPrice
public static function failedToFindOrderDeliveryCountry(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order delivery country',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY
);
}

public static function failedToGetSelectedCarrierPriceForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice
public static function failedToGetSelectedCarrierPrice(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to get selected carrier price for subscription order',
ExceptionCode::ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE_FOR_SUBSCRIPTION_ORDER
'Failed to get selected carrier price',
ExceptionCode::ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE
);
}
}
16 changes: 7 additions & 9 deletions subscription/Exception/ExceptionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ class ExceptionCode
{
//Order error codes starts from 1000

public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001;
public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002;
public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER = 1003;
public const ORDER_FAILED_TO_FIND_ORDER_CART = 1004;
public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1005;
public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER = 1006;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1007;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1008;
public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE_FOR_SUBSCRIPTION_ORDER = 1009;
public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1001;
public const ORDER_FAILED_TO_FIND_ORDER_CART = 1002;
public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1003;
public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1004;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1005;
public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1006;
public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1007;

//Cart error codes starts from 2000

Expand Down
18 changes: 9 additions & 9 deletions subscription/Factory/CreateSubscriptionDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Mollie\Repository\PaymentMethodRepositoryInterface;
use Mollie\Subscription\DTO\CreateSubscriptionData as SubscriptionDataDTO;
use Mollie\Subscription\DTO\Object\Amount;
use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice;
use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice;
use Mollie\Subscription\Exception\SubscriptionIntervalException;
use Mollie\Subscription\Provider\CarrierDeliveryPriceProvider;
use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider;
use Mollie\Subscription\Provider\SubscriptionDescriptionProvider;
use Mollie\Subscription\Provider\SubscriptionIntervalProvider;
use Mollie\Subscription\Repository\CombinationRepository;
Expand Down Expand Up @@ -43,8 +43,8 @@ class CreateSubscriptionDataFactory
private $module;
/** @var Context */
private $context;
/** @var CarrierDeliveryPriceProvider */
private $carrierDeliveryPriceProvider;
/** @var SubscriptionCarrierDeliveryPriceProvider */
private $subscriptionCarrierDeliveryPriceProvider;

public function __construct(
MolCustomerRepository $customerRepository,
Expand All @@ -55,7 +55,7 @@ public function __construct(
PaymentMethodRepositoryInterface $methodRepository,
Mollie $module,
Context $context,
CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider
SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider
) {
$this->customerRepository = $customerRepository;
$this->subscriptionInterval = $subscriptionInterval;
Expand All @@ -65,12 +65,12 @@ public function __construct(
$this->methodRepository = $methodRepository;
$this->module = $module;
$this->context = $context;
$this->carrierDeliveryPriceProvider = $carrierDeliveryPriceProvider;
$this->subscriptionCarrierDeliveryPriceProvider = $subscriptionCarrierDeliveryPriceProvider;
}

/**
* @throws \PrestaShopException
* @throws CouldNotProvideCarrierDeliveryPrice
* @throws CouldNotProvideSubscriptionCarrierDeliveryPrice
* @throws SubscriptionIntervalException
*/
public function build(Order $order, array $subscriptionProduct): SubscriptionDataDTO
Expand All @@ -87,13 +87,13 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat
$description = $this->subscriptionDescription->getSubscriptionDescription($order);

try {
$deliveryPrice = $this->carrierDeliveryPriceProvider->getPrice(
$deliveryPrice = $this->subscriptionCarrierDeliveryPriceProvider->getPrice(
(int) $order->id_address_delivery,
(int) $order->id_cart,
(int) $order->id_customer,
$subscriptionProduct
);
} catch (CouldNotProvideCarrierDeliveryPrice $exception) {
} catch (CouldNotProvideSubscriptionCarrierDeliveryPrice $exception) {
// TODO throw generic error when new logger will be implemented
throw $exception;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Mollie\Repository\CartRepositoryInterface;
use Mollie\Repository\CountryRepositoryInterface;
use Mollie\Repository\CustomerRepositoryInterface;
use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice;
use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice;

class CarrierDeliveryPriceProvider
class SubscriptionCarrierDeliveryPriceProvider
{
/** @var ConfigurationAdapter */
private $configuration;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct(
}

/**
* @throws CouldNotProvideCarrierDeliveryPrice
* @throws CouldNotProvideSubscriptionCarrierDeliveryPrice
*/
public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, array $subscriptionProduct): float
{
Expand All @@ -52,10 +52,12 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
/** @var \Carrier|null $carrier */
$carrier = $this->carrierRepository->findOneBy([
'id_carrier' => $subscriptionCarrierId,
'active' => 1,
'deleted' => 0,
]);

if (!$carrier) {
throw CouldNotProvideCarrierDeliveryPrice::failedToFindSelectedCarrierForSubscriptionOrder();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindSelectedCarrier();
}

/** @var \Cart|null $cart */
Expand All @@ -64,7 +66,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
]);

if (!$cart) {
throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCart();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderCart();
}

/** @var \Customer|null $customer */
Expand All @@ -73,7 +75,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
]);

if (!$customer) {
throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCustomer();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderCustomer();
}

$getAvailableOrderCarriers = $this->carrierRepository->getCarriersForOrder(
Expand All @@ -83,7 +85,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
);

if (!in_array($subscriptionCarrierId, array_column($getAvailableOrderCarriers, 'id_carrier'), false)) {
throw CouldNotProvideCarrierDeliveryPrice::failedToApplySelectedCarrierForSubscriptionOrder();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToApplySelectedCarrier();
}

/** @var \Address|bool $address */
Expand All @@ -92,7 +94,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
]);

if (!$address) {
throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryAddress();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderDeliveryAddress();
}

/** @var \Country|bool $country */
Expand All @@ -101,7 +103,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
]);

if (!$country) {
throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryCountry();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderDeliveryCountry();
}

/** @var float|bool $deliveryPrice */
Expand All @@ -114,7 +116,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a
);

if (is_bool($deliveryPrice) && !$deliveryPrice) {
throw CouldNotProvideCarrierDeliveryPrice::failedToGetSelectedCarrierPriceForSubscriptionOrder();
throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToGetSelectedCarrierPrice();
}

return (float) $deliveryPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Config\Config;
use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice;
use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice;
use Mollie\Subscription\Exception\ExceptionCode;
use Mollie\Subscription\Provider\CarrierDeliveryPriceProvider;
use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider;
use Mollie\Tests\Integration\BaseTestCase;
use Mollie\Tests\Integration\Factory\AddressFactory;
use Mollie\Tests\Integration\Factory\CarrierFactory;
use Mollie\Tests\Integration\Factory\CartFactory;
use Mollie\Tests\Integration\Factory\ProductFactory;

class CarrierDeliveryPriceProviderTest extends BaseTestCase
class SubscriptionCarrierDeliveryPriceProviderTest extends BaseTestCase
{
public function setUp(): void
{
Expand Down Expand Up @@ -67,10 +67,10 @@ public function testItSuccessfullyProvidesCarrierDeliveryPrice(): void
$cart->updateQty(2, $product1->id);
$cart->updateQty(3, $product2->id);

/** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */
$carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class);
/** @var SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider */
$subscriptionCarrierDeliveryPriceProvider = $this->getService(SubscriptionCarrierDeliveryPriceProvider::class);

$result = $carrierDeliveryPriceProvider->getPrice(
$result = $subscriptionCarrierDeliveryPriceProvider->getPrice(
$address->id,
$cart->id,
$cart->id_customer,
Expand Down Expand Up @@ -124,13 +124,13 @@ public function testItUnsuccessfullyProvidesCarrierDeliveryPriceCarrierIsOutOfZo
$cart->updateQty(2, $product1->id);
$cart->updateQty(3, $product2->id);

$this->expectException(CouldNotProvideCarrierDeliveryPrice::class);
$this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER);
$this->expectException(CouldNotProvideSubscriptionCarrierDeliveryPrice::class);
$this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER);

/** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */
$carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class);
/** @var SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider */
$subscriptionCarrierDeliveryPriceProvider = $this->getService(SubscriptionCarrierDeliveryPriceProvider::class);

$carrierDeliveryPriceProvider->getPrice(
$subscriptionCarrierDeliveryPriceProvider->getPrice(
$address->id,
$cart->id,
$cart->id_customer,
Expand Down

0 comments on commit 036e783

Please sign in to comment.