Conditions | 17 |
Paths | 1280 |
Total Lines | 93 |
Code Lines | 63 |
Lines | 42 |
Ratio | 45.16 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace GeneaLabs\LaravelMixpanel\Http\Requests; |
||
81 | private function recordSubscription(array $transaction, $user, array $originalValues = []) |
||
82 | { |
||
83 | $planStatus = array_key_exists('status', $transaction) ? $transaction['status'] : null; |
||
84 | $planName = isset($transaction['plan']['name']) ? $transaction['plan']['name'] : null; |
||
85 | $planStart = array_key_exists('start', $transaction) ? $transaction['start'] : null; |
||
86 | $planAmount = isset($transaction['plan']['amount']) ? $transaction['plan']['amount'] : null; |
||
87 | $oldPlanName = isset($originalValues['plan']['name']) ? $originalValues['plan']['name'] : null; |
||
88 | $oldPlanAmount = isset($originalValues['plan']['amount']) ? $originalValues['plan']['amount'] : null; |
||
89 | |||
90 | if ($planStatus === 'canceled') { |
||
91 | $profileData = [ |
||
92 | 'Subscription' => 'None', |
||
93 | 'Churned' => (new Carbon($transaction['canceled_at']))->format('Y-m-d\Th:i:s'), |
||
94 | 'Plan When Churned' => $planName, |
||
95 | 'Paid Lifetime' => (new Carbon)->createFromTimestampUTC($planStart) |
||
96 | ->diffInDays((new Carbon(timestamp($transaction['ended_at']))) |
||
97 | ->timezone('UTC')) . ' days' |
||
98 | ]; |
||
99 | $trackingData = [ |
||
100 | ['Subscription', ['Status' => 'Canceled', 'Upgraded' => false]], |
||
101 | ['Churn! :-('], |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | if (count($originalValues)) { |
||
106 | if ($planAmount && $oldPlanAmount) { |
||
107 | if ($planAmount < $oldPlanAmount) { |
||
108 | $profileData = [ |
||
109 | 'Subscription' => $planName, |
||
110 | 'Churned' => (new Carbon($transaction['ended_at'])) |
||
111 | ->timezone('UTC') |
||
112 | ->format('Y-m-d\Th:i:s'), |
||
113 | 'Plan When Churned' => $oldPlanName, |
||
114 | ]; |
||
115 | $trackingData = [ |
||
116 | ['Subscription', [ |
||
117 | 'Upgraded' => false, |
||
118 | 'FromPlan' => $oldPlanName, |
||
119 | 'ToPlan' => $planName, |
||
120 | ]], |
||
121 | ['Churn! :-('], |
||
122 | ]; |
||
123 | } |
||
124 | |||
125 | View Code Duplication | if ($planAmount > $oldPlanAmount) { |
|
126 | $profileData = [ |
||
127 | 'Subscription' => $planName, |
||
128 | ]; |
||
129 | $trackingData = [ |
||
130 | ['Subscription', [ |
||
131 | 'Upgraded' => true, |
||
132 | 'FromPlan' => $oldPlanName, |
||
133 | 'ToPlan' => $planName, |
||
134 | ]], |
||
135 | ['Unchurn! :-)'], |
||
136 | ]; |
||
137 | } |
||
138 | } else { |
||
139 | View Code Duplication | if ($planStatus === 'trialing' && ! $oldPlanName) { |
|
140 | $profileData = [ |
||
141 | 'Subscription' => $planName, |
||
142 | ]; |
||
143 | $trackingData = [ |
||
144 | ['Subscription', [ |
||
145 | 'Upgraded' => true, |
||
146 | 'FromPlan' => 'Trial', |
||
147 | 'ToPlan' => $planName, |
||
148 | ]], |
||
149 | ['Unchurn! :-)'], |
||
150 | ]; |
||
151 | } |
||
152 | } |
||
153 | } else { |
||
154 | View Code Duplication | if ($planStatus === 'active') { |
|
155 | $profileData = [ |
||
156 | 'Subscription' => $planName, |
||
157 | ]; |
||
158 | $trackingData = [ |
||
159 | ['Subscription', ['Status' => 'Created']], |
||
160 | ]; |
||
161 | } |
||
162 | |||
163 | View Code Duplication | if ($planStatus === 'trialing') { |
|
164 | $profileData = [ |
||
165 | 'Subscription' => 'Trial', |
||
166 | ]; |
||
167 | $trackingData = [ |
||
168 | ['Subscription', ['Status' => 'Trial']], |
||
169 | ]; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | event(new MixpanelEvent($user, $trackingData, 0, $profileData)); |
||
174 | } |
||
195 |