Completed
Push — master ( 8ce3f9...e1543f )
by Adolfo
14s queued 11s
created

HasSubscriptions::subscribeToPlan()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 9.456
cc 4
nc 5
nop 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Traits;
4
5
use Carbon\Carbon;
6
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval;
7
use Sagitarius29\LaravelSubscriptions\Entities\Subscription;
8
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
9
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact;
10
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract;
11
use Sagitarius29\LaravelSubscriptions\Exceptions\SubscriptionErrorException;
12
13
trait HasSubscriptions
14
{
15
    public function subscriptions()
16
    {
17
        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...
18
    }
19
20
    /**
21
     * @param PlanContract|PlanIntervalContract $planOrInterval
22
     * @return \Illuminate\Database\Eloquent\Model
23
     */
24
    public function subscribeTo($planOrInterval)
25
    {
26
        if ($planOrInterval instanceof PlanContract) {
27
            return $this->subscribeToPlan($planOrInterval);
28
        }
29
30
        return $this->subscribeToInterval($planOrInterval);
31
    }
32
33
    public function subscribeToPlan(PlanContract $plan)
34
    {
35
        if ($plan->hasManyIntervals()) {
36
            throw new SubscriptionErrorException(
37
                'This plan has many intervals, please use subscribeToInterval() function'
38
            );
39
        }
40
41
        $currentSubscription = $this->getCurrentSubscription();
42
        $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...
43
        $end_at = null;
0 ignored issues
show
Unused Code introduced by
$end_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...
44
45
        if ($currentSubscription == null) {
46
            $start_at = now();
47
        } else {
48
            $start_at = $currentSubscription->getExpirationDate();
49
        }
50
51
        if ($plan->isFree()) {
52
            $end_at = null;
53
        } else {
54
            $end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first());
55
        }
56
57
        $subscription = Subscription::make($plan, $start_at, $end_at);
58
        $subscription = $this->subscriptions()->save($subscription);
59
60
        return $subscription;
61
    }
62
63
    public function subscribeToInterval(PlanIntervalContract $interval)
64
    {
65
        $currentSubscription = $this->getCurrentSubscription();
66
        $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...
67
        $end_at = null;
0 ignored issues
show
Unused Code introduced by
$end_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...
68
69
        if ($currentSubscription == null) {
70
            $start_at = now();
71
        } else {
72
            $start_at = $currentSubscription->getExpirationDate();
73
        }
74
75
        $end_at = $this->calculateExpireDate($start_at, $interval);
76
77
        $subscription = Subscription::make($interval->plan, $start_at, $end_at);
0 ignored issues
show
Bug introduced by
Accessing plan on the interface Sagitarius29\LaravelSubs...ts\PlanIntervalContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
        $subscription = $this->subscriptions()->save($subscription);
79
80
        return $subscription;
81
    }
82
83
    private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval)
84
    {
85
        $end_at = Carbon::createFromTimestamp($start_at->timestamp);
86
87
        switch ($interval->getType()) {
88
            case PlanInterval::$DAY:
89
                return $end_at->days($interval->getUnit());
90
                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...
91
            case PlanInterval::$MONTH:
92
                return $end_at->addMonths($interval->getUnit());
93
                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...
94
            case PlanInterval::$YEAR:
95
                return $end_at->addYears($interval->getUnit());
96
                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...
97
            default:
98
                //TODO error exception
99
                break;
100
        }
101
    }
102
103
    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...
104
    {
105
    }
106
107
    public function renewSubscription(): bool
108
    {
109
    }
110
111
    public function cancelSubscription(): bool
112
    {
113
    }
114
115
    public function getCurrentSubscription(): ?SubscriptionContact
116
    {
117
        return $this->subscriptions()
118
            ->current(now())
119
            ->first();
120
    }
121
122
    public function getConsumables()
123
    {
124
    }
125
}
126