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

HasSubscriptions   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A subscriptions() 0 4 1
A subscribeTo() 0 19 3
A changePlanTo() 0 3 1
A renewSubscription() 0 3 1
A cancelSubscription() 0 3 1
A getCurrentSubscription() 0 6 1
A getConsumables() 0 3 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Traits;
4
5
use Sagitarius29\LaravelSubscriptions\Entities\Subscription;
6
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract;
7
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact;
8
9
trait HasSubscriptions
10
{
11
    public function subscriptions()
12
    {
13
        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...
14
    }
15
16
    public function subscribeTo(PlanContract $plan)
17
    {
18
        $currentSubscription = $this->getCurrentSubscription();
19
        $start_at = null;
20
        $end_at = null;
21
22
        if ($currentSubscription == null) {
23
            $start_at = now();
24
        }
25
26
        if ($plan->isFree()) {
27
            $end_at = null;
28
        }
29
30
        $subscription = Subscription::make($plan, $start_at, $end_at);
31
        $subscription = $this->subscriptions()->save($subscription);
32
33
        return $subscription;
34
    }
35
36
    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...
37
    {
38
    }
39
40
    public function renewSubscription(): bool
41
    {
42
    }
43
44
    public function cancelSubscription(): bool
45
    {
46
    }
47
48
    public function getCurrentSubscription(): ?SubscriptionContact
49
    {
50
        return $this->subscriptions()
51
            ->current(now())
52
            ->first();
53
    }
54
55
    public function getConsumables()
56
    {
57
    }
58
}
59