1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions\Traits; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval; |
7
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\Subscription; |
8
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract; |
9
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact; |
10
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract; |
11
|
|
|
use Sagitarius29\LaravelSubscriptions\Exceptions\SubscriptionErrorException; |
12
|
|
|
|
13
|
|
|
trait HasSubscriptions |
14
|
|
|
{ |
15
|
|
|
public function subscriptions() |
16
|
|
|
{ |
17
|
|
|
return $this->morphMany(config('subscriptions.entities.plan_subscription'), 'subscriber'); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param PlanContract|PlanIntervalContract $planOrInterval |
22
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
23
|
|
|
*/ |
24
|
|
|
public function subscribeTo($planOrInterval) |
25
|
|
|
{ |
26
|
|
|
if ($planOrInterval instanceof PlanContract) { |
27
|
|
|
return $this->subscribeToPlan($planOrInterval); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $this->subscribeToInterval($planOrInterval); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function subscribeToPlan(PlanContract $plan) |
34
|
|
|
{ |
35
|
|
|
if ($plan->hasManyIntervals()) { |
36
|
|
|
throw new SubscriptionErrorException( |
37
|
|
|
'This plan has many intervals, please use subscribeToInterval() function' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$currentSubscription = $this->getCurrentSubscription(); |
42
|
|
|
$start_at = null; |
|
|
|
|
43
|
|
|
$end_at = null; |
|
|
|
|
44
|
|
|
|
45
|
|
|
if ($currentSubscription == null) { |
46
|
|
|
$start_at = now(); |
47
|
|
|
} else { |
48
|
|
|
$start_at = $currentSubscription->getExpirationDate(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($plan->isFree()) { |
52
|
|
|
$end_at = null; |
53
|
|
|
} else { |
54
|
|
|
$end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$subscription = Subscription::make($plan, $start_at, $end_at); |
58
|
|
|
$subscription = $this->subscriptions()->save($subscription); |
59
|
|
|
|
60
|
|
|
return $subscription; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function subscribeToInterval(PlanIntervalContract $interval) |
64
|
|
|
{ |
65
|
|
|
$currentSubscription = $this->getCurrentSubscription(); |
66
|
|
|
$start_at = null; |
|
|
|
|
67
|
|
|
$end_at = null; |
|
|
|
|
68
|
|
|
|
69
|
|
|
if ($currentSubscription == null) { |
70
|
|
|
$start_at = now(); |
71
|
|
|
} else { |
72
|
|
|
$start_at = $currentSubscription->getExpirationDate(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$end_at = $this->calculateExpireDate($start_at, $interval); |
76
|
|
|
|
77
|
|
|
$subscription = Subscription::make($interval->plan, $start_at, $end_at); |
|
|
|
|
78
|
|
|
$subscription = $this->subscriptions()->save($subscription); |
79
|
|
|
|
80
|
|
|
return $subscription; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval) |
84
|
|
|
{ |
85
|
|
|
$end_at = Carbon::createFromTimestamp($start_at->timestamp); |
86
|
|
|
|
87
|
|
|
switch ($interval->getType()) { |
88
|
|
|
case PlanInterval::$DAY: |
89
|
|
|
return $end_at->days($interval->getUnit()); |
90
|
|
|
break; |
|
|
|
|
91
|
|
|
case PlanInterval::$MONTH: |
92
|
|
|
return $end_at->addMonths($interval->getUnit()); |
93
|
|
|
break; |
|
|
|
|
94
|
|
|
case PlanInterval::$YEAR: |
95
|
|
|
return $end_at->addYears($interval->getUnit()); |
96
|
|
|
break; |
|
|
|
|
97
|
|
|
default: |
98
|
|
|
//TODO error exception |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function changePlanTo(PlanContract $plan) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function renewSubscription(): bool |
108
|
|
|
{ |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function cancelSubscription(): bool |
112
|
|
|
{ |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCurrentSubscription(): ?SubscriptionContact |
116
|
|
|
{ |
117
|
|
|
return $this->subscriptions() |
118
|
|
|
->current(now()) |
119
|
|
|
->first(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getConsumables() |
123
|
|
|
{ |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.