-
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: Restrict subscription product add to cart based on subscr…
…iption settings (#850) * PIPRES-349: Restrict subscription product add to cart based on subscription settings * subtracted validation to a single service
- Loading branch information
Showing
16 changed files
with
400 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
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
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
36 changes: 36 additions & 0 deletions
36
subscription/Exception/CouldNotValidateSubscriptionSettings.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,36 @@ | ||
<?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 CouldNotValidateSubscriptionSettings extends MollieSubscriptionException | ||
{ | ||
public static function subscriptionServiceDisabled(): self | ||
{ | ||
return new self( | ||
'Subscription service disabled.', | ||
ExceptionCode::CART_SUBSCRIPTION_SERVICE_DISABLED | ||
); | ||
} | ||
|
||
public static function subscriptionCarrierInvalid(): self | ||
{ | ||
return new self( | ||
'Subscription carrier invalid.', | ||
ExceptionCode::CART_SUBSCRIPTION_CARRIER_INVALID | ||
); | ||
} | ||
} |
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
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
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
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
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,73 @@ | ||
<?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\Validator; | ||
|
||
use Mollie\Adapter\ConfigurationAdapter; | ||
use Mollie\Config\Config; | ||
use Mollie\Repository\CarrierRepositoryInterface; | ||
use Mollie\Subscription\Exception\CouldNotValidateSubscriptionSettings; | ||
|
||
if (!defined('_PS_VERSION_')) { | ||
exit; | ||
} | ||
|
||
class SubscriptionSettingsValidator | ||
{ | ||
/** @var ConfigurationAdapter */ | ||
private $configuration; | ||
/** @var CarrierRepositoryInterface */ | ||
private $carrierRepository; | ||
|
||
public function __construct( | ||
ConfigurationAdapter $configuration, | ||
CarrierRepositoryInterface $carrierRepository | ||
) { | ||
$this->configuration = $configuration; | ||
$this->carrierRepository = $carrierRepository; | ||
} | ||
|
||
/** | ||
* @throws CouldNotValidateSubscriptionSettings | ||
*/ | ||
public function validate(): bool | ||
{ | ||
if (!$this->isSubscriptionActive()) { | ||
throw CouldNotValidateSubscriptionSettings::subscriptionServiceDisabled(); | ||
} | ||
|
||
if (!$this->isSubscriptionCarrierValid()) { | ||
throw CouldNotValidateSubscriptionSettings::subscriptionCarrierInvalid(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function isSubscriptionActive(): bool | ||
{ | ||
return (bool) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ENABLED); | ||
} | ||
|
||
private function isSubscriptionCarrierValid(): bool | ||
{ | ||
$carrierId = (int) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); | ||
|
||
/** @var \Carrier|null $carrier */ | ||
$carrier = $this->carrierRepository->findOneBy([ | ||
'id_carrier' => $carrierId, | ||
'active' => 1, | ||
'deleted' => 0, | ||
]); | ||
|
||
return (bool) $carrier; | ||
} | ||
} |
Oops, something went wrong.