Total Complexity | 44 |
Total Lines | 284 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MailChimpController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MailChimpController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class MailChimpController extends BaseMailChimpController |
||
20 | { |
||
21 | protected $mail_api_key; |
||
22 | protected $mailchimp; |
||
23 | protected $mailchimp_field_model; |
||
24 | protected $mailchimp_set; |
||
25 | protected $list_id; |
||
26 | protected $lists; |
||
27 | protected $relation; |
||
28 | |||
29 | public function __construct() |
||
54 | } |
||
55 | |||
56 | public function addSubscriber($email) |
||
57 | { |
||
58 | try { |
||
59 | $merge_fields = $this->field($email); |
||
60 | $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No |
||
61 | $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
||
62 | $result = $this->mailchimp->post("lists/$this->list_id/members", [ |
||
63 | 'status' => $this->mailchimp_set->subscribe_status, |
||
64 | 'email_address' => $email, |
||
65 | 'merge_fields' => $merge_fields, |
||
66 | |||
67 | 'interests' => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false], |
||
68 | |||
69 | ]); |
||
70 | |||
71 | return $result; |
||
72 | } catch (Exception $ex) { |
||
73 | $exe = json_decode($ex->getMessage(), true); |
||
74 | if ($exe['status'] == 400) { |
||
75 | throw new Exception("$email is already subscribed to newsletter", 400); |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | //Update to Mailchimp For Paid Product |
||
81 | public function addSubscriberByClientPanel(Request $request) |
||
105 | } |
||
106 | } |
||
107 | |||
108 | public function field($email) |
||
109 | { |
||
110 | try { |
||
111 | $user = new User(); |
||
112 | $setting = new Setting(); |
||
113 | $user = $user->where('email', $email)->first(); |
||
114 | $country = Country::where('country_code_char2', $user->country)->pluck('nicename')->first(); |
||
115 | if ($user) { |
||
116 | $fields = ['first_name', 'last_name', 'company', 'mobile', |
||
117 | 'address', 'town', 'country', 'state', 'zip', 'active', 'role', 'source', ]; |
||
118 | $relation = $this->relation; |
||
119 | $merge_fields = []; |
||
120 | foreach ($fields as $field) { |
||
121 | if ($relation->$field) { |
||
122 | $merge_fields[$relation->$field] = $user->$field; |
||
123 | } |
||
124 | } |
||
125 | $merge_fields[$relation->source] = $setting->findorFail(1)->title; |
||
126 | |||
127 | return $merge_fields; |
||
128 | } else { |
||
129 | return redirect()->back()->with('fails', 'user not found'); |
||
130 | } |
||
131 | } catch (Exception $ex) { |
||
132 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public function addFieldsToAgora() |
||
166 | } |
||
167 | } |
||
168 | |||
169 | |||
170 | |||
171 | |||
172 | public function mapField() |
||
173 | { |
||
174 | try { |
||
175 | $model = $this->relation; |
||
176 | $model2 = $this->groupRelation; |
||
177 | $this->addFieldsToAgora(); |
||
178 | |||
179 | $mailchimp_fields = $this->mailchimp_field_model |
||
180 | ->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray(); |
||
181 | $agoraProducts = Product::pluck('name', 'id')->toArray(); |
||
182 | |||
183 | $mailchimpProducts = $this->mailchimp->get("lists/$this->list_id/interest-categories"); |
||
184 | $selectedProducts = MailchimpGroupAgoraRelation::select('agora_product_id', 'mailchimp_group_cat_id')->orderBy('id', 'asc')->get()->toArray(); |
||
|
|||
185 | $allGroups = $this->mailchimp->get("lists/$this->list_id/interest-categories"); //Get all the groups(interest-categories for a list) |
||
186 | foreach ($allGroups['categories'] as $key => $value) { |
||
187 | $display[] = (['id'=> $value->id, 'title'=>$value->title]); |
||
188 | } |
||
189 | |||
190 | $this->addProductInterestFieldsToAgora(); //add all the fields in Product Section of Groups to the db |
||
191 | $group_fields = $this->groups->where('list_id', $this->list_id) |
||
192 | ->select('category_name', 'category_option_id', 'category_id')->get()->toArray(); |
||
193 | // dd($group_fields[0]); |
||
194 | $relations = MailchimpGroupAgoraRelation::where('id', '!=', 0) |
||
195 | ->select('agora_product_id', 'mailchimp_group_cat_id') |
||
196 | ->orderBy('id', 'asc')->get()->toArray(); |
||
197 | $productList = []; |
||
198 | $categoryList = []; |
||
199 | if (count($relations) != 0) { |
||
200 | foreach ($relations as $key => $value) { |
||
201 | $categoryList[] = $this->groups->where('category_option_id', $value['mailchimp_group_cat_id'])->pluck('category_name')->first(); |
||
202 | $productList[] = Product::where('id', $value['agora_product_id'])->pluck('name')->first(); |
||
203 | } |
||
204 | } |
||
205 | $isPaidYesId = MailchimpFieldAgoraRelation::first()->pluck('is_paid_yes')->toArray(); |
||
206 | $selectedIsPaid[] = $isPaidYesId ? MailchimpGroup::where('category_option_id', $isPaidYesId)->pluck('category_id')->first() : ''; |
||
207 | $status = StatusSetting::select('mailchimp_product_status', 'mailchimp_ispaid_status')->first(); |
||
208 | |||
209 | return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model2', 'model', 'agoraProducts', 'display', 'selectedProducts', 'relations', 'group_fields', 'categoryList', 'productList', 'status', 'selectedIsPaid')); |
||
210 | } catch (Exception $ex) { |
||
211 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | public function addInterestFieldsToAgora(Request $request, $value) |
||
216 | { |
||
217 | $groupInterests = $this->mailchimp->get("lists/$this->list_id/interest-categories/$value/interests?count=20"); |
||
218 | echo '<option value="">Choose a Category</option>'; |
||
219 | if (count($groupInterests) > 0) { |
||
220 | foreach ($groupInterests['interests'] as $key=>$value) { |
||
221 | $fields[] = (['category_id' => $value->category_id, |
||
222 | 'list_id' => $value->list_id, |
||
223 | 'category_option_id' => $value->id, |
||
224 | 'category_option_name' => $value->name, |
||
225 | ]); |
||
226 | } |
||
227 | foreach ($fields as $field) { |
||
228 | $selectedCategory = MailchimpGroupAgoraRelation::where('mailchimp_group_cat_id', $field['category_option_id'])->pluck('mailchimp_group_cat_id')->first(); |
||
229 | echo '<option value='.$field['category_option_id'].'>'.$field['category_option_name'].'</option>'; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | public function addProductInterestFieldsToAgora() |
||
251 | ]); |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | public function postMapField(Request $request) |
||
259 | { |
||
260 | try { |
||
261 | $this->relation->fill($request->input())->save(); |
||
262 | |||
263 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
264 | } catch (Exception $ex) { |
||
265 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | public function postGroupMapField(Request $request) |
||
270 | { |
||
271 | try { |
||
272 | MailchimpGroupAgoraRelation::where('id', '!=', 0)->delete(); |
||
273 | foreach ($request->row as $key => $value) { |
||
274 | MailchimpGroupAgoraRelation::create(['agora_product_id'=> $value[0], |
||
275 | 'mailchimp_group_cat_id' => $value[1], ]); |
||
276 | } |
||
277 | |||
278 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
279 | } catch (Exception $ex) { |
||
280 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | public function postIsPaidMapField(Request $request) |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.