Completed
Push — develop ( cd2304...f50f20 )
by Adolfo
03:04
created

HasSubscriptions::changePlanTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Traits;
4
5
use Carbon\Carbon;
6
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract;
7
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval;
8
use Sagitarius29\LaravelSubscriptions\Entities\Subscription;
9
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
10
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact;
11
12
trait HasSubscriptions
13
{
14
    public function subscriptions()
15
    {
16
        return $this->morphMany(config('subscriptions.entities.plan_subscription'), 'subscriber');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    public function subscribeTo(PlanContract $plan)
20
    {
21
        $currentSubscription = $this->getCurrentSubscription();
22
        $start_at = null;
0 ignored issues
show
Unused Code introduced by
$start_at is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
        $end_at = null;
24
25
        if ($currentSubscription == null) {
26
            $start_at = now();
27
        } else {
28
            $start_at = $currentSubscription->getExpirationDate();
29
        }
30
31
        if ($plan->isFree()) {
32
            $end_at = null;
33
        } else {
34
            if(!$plan->hasManyIntervals()) {
35
                $end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first());
36
            }
37
        }
38
39
        $subscription = Subscription::make($plan, $start_at, $end_at);
40
        $subscription = $this->subscriptions()->save($subscription);
41
42
        return $subscription;
43
    }
44
45
    private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval)
46
    {
47
        $end_at = Carbon::createFromTimestamp($start_at->timestamp);
48
49
        switch($interval->getType()) {
50
            case PlanInterval::$DAY:
51
                return $end_at->days($interval->getUnit());
52
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
53
            case PlanInterval::$MONTH:
54
                return $end_at->addMonths($interval->getUnit());
55
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56
            case PlanInterval::$YEAR:
57
                return $end_at->addYears($interval->getUnit());
58
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
59
            default:
60
                //TODO error exception
61
                break;
62
        }
63
    }
64
65
    public function changePlanTo(PlanContract $plan)
0 ignored issues
show
Unused Code introduced by
The parameter $plan is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
    }
68
69
    public function renewSubscription(): bool
70
    {
71
    }
72
73
    public function cancelSubscription(): bool
74
    {
75
    }
76
77
    public function getCurrentSubscription(): ?SubscriptionContact
78
    {
79
        return $this->subscriptions()
80
            ->current(now())
81
            ->first();
82
    }
83
84
    public function getConsumables()
85
    {
86
    }
87
}
88