1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Subscriptions\Traits; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Rinvex\Subscriptions\Models\Plan; |
9
|
|
|
use Rinvex\Subscriptions\Services\Period; |
10
|
|
|
use Illuminate\Database\Eloquent\Collection; |
11
|
|
|
use Rinvex\Subscriptions\Models\PlanSubscription; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
13
|
|
|
|
14
|
|
|
trait HasSubscriptions |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Define a polymorphic one-to-many relationship. |
18
|
|
|
* |
19
|
|
|
* @param string $related |
20
|
|
|
* @param string $name |
21
|
|
|
* @param string $type |
|
|
|
|
22
|
|
|
* @param string $id |
|
|
|
|
23
|
|
|
* @param string $localKey |
|
|
|
|
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
26
|
|
|
*/ |
27
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The subscriber may have many subscriptions. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
33
|
|
|
*/ |
34
|
|
|
public function subscriptions(): MorphMany |
35
|
|
|
{ |
36
|
|
|
return $this->morphMany(config('rinvex.subscriptions.models.plan_subscription'), 'subscriber', 'subscriber_type', 'subscriber_id'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A model may have many active subscriptions. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
43
|
|
|
*/ |
44
|
|
|
public function activeSubscriptions(): Collection |
45
|
|
|
{ |
46
|
|
|
return $this->subscriptions->reject->inactive(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a subscription by slug. |
51
|
|
|
* |
52
|
|
|
* @param string $subscriptionSlug |
53
|
|
|
* |
54
|
|
|
* @return \Rinvex\Subscriptions\Models\PlanSubscription|null |
55
|
|
|
*/ |
56
|
|
|
public function subscription(string $subscriptionSlug): ?PlanSubscription |
57
|
|
|
{ |
58
|
|
|
return $this->subscriptions()->where('slug', $subscriptionSlug)->first(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get subscribed plans. |
63
|
|
|
* |
64
|
|
|
* @return \Rinvex\Subscriptions\Models\PlanSubscription|null |
65
|
|
|
*/ |
66
|
|
|
public function subscribedPlans(): ?PlanSubscription |
67
|
|
|
{ |
68
|
|
|
$planIds = $this->subscriptions->reject->inactive()->pluck('plan_id')->unique(); |
69
|
|
|
|
70
|
|
|
return app('rinvex.subscriptions.plan')->whereIn('id', $planIds)->get(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the subscriber subscribed to the given plan. |
75
|
|
|
* |
76
|
|
|
* @param int $planId |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function subscribedTo($planId): bool |
81
|
|
|
{ |
82
|
|
|
$subscription = $this->subscriptions()->where('plan_id', $planId)->first(); |
83
|
|
|
|
84
|
|
|
return $subscription && $subscription->active(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Subscribe subscriber to a new plan. |
89
|
|
|
* |
90
|
|
|
* @param string $subscription |
91
|
|
|
* @param \Rinvex\Subscriptions\Models\Plan $plan |
92
|
|
|
* @param \Carbon\Carbon|null $startDate |
93
|
|
|
* |
94
|
|
|
* @return \Rinvex\Subscriptions\Models\PlanSubscription |
95
|
|
|
*/ |
96
|
|
|
public function newSubscription($subscription, Plan $plan, Carbon $startDate = null): PlanSubscription |
97
|
|
|
{ |
98
|
|
|
$trial = new Period($plan->trial_interval, $plan->trial_period, $startDate ?? now()); |
99
|
|
|
$period = new Period($plan->invoice_interval, $plan->invoice_period, $trial->getEndDate()); |
100
|
|
|
|
101
|
|
|
return $this->subscriptions()->create([ |
102
|
|
|
'name' => $subscription, |
103
|
|
|
'plan_id' => $plan->getKey(), |
104
|
|
|
'trial_ends_at' => $trial->getEndDate(), |
105
|
|
|
'starts_at' => $period->getStartDate(), |
106
|
|
|
'ends_at' => $period->getEndDate(), |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.