Completed
Push — master ( dd2d89...e4f576 )
by Abdelrahman
01:21
created

HasSubscriptions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 3
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
morphMany() 0 1 ?
A subscriptions() 0 4 1
A activeSubscriptions() 0 4 1
A subscription() 0 4 1
A subscribedPlans() 0 6 1
A subscribedTo() 0 6 2
A newSubscription() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Subscriptions\Traits;
6
7
use Rinvex\Subscriptions\Models\Plan;
8
use Rinvex\Subscriptions\Services\Period;
9
use Illuminate\Database\Eloquent\Collection;
10
use Rinvex\Subscriptions\Models\PlanSubscription;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13
trait HasSubscriptions
14
{
15
    /**
16
     * Define a polymorphic one-to-many relationship.
17
     *
18
     * @param string $related
19
     * @param string $name
20
     * @param string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
21
     * @param string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
22
     * @param string $localKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $localKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     *
24
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
25
     */
26
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
27
28
    /**
29
     * The user may have many subscriptions.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
32
     */
33
    public function subscriptions(): MorphMany
34
    {
35
        return $this->morphMany(config('rinvex.subscriptions.models.plan_subscription'), 'user');
36
    }
37
38
    /**
39
     * A model may have many active subscriptions.
40
     *
41
     * @return \Illuminate\Database\Eloquent\Collection
42
     */
43
    public function activeSubscriptions(): Collection
44
    {
45
        return $this->subscriptions->reject->inactive();
0 ignored issues
show
Bug introduced by
The property subscriptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
    }
47
48
    /**
49
     * Get a subscription by slug.
50
     *
51
     * @param string $subscriptionSlug
52
     *
53
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
54
     */
55
    public function subscription(string $subscriptionSlug): ?PlanSubscription
56
    {
57
        return $this->subscriptions()->where('slug', $subscriptionSlug)->first();
58
    }
59
60
    /**
61
     * Get subscribed plans.
62
     *
63
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
64
     */
65
    public function subscribedPlans(): ?PlanSubscription
66
    {
67
        $planIds = $this->subscriptions->reject->inactive()->pluck('plan_id')->unique();
68
69
        return app('rinvex.subscriptions.plan')->whereIn('id', $planIds)->get();
70
    }
71
72
    /**
73
     * Check if the user subscribed to the given plan.
74
     *
75
     * @param int $planId
76
     *
77
     * @return bool
78
     */
79
    public function subscribedTo($planId): bool
80
    {
81
        $subscription = $this->subscriptions()->where('plan_id', $planId)->first();
82
83
        return $subscription && $subscription->active();
84
    }
85
86
    /**
87
     * Subscribe user to a new plan.
88
     *
89
     * @param string                            $subscription
90
     * @param \Rinvex\Subscriptions\Models\Plan $plan
91
     *
92
     * @return \Rinvex\Subscriptions\Models\PlanSubscription
93
     */
94
    public function newSubscription($subscription, Plan $plan): PlanSubscription
95
    {
96
        $trial = new Period($plan->trial_interval, $plan->trial_period, now());
97
        $period = new Period($plan->invoice_interval, $plan->invoice_period, $trial->getEndDate());
98
99
        return $this->subscriptions()->create([
100
            'name' => $subscription,
101
            'plan_id' => $plan->getKey(),
102
            'trial_ends_at' => $trial->getEndDate(),
103
            'starts_at' => $period->getStartDate(),
104
            'ends_at' => $period->getEndDate(),
105
        ]);
106
    }
107
}
108