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 |
|
|
|
|
21
|
|
|
* @param string $id |
|
|
|
|
22
|
|
|
* @param string $localKey |
|
|
|
|
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(); |
|
|
|
|
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
|
|
|
|
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.