PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Subscriptions / Actions / GenerateNextRenewalForSubscription.php

GenerateNextRenewalForSubscription.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Subscriptions/Actions/GenerateNextRenewalForSubscription.php

42 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Give\Subscriptions\Actions;
6
7 use DateInterval;
8 use DateTime;
9 use DateTimeInterface;
10 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
11 use Give\Framework\Support\Facades\DateTime\Temporal;
12 use Give\Subscriptions\ValueObjects\SubscriptionPeriod;
13
14 /**
15 * Generates the next renewal date for a subscription. A base date may be provided, otherwise it will default to the
16 * current date.
17 *
18 * @since 2.23.0
19 */
20 class GenerateNextRenewalForSubscription
21 {
22 public function __invoke(
23 SubscriptionPeriod $period,
24 int $frequency,
25 DateTimeInterface $baseDate = null
26 ): DateTimeInterface {
27 if ( $frequency < 1 ) {
28 throw new InvalidArgumentException('Frequency must be greater than 0');
29 }
30
31 // Calculate the quarter as times 3 months
32 if ($period->equals(SubscriptionPeriod::QUARTER())) {
33 $frequency *= 3;
34 $period = SubscriptionPeriod::MONTH();
35 }
36
37 $baseDate = $baseDate ?? new DateTime('now', wp_timezone());
38
39 return Temporal::withoutMicroseconds($baseDate->modify("$frequency {$period->getValue()}"));
40 }
41 }
42