1 | <?php |
||
15 | trait HasSubscriptions |
||
16 | { |
||
17 | /** |
||
18 | * @param PlanContract|PlanIntervalContract $planOrInterval |
||
19 | * @return Model|SubscriptionContact |
||
20 | */ |
||
21 | public function subscribeTo($planOrInterval): SubscriptionContact |
||
22 | { |
||
23 | if ($planOrInterval instanceof PlanContract) { |
||
24 | return $this->subscribeToPlan($planOrInterval); |
||
25 | } |
||
26 | |||
27 | return $this->subscribeToInterval($planOrInterval); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param PlanContract $plan |
||
32 | * @return Model|SubscriptionContact |
||
33 | * @throws SubscriptionErrorException |
||
34 | */ |
||
35 | public function subscribeToPlan(PlanContract $plan): SubscriptionContact |
||
36 | { |
||
37 | if ($plan->hasManyIntervals()) { |
||
38 | throw new SubscriptionErrorException( |
||
39 | 'This plan has many intervals, please use subscribeToInterval() function' |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | $currentSubscription = $this->getActiveSubscription(); |
||
44 | $start_at = null; |
||
|
|||
45 | $end_at = null; |
||
46 | |||
47 | if ($currentSubscription == null) { |
||
48 | $start_at = now(); |
||
49 | } else { |
||
50 | $start_at = $currentSubscription->getExpirationDate(); |
||
51 | } |
||
52 | |||
53 | if ($plan->isFree()) { |
||
54 | $end_at = null; |
||
55 | } else { |
||
56 | $end_at = $this->calculateExpireDate($start_at, optional($plan->intervals())->first()); |
||
57 | } |
||
58 | |||
59 | $subscription = Subscription::make($plan, $start_at, $end_at); |
||
60 | $subscription = $this->subscriptions()->save($subscription); |
||
61 | |||
62 | return $subscription; |
||
63 | } |
||
64 | |||
65 | public function getActiveSubscription(): ?SubscriptionContact |
||
66 | { |
||
67 | return $this->subscriptions() |
||
68 | ->current() |
||
69 | ->first(); |
||
70 | } |
||
71 | |||
72 | public function subscriptions(): MorphMany |
||
76 | |||
77 | private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval) |
||
78 | { |
||
79 | $end_at = Carbon::createFromTimestamp($start_at->timestamp); |
||
98 | |||
99 | public function subscribeToInterval(PlanIntervalContract $interval): SubscriptionContact |
||
118 | |||
119 | public function changePlanTo(PlanContract $plan, PlanIntervalContract $interval = null) |
||
149 | |||
150 | public function hasActiveSubscription(): bool |
||
156 | |||
157 | protected function upgradeTo(PlanIntervalContract $interval): SubscriptionContact |
||
167 | |||
168 | public function forceUnsubscribe() |
||
169 | { |
||
170 | $currentSubscription = $this->getActiveSubscription(); |
||
171 | $currentSubscription->end_at = now()->subSecond(); |
||
172 | $currentSubscription->cancelled_at = now(); |
||
173 | $currentSubscription->save(); |
||
174 | } |
||
175 | |||
176 | protected function downgradeTo(PlanIntervalContract $interval): SubscriptionContact |
||
184 | |||
185 | public function renewSubscription(PlanIntervalContract $interval = null) |
||
208 | |||
209 | public function unsubscribe() |
||
210 | { |
||
211 | $currentSubscription = $this->getActiveSubscription(); |
||
212 | $currentSubscription->cancelled_at = now(); |
||
213 | $currentSubscription->save(); |
||
214 | } |
||
215 | |||
216 | public function getConsumables() |
||
219 | } |
||
220 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.