Completed
Pull Request — master (#73)
by
unknown
13:42
created

HasSubscriptions::newSubscription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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->rinvex_subscriptions();
36
    }
37
38
    /**
39
     * The user may have many subscriptions.
40
     *
41
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
42
     */
43
    public function rinvex_subscriptions(): MorphMany
44
    {
45
        return $this->morphMany(config('rinvex.subscriptions.models.plan_subscription'), 'user');
46
    }
47
48
    /**
49
     * A model may have many active subscriptions.
50
     *
51
     * @return \Illuminate\Database\Eloquent\Collection
52
     */
53
    public function activeSubscriptions(): Collection
54
    {
55
        return $this->rinvex_subscriptions->reject->inactive();
0 ignored issues
show
Bug introduced by
The property rinvex_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...
56
    }
57
58
    /**
59
     * Get a subscription by slug.
60
     *
61
     * @param string $subscriptionSlug
62
     *
63
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
64
     */
65
    public function subscription(string $subscriptionSlug): ?PlanSubscription
66
    {
67
        return $this->rinvex_subscription($subscriptionSlug);
68
    }
69
70
    /**
71
     * Get a subscription by slug.
72
     *
73
     * @param string $subscriptionSlug
74
     *
75
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
76
     */
77
    public function rinvex_subscription(string $subscriptionSlug): ?PlanSubscription
78
    {
79
        return $this->rinvex_subscriptions()->where('slug', $subscriptionSlug)->first();
80
    }
81
82
    /**
83
     * Get subscribed plans.
84
     *
85
     * @return \Rinvex\Subscriptions\Models\PlanSubscription|null
86
     */
87
    public function subscribedPlans(): ?PlanSubscription
88
    {
89
        $planIds = $this->rinvex_subscriptions->reject->inactive()->pluck('plan_id')->unique();
90
91
        return app('rinvex.subscriptions.plan')->whereIn('id', $planIds)->get();
92
    }
93
94
    /**
95
     * Check if the user subscribed to the given plan.
96
     *
97
     * @param int $planId
98
     *
99
     * @return bool
100
     */
101
    public function subscribedTo($planId): bool
102
    {
103
        $subscription = $this->rinvex_subscriptions()->where('plan_id', $planId)->first();
104
105
        return $subscription && $subscription->active();
106
    }
107
108
    /**
109
     * Subscribe user to a new plan.
110
     *
111
     * @param string                            $subscription
112
     * @param \Rinvex\Subscriptions\Models\Plan $plan
113
     *
114
     * @return \Rinvex\Subscriptions\Models\PlanSubscription
115
     */
116
    public function newSubscription($subscription, Plan $plan): PlanSubscription
117
    {
118
        return $this->rinvex_newSubscription($subscription, $plan);
119
    }
120
121
    /**
122
     * Subscribe user to a new plan.
123
     *
124
     * @param string                            $subscription
125
     * @param \Rinvex\Subscriptions\Models\Plan $plan
126
     *
127
     * @return \Rinvex\Subscriptions\Models\PlanSubscription
128
     */
129
    public function rinvex_newSubscription($subscription, Plan $plan): PlanSubscription
130
    {
131
        $trial = new Period($plan->trial_interval, $plan->trial_period, now());
132
        $period = new Period($plan->invoice_interval, $plan->invoice_period, $trial->getEndDate());
133
134
        return $this->rinvex_subscriptions()->create([
135
            'name' => $subscription,
136
            'plan_id' => $plan->getKey(),
137
            'trial_ends_at' => $trial->getEndDate(),
138
            'starts_at' => $period->getStartDate(),
139
            'ends_at' => $period->getEndDate(),
140
        ]);
141
    }
142
}
143