-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIPRES-349: Abstract recurring orders product creation (#851)
- Loading branch information
1 parent
2ee045a
commit ab56953
Showing
6 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
subscription/Action/CreateRecurringOrdersProductAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
subscription/Exception/CouldNotCreateRecurringOrdersProduct.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/Integration/Subscription/Action/CreateRecurringOrdersProductActionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |