Completed
Pull Request — master (#2)
by Adolfo
01:21
created

HasSubscriptions::getConsumables()   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 0
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
    /**
22
     * @param PlanContract|PlanIntervalContract $planOrInterval
23
     * @return \Illuminate\Database\Eloquent\Model
24
     */
25
    public function subscribeTo($planOrInterval)
26
    {
27
        if ($planOrInterval instanceof PlanContract) {
28
            return $this->subscribeToPlan($planOrInterval);
29
        }
30
31
        return $this->subscribeToInterval($planOrInterval);
32
    }
33
34
    public function subscribeToPlan(PlanContract $plan)
35
    {
36
        if ($plan->hasManyIntervals()) {
37
            throw new SubscriptionErrorException(
38
                'This plan has many intervals, please use subscribeToInterval() function'
39
            );
40
        }
41
42
        $currentSubscription = $this->getCurrentSubscription();
43
        $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...
44
        $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...
45
46
        if ($currentSubscription == null) {
47
            $start_at = now();
48
        } else {
49
            $start_at = $currentSubscription->getExpirationDate();
50
        }
51
52
        if ($plan->isFree()) {
53
            $end_at = null;
54
        } else {
55
            $end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first());
56
        }
57
58
        $subscription = Subscription::make($plan, $start_at, $end_at);
59
        $subscription = $this->subscriptions()->save($subscription);
60
61
        return $subscription;
62
    }
63
64
    public function subscribeToInterval(PlanIntervalContract $interval)
65
    {
66
        $currentSubscription = $this->getCurrentSubscription();
67
        $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...
68
        $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...
69
70
        if ($currentSubscription == null) {
71
            $start_at = now();
72
        } else {
73
            $start_at = $currentSubscription->getExpirationDate();
74
        }
75
76
        $end_at = $this->calculateExpireDate($start_at, $interval);
77
78
        $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...
79
        $subscription = $this->subscriptions()->save($subscription);
80
81
        return $subscription;
82
    }
83
84
    private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval)
85
    {
86
        $end_at = Carbon::createFromTimestamp($start_at->timestamp);
87
88
        switch ($interval->getType()) {
89
            case PlanInterval::$DAY:
90
                return $end_at->days($interval->getUnit());
91
                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...
92
            case PlanInterval::$MONTH:
93
                return $end_at->addMonths($interval->getUnit());
94
                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...
95
            case PlanInterval::$YEAR:
96
                return $end_at->addYears($interval->getUnit());
97
                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...
98
            default:
99
                //TODO error exception
100
                break;
101
        }
102
    }
103
104
    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...
105
    {
106
    }
107
108
    public function renewSubscription(): bool
109
    {
110
    }
111
112
    public function cancelSubscription(): bool
113
    {
114
    }
115
116
    public function getCurrentSubscription(): ?SubscriptionContact
117
    {
118
        return $this->subscriptions()
119
            ->current(now())
120
            ->first();
121
    }
122
123
    public function getConsumables()
124
    {
125
    }
126
}
127