forked from mollie/laravel-cashier-mollie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultIntervalGenerator.php
More file actions
53 lines (44 loc) · 1.35 KB
/
Copy pathDefaultIntervalGenerator.php
File metadata and controls
53 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Laravel\Cashier\Plan;
use Illuminate\Support\Str;
use Laravel\Cashier\Plan\Contracts\IntervalGeneratorContract;
use Laravel\Cashier\Subscription;
class DefaultIntervalGenerator extends BaseIntervalGenerator implements IntervalGeneratorContract
{
/**
* @var string
*/
protected $interval;
public function __construct(string $interval)
{
$this->interval = $interval;
$this->useCarbonThisDayOrLast();
}
/**
* @param \Laravel\Cashier\Subscription|null $subscription
* @return \Carbon\Carbon|\Carbon\Traits\Modifiers
*/
public function getEndOfNextSubscriptionCycle(?Subscription $subscription = null)
{
$cycle_ends_at = $subscription->cycle_ends_at ?? now();
$subscription_date = $this->startOfTheSubscription($subscription);
if ($this->isMonthly()) {
return $cycle_ends_at->addMonthsNoOverflow((int) filter_var($this->interval, FILTER_SANITIZE_NUMBER_INT))
->thisDayOrLastOfTheMonth($subscription_date);
}
return $cycle_ends_at->modify('+'.$this->interval);
}
/**
* @return string
*/
public function interval() {
return $this->interval;
}
/**
* @return bool
*/
protected function isMonthly()
{
return Str::contains($this->interval, 'month');
}
}