forked from mollie/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIPRES-373: start date added into subscription request
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mollie\Subscription\Provider; | ||
|
||
use Combination; | ||
use DateInterval; | ||
use DateTime; | ||
use DateTimeZone; | ||
use Mollie\Adapter\ConfigurationAdapter; | ||
use Mollie\Subscription\Config\Config; | ||
use Mollie\Subscription\DTO\Object\Interval; | ||
use Mollie\Subscription\Exception\SubscriptionIntervalException; | ||
use Mollie\Subscription\Utility\Clock; | ||
|
||
if (!defined('_PS_VERSION_')) { | ||
exit; | ||
} | ||
|
||
class SubscriptionStartDateProvider | ||
{ | ||
/** @var ConfigurationAdapter */ | ||
private $configuration; | ||
|
||
public function __construct(ConfigurationAdapter $configuration, Clock $clock) | ||
{ | ||
$this->configuration = $configuration; | ||
$this->clock = $clock; | ||
} | ||
|
||
/** | ||
* Returns subscription date time | ||
* | ||
* @throws SubscriptionIntervalException | ||
*/ | ||
public function getSubscriptionStartDate(Combination $combination): string | ||
{ | ||
$currentTime = new DateTime('now', new DateTimeZone('UTC')); | ||
|
||
foreach ($combination->getWsProductOptionValues() as $attribute) { | ||
switch ($attribute['id']) { | ||
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_DAILY): | ||
$interval = new DateInterval('P1D'); | ||
break; | ||
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_WEEKLY): | ||
$interval = new DateInterval('P7D'); | ||
break; | ||
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY): | ||
$interval = new DateInterval('P1M'); | ||
break; | ||
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_YEARLY): | ||
$interval = new DateInterval('P1Y'); | ||
break; | ||
default: | ||
throw new SubscriptionIntervalException(sprintf('No interval exists for this %s attribute', $combination->id)); | ||
} | ||
|
||
// Add the interval to the current time | ||
$currentTime->add($interval); | ||
return $currentTime->format('Y-m-d'); | ||
} | ||
|
||
throw new SubscriptionIntervalException(sprintf('No interval exists for this %s attribute', $combination->id)); | ||
} | ||
} |