Passed
Push — master ( 19a915...d9efc8 )
by Gabriel
14:56
created

CalculateNextCharge::determineStartDate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Actions\Subscriptions\Charges;
4
5
use ByTIC\Payments\Models\Subscriptions\Subscription;
6
use ByTIC\Payments\Subscriptions\Billing\BillingPeriod;
7
use Nip\Utility\Date;
8
9
/**
10
 * Class CalculateNextCharge
11
 * @package ByTIC\Payments\Actions\Subscriptions\Charges
12
 */
13
class CalculateNextCharge
14
{
15
    /**
16
     * @param Subscription $subscription
17
     */
18
    public static function for($subscription)
19
    {
20
        $count = $subscription->charge_count > 0 ? $subscription->charge_count : 1;
21
        $subscription->charge_at = static::nextBillingDate(
22
            $subscription->start_at,
0 ignored issues
show
Bug introduced by
It seems like $subscription->start_at can also be of type string; however, parameter $startDate of ByTIC\Payments\Actions\S...arge::nextBillingDate() does only seem to accept DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
            /** @scrutinizer ignore-type */ $subscription->start_at,
Loading history...
23
            $subscription->billing_period,
24
            $subscription->billing_interval * $count
25
        );
26
    }
27
28
    /**
29
     * @param \DateTime $startDate
30
     * @param $period
31
     * @param $interval
32
     */
33
    protected static function nextBillingDate(\DateTime $startDate, $period, $interval)
34
    {
35
        $startDate = Date::instance($startDate)->setHour(8)->setMinute(0);
36
37
        switch ($period) {
38
            case BillingPeriod::DAILY:
39
                return $startDate->addDays($interval);
40
41
            case BillingPeriod::WEEKLY:
42
                return $startDate->addWeeks($interval);
43
44
            case BillingPeriod::MONTHLY:
45
                return $startDate->addMonthsNoOverflow($interval);
46
47
            case BillingPeriod::YEARLY:
48
                return $startDate->addYears($interval);
49
        }
50
        throw new \InvalidArgumentException("Invalid period [{$period}] provided.");
51
    }
52
}