|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract; |
|
7
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval; |
|
8
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\Subscription; |
|
9
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract; |
|
10
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact; |
|
11
|
|
|
|
|
12
|
|
|
trait HasSubscriptions |
|
13
|
|
|
{ |
|
14
|
|
|
public function subscriptions() |
|
15
|
|
|
{ |
|
16
|
|
|
return $this->morphMany(config('subscriptions.entities.plan_subscription'), 'subscriber'); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function subscribeTo(PlanContract $plan) |
|
20
|
|
|
{ |
|
21
|
|
|
$currentSubscription = $this->getCurrentSubscription(); |
|
22
|
|
|
$start_at = null; |
|
|
|
|
|
|
23
|
|
|
$end_at = null; |
|
24
|
|
|
|
|
25
|
|
|
if ($currentSubscription == null) { |
|
26
|
|
|
$start_at = now(); |
|
27
|
|
|
} else { |
|
28
|
|
|
$start_at = $currentSubscription->getExpirationDate(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ($plan->isFree()) { |
|
32
|
|
|
$end_at = null; |
|
33
|
|
|
} else { |
|
34
|
|
|
if(!$plan->hasManyIntervals()) { |
|
35
|
|
|
$end_at = $this->calculateExpireDate($start_at, $plan->intervals()->first()); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$subscription = Subscription::make($plan, $start_at, $end_at); |
|
40
|
|
|
$subscription = $this->subscriptions()->save($subscription); |
|
41
|
|
|
|
|
42
|
|
|
return $subscription; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval) |
|
46
|
|
|
{ |
|
47
|
|
|
$end_at = Carbon::createFromTimestamp($start_at->timestamp); |
|
48
|
|
|
|
|
49
|
|
|
switch($interval->getType()) { |
|
50
|
|
|
case PlanInterval::$DAY: |
|
51
|
|
|
return $end_at->days($interval->getUnit()); |
|
52
|
|
|
break; |
|
|
|
|
|
|
53
|
|
|
case PlanInterval::$MONTH: |
|
54
|
|
|
return $end_at->addMonths($interval->getUnit()); |
|
55
|
|
|
break; |
|
|
|
|
|
|
56
|
|
|
case PlanInterval::$YEAR: |
|
57
|
|
|
return $end_at->addYears($interval->getUnit()); |
|
58
|
|
|
break; |
|
|
|
|
|
|
59
|
|
|
default: |
|
60
|
|
|
//TODO error exception |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function changePlanTo(PlanContract $plan) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function renewSubscription(): bool |
|
70
|
|
|
{ |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function cancelSubscription(): bool |
|
74
|
|
|
{ |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getCurrentSubscription(): ?SubscriptionContact |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->subscriptions() |
|
80
|
|
|
->current(now()) |
|
81
|
|
|
->first(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getConsumables() |
|
85
|
|
|
{ |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.