Skip to content

Commit

Permalink
PIPRES-349: Abstract recurring orders product creation (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 authored and JevgenijVisockij committed May 14, 2024
1 parent 2ee045a commit ab56953
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 15 deletions.
59 changes: 59 additions & 0 deletions subscription/Action/CreateRecurringOrdersProductAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

namespace Mollie\Subscription\Action;

use Mollie\Subscription\DTO\CreateRecurringOrdersProductData;
use Mollie\Subscription\Exception\CouldNotCreateRecurringOrdersProduct;
use Mollie\Subscription\Exception\MollieSubscriptionException;
use Mollie\Subscription\Logger\LoggerInterface;

if (!defined('_PS_VERSION_')) {
exit;
}

class CreateRecurringOrdersProductAction
{
/** @var LoggerInterface */
private $logger;

public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}

/**
* @throws MollieSubscriptionException
*/
public function run(CreateRecurringOrdersProductData $data): \MolRecurringOrdersProduct
{
$this->logger->debug(sprintf('%s - Function called', __METHOD__));

try {
$recurringOrdersProduct = new \MolRecurringOrdersProduct();

$recurringOrdersProduct->id_product = $data->getProductId();
$recurringOrdersProduct->id_product_attribute = $data->getProductAttributeId();
$recurringOrdersProduct->quantity = $data->getProductQuantity();
$recurringOrdersProduct->unit_price = $data->getUnitPriceTaxExcl();

$recurringOrdersProduct->add();
} catch (\Throwable $exception) {
throw CouldNotCreaterecurringOrdersProduct::unknownError($exception);
}

$this->logger->debug(sprintf('%s - Function ended', __METHOD__));

return $recurringOrdersProduct;
}
}
87 changes: 87 additions & 0 deletions subscription/DTO/CreateRecurringOrdersProductData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

namespace Mollie\Subscription\DTO;

if (!defined('_PS_VERSION_')) {
exit;
}

class CreateRecurringOrdersProductData
{
/** @var int */
private $productId;
/** @var int */
private $productAttributeId;
/** @var int */
private $productQuantity;
/** @var float */
private $unitPriceTaxExcl;

private function __construct(
int $productId,
int $productAttributeId,
int $productQuantity,
float $unitPriceTaxExcl
) {
$this->productId = $productId;
$this->productAttributeId = $productAttributeId;
$this->productQuantity = $productQuantity;
$this->unitPriceTaxExcl = $unitPriceTaxExcl;
}

/**
* @return int
*/
public function getProductId(): int
{
return $this->productId;
}

/**
* @return int
*/
public function getProductAttributeId(): int
{
return $this->productAttributeId;
}

/**
* @return int
*/
public function getProductQuantity(): int
{
return $this->productQuantity;
}

/**
* @return float
*/
public function getUnitPriceTaxExcl(): float
{
return $this->unitPriceTaxExcl;
}

public static function create(
int $productId,
int $productAttributeId,
int $productQuantity,
float $unitPriceTaxExcl
): self {
return new self(
$productId,
$productAttributeId,
$productQuantity,
$unitPriceTaxExcl
);
}
}
2 changes: 1 addition & 1 deletion subscription/DTO/CreateSpecificPriceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CreateSpecificPriceData
/** @var int */
private $currencyId;

public function __construct(
private function __construct(
int $productId,
int $productAttributeId,
float $price,
Expand Down
21 changes: 21 additions & 0 deletions subscription/Exception/CouldNotCreateRecurringOrdersProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

namespace Mollie\Subscription\Exception;

if (!defined('_PS_VERSION_')) {
exit;
}

class CouldNotCreateRecurringOrdersProduct extends MollieSubscriptionException
{
}
35 changes: 21 additions & 14 deletions subscription/Handler/SubscriptionCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
namespace Mollie\Subscription\Handler;

use Mollie\Api\Resources\Subscription;
use Mollie\Subscription\Action\CreateRecurringOrdersProductAction;
use Mollie\Subscription\Api\SubscriptionApi;
use Mollie\Subscription\DTO\CreateRecurringOrdersProductData;
use Mollie\Subscription\Factory\CreateSubscriptionDataFactory;
use Mollie\Subscription\Utility\ClockInterface;
use Mollie\Subscription\Validator\SubscriptionProductValidator;
Expand All @@ -39,17 +41,21 @@ class SubscriptionCreationHandler
private $createSubscriptionDataFactory;
/** @var SubscriptionProductValidator */
private $subscriptionProductValidator;
/** @var CreateRecurringOrdersProductAction */
private $createRecurringOrdersProductAction;

public function __construct(
ClockInterface $clock,
SubscriptionApi $subscriptionApi,
CreateSubscriptionDataFactory $subscriptionDataFactory,
SubscriptionProductValidator $subscriptionProductValidator
SubscriptionProductValidator $subscriptionProductValidator,
CreateRecurringOrdersProductAction $createRecurringOrdersProductAction
) {
$this->clock = $clock;
$this->subscriptionApi = $subscriptionApi;
$this->createSubscriptionDataFactory = $subscriptionDataFactory;
$this->subscriptionProductValidator = $subscriptionProductValidator;
$this->createRecurringOrdersProductAction = $createRecurringOrdersProductAction;
}

/**
Expand All @@ -73,23 +79,24 @@ public function handle(Order $order, string $method): void
$subscriptionData = $this->createSubscriptionDataFactory->build($order, $subscriptionProduct);
$subscription = $this->subscriptionApi->subscribeOrder($subscriptionData);

$recurringOrdersProduct = $this->createRecurringOrdersProduct($subscriptionProduct);
try {
$recurringOrdersProduct = $this->createRecurringOrdersProductAction->run(
CreateRecurringOrdersProductData::create(
(int) $subscriptionProduct['id_product'],
(int) $subscriptionProduct['id_product_attribute'],
(int) $subscriptionProduct['product_quantity'],
(float) $subscriptionProduct['unit_price_tax_excl']
)
);
} catch (\Throwable $exception) {
// TODO throw different exception

throw $exception;
}

$this->createRecurringOrder($recurringOrdersProduct, $order, $subscription, $method);
}

private function createRecurringOrdersProduct(array $product): MolRecurringOrdersProduct
{
$recurringOrdersProduct = new MolRecurringOrdersProduct();
$recurringOrdersProduct->id_product = $product['id_product'];
$recurringOrdersProduct->id_product_attribute = $product['id_product_attribute'];
$recurringOrdersProduct->quantity = $product['product_quantity'];
$recurringOrdersProduct->unit_price = $product['unit_price_tax_excl'];
$recurringOrdersProduct->add();

return $recurringOrdersProduct;
}

private function createRecurringOrder(MolRecurringOrdersProduct $recurringOrdersProduct, Order $order, Subscription $subscription, string $method): void
{
$recurringOrder = new MolRecurringOrder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

namespace Mollie\Tests\Integration\Subscription\Action;

use Mollie\Subscription\Action\CreateRecurringOrdersProductAction;
use Mollie\Subscription\DTO\CreateRecurringOrdersProductData;
use Mollie\Tests\Integration\BaseTestCase;

class CreateRecurringOrdersProductActionTest extends BaseTestCase
{
public function testItSuccessfullyCreateDatabaseEntry(): void
{
$this->assertDatabaseHasNot(\MolRecurringOrdersProduct::class, [
'id_product' => 1,
'id_product_attribute' => 1,
'quantity' => 1,
'unit_price' => 19.99,
]);

/** @var CreateRecurringOrdersProductAction $createRecurringOrdersProductAction */
$createRecurringOrdersProductAction = $this->getService(CreateRecurringOrdersProductAction::class);

$result = $createRecurringOrdersProductAction->run(CreateRecurringOrdersProductData::create(
1,
1,
1,
19.99
));

$this->assertDatabaseHas(\MolRecurringOrdersProduct::class, [
'id_product' => 1,
'id_product_attribute' => 1,
'quantity' => 1,
'unit_price' => 19.99,
]);

$result->delete();
}
}

0 comments on commit ab56953

Please sign in to comment.