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

CalculateNextAttempt::nextAttempt()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 16
rs 9.6111
1
<?php
2
3
namespace ByTIC\Payments\Actions\Subscriptions\Charges;
4
5
use ByTIC\Payments\Models\Subscriptions\Subscription;
6
7
/**
8
 * Class CalculateNextAttempt
9
 * @package ByTIC\Payments\Actions\Subscriptions\Charges
10
 */
11
class CalculateNextAttempt
12
{
13
    /**
14
     * @param Subscription $subscription
15
     */
16
    public static function for($subscription)
17
    {
18
        $count = $subscription->charge_attempts > 0 ? $subscription->charge_attempts : 1;
19
        $subscription->charge_at = static::nextAttempt(
20
            $subscription->charge_at,
0 ignored issues
show
Bug introduced by
It seems like $subscription->charge_at can also be of type string; however, parameter $lastAttempt of ByTIC\Payments\Actions\S...tAttempt::nextAttempt() 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

20
            /** @scrutinizer ignore-type */ $subscription->charge_at,
Loading history...
21
            $count
22
        );
23
    }
24
25
    /**
26
     * @param \DateTime $startDate
27
     * @param $period
28
     * @param $interval
29
     */
30
    protected static function nextAttempt(\DateTime $lastAttempt, $tries)
31
    {
32
        switch ($tries) {
33
            case 1:
34
                return $lastAttempt->addHours(3);
0 ignored issues
show
Bug introduced by
The method addHours() does not exist on DateTime. It seems like you code against a sub-type of DateTime such as Carbon\Carbon. ( Ignorable by Annotation )

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

34
                return $lastAttempt->/** @scrutinizer ignore-call */ addHours(3);
Loading history...
35
36
            case 2:
37
                return $lastAttempt->addHours(12);
38
39
            case 3:
40
                return $lastAttempt->addDays(1);
0 ignored issues
show
Bug introduced by
The method addDays() does not exist on DateTime. It seems like you code against a sub-type of DateTime such as Carbon\Carbon. ( Ignorable by Annotation )

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

40
                return $lastAttempt->/** @scrutinizer ignore-call */ addDays(1);
Loading history...
41
42
            case 4:
43
                return $lastAttempt->addDays(2);
44
        }
45
        return $lastAttempt->addDays($tries - 2);
46
    }
47
}