| Total Complexity | 53 |
| Total Lines | 344 |
| 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 |
||
| 20 | class MailChimpController extends BaseMailChimpController |
||
| 21 | { |
||
| 22 | protected $mail_api_key; |
||
| 23 | protected $mailchimp; |
||
| 24 | protected $mailchimp_field_model; |
||
| 25 | protected $mailchimp_set; |
||
| 26 | protected $list_id; |
||
| 27 | protected $lists; |
||
| 28 | protected $relation; |
||
| 29 | |||
| 30 | public function __construct() |
||
| 31 | { |
||
| 32 | $mailchimp_set = new MailchimpSetting(); |
||
| 33 | $this->mailchimp_set = $mailchimp_set->firstOrFail(); |
||
| 34 | $this->mail_api_key = $this->mailchimp_set->api_key; |
||
| 35 | $this->list_id = $this->mailchimp_set->list_id; |
||
| 36 | $this->product_group_id = $this->mailchimp_set->group_id_products; |
||
| 37 | $this->is_paid_group_id = $this->mailchimp_set->group_id_is_paid; |
||
| 38 | |||
| 39 | $mailchimp_filed_model = new MailchimpField(); |
||
| 40 | $this->mailchimp_field_model = $mailchimp_filed_model; |
||
| 41 | |||
| 42 | $lists = new MailchimpLists(); |
||
| 43 | $this->lists = $lists; |
||
| 44 | |||
| 45 | $groups = new MailchimpGroup(); |
||
| 46 | $this->groups = $groups; |
||
| 47 | |||
| 48 | $relation = new MailchimpFieldAgoraRelation(); |
||
| 49 | $this->relation = $relation->firstOrFail(); |
||
| 50 | |||
| 51 | $groupRelation = new MailchimpGroupAgoraRelation(); |
||
| 52 | $this->groupRelation = $groupRelation; |
||
| 53 | |||
| 54 | $this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key); |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | public function addSubscriber($email) |
||
| 61 | { |
||
| 62 | try { |
||
| 63 | $merge_fields = $this->field($email); |
||
| 64 | $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No |
||
| 65 | $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
||
| 66 | $result = $this->mailchimp->post("lists/$this->list_id/members", [ |
||
| 67 | 'status' => $this->mailchimp_set->subscribe_status, |
||
| 68 | 'email_address' => $email, |
||
| 69 | 'merge_fields' => $merge_fields, |
||
| 70 | |||
| 71 | 'interests' => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false], |
||
| 72 | |||
| 73 | ]); |
||
| 74 | |||
| 75 | return $result; |
||
| 76 | } catch (Exception $ex) { |
||
| 77 | $exe = json_decode($ex->getMessage(), true); |
||
| 78 | if ($exe['status'] == 400) { |
||
| 79 | throw new Exception("$email is already subscribed to newsletter", 400); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | //Update to Mailchimp For Paid Product |
||
| 87 | public function addSubscriberByClientPanel(Request $request) |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | public function field($email) |
||
| 115 | { |
||
| 116 | try { |
||
| 117 | $user = new User(); |
||
| 118 | $setting = new Setting(); |
||
| 119 | $user = $user->where('email', $email)->first(); |
||
| 120 | $country = Country::where('country_code_char2', $user->country)->pluck('nicename')->first(); |
||
| 121 | if ($user) { |
||
| 122 | $fields = ['first_name', 'last_name', 'company', 'mobile', |
||
| 123 | 'address', 'town', 'country', 'state', 'zip', 'active', 'role', 'source', ]; |
||
| 124 | $relation = $this->relation; |
||
| 125 | $merge_fields = []; |
||
| 126 | foreach ($fields as $field) { |
||
| 127 | if ($relation->$field) { |
||
| 128 | $merge_fields[$relation->$field] = $user->$field; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $merge_fields[$relation->source] = $setting->findorFail(1)->title; |
||
| 132 | |||
| 133 | return $merge_fields; |
||
| 134 | } else { |
||
| 135 | return redirect()->back()->with('fails', 'user not found'); |
||
| 136 | } |
||
| 137 | } catch (Exception $ex) { |
||
| 138 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | public function addFieldsToAgora() |
||
| 145 | { |
||
| 146 | try { |
||
| 147 | /** @scrutinizer ignore-call */ |
||
| 148 | $fields = $this->getMergeFields($this->list_id); |
||
| 149 | $mailchimp_field_in_agora = $this->mailchimp_field_model->get(); |
||
| 150 | if (count($mailchimp_field_in_agora) > 0) { |
||
| 151 | foreach ($mailchimp_field_in_agora as $field) { |
||
| 152 | $field->delete(); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | foreach ($fields['merge_fields'] as $key => $value) { |
||
| 156 | $merge_id = $value->merge_id; |
||
| 157 | $name = $value->name; |
||
| 158 | $type = $value->type; |
||
| 159 | $required = $value->required; |
||
| 160 | $list_id = $value->list_id; |
||
| 161 | $tag = $value->tag; |
||
| 162 | |||
| 163 | $this->mailchimp_field_model->create([ |
||
| 164 | 'merge_id' => $merge_id, |
||
| 165 | 'tag' => $tag, |
||
| 166 | 'name' => $name, |
||
| 167 | 'type' => $type, |
||
| 168 | 'required' => $required, |
||
| 169 | 'list_id' => $list_id, |
||
| 170 | ]); |
||
| 171 | } |
||
| 172 | } catch (Exception $ex) { |
||
| 173 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | public function addListsToAgora() |
||
| 178 | { |
||
| 179 | try { |
||
| 180 | $lists = $this->getLists(); |
||
| 181 | $agora_lists = $this->lists->get(); |
||
| 182 | if (count($agora_lists) > 0) { |
||
| 183 | foreach ($agora_lists as $agora) { |
||
| 184 | $agora->delete(); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | foreach ($lists['lists'] as $list) { |
||
| 188 | $name = $list->name; |
||
| 189 | $list_id = $list->id; |
||
| 190 | $this->lists->create([ |
||
| 191 | 'name' => $name, |
||
| 192 | 'list_id' => $list_id, |
||
| 193 | ]); |
||
| 194 | } |
||
| 195 | //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora')); |
||
| 196 | } catch (Exception $ex) { |
||
| 197 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | public function mailChimpSettings() |
||
| 202 | { |
||
| 203 | try { |
||
| 204 | $set = $this->mailchimp_set; |
||
| 205 | $allists = $this->mailchimp->get('lists?count=20')['lists']; |
||
| 206 | $selectedList[] = $set->list_id; |
||
|
|
|||
| 207 | return view('themes.default1.common.mailchimp.settings', compact('set','allists','selectedList')); |
||
| 208 | } catch (Exception $ex) { |
||
| 209 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | public function postMailChimpSettings(Request $request) |
||
| 214 | { |
||
| 215 | $this->validate($request, [ |
||
| 216 | 'api_key' => 'required', |
||
| 217 | 'list_id'=>'required', |
||
| 218 | ]); |
||
| 219 | |||
| 220 | try { |
||
| 221 | |||
| 222 | $this->mailchimp_set->first()->update(['subscribe_status'=>$request->input('subscribe_status'), |
||
| 223 | 'list_id'=>$request->input('list_id')]); |
||
| 224 | $this->addListsToAgora(); |
||
| 225 | |||
| 226 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 227 | } catch (Exception $ex) { |
||
| 228 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | public function mapField() |
||
| 233 | { |
||
| 234 | try { |
||
| 235 | $model = $this->relation; |
||
| 236 | $model2 = $this->groupRelation; |
||
| 237 | $this->addFieldsToAgora(); |
||
| 238 | |||
| 239 | $mailchimp_fields = $this->mailchimp_field_model |
||
| 240 | ->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray(); |
||
| 241 | $agoraProducts = Product::pluck('name', 'id')->toArray(); |
||
| 242 | |||
| 243 | $mailchimpProducts = $this->mailchimp->get("lists/$this->list_id/interest-categories"); |
||
| 244 | $selectedProducts = MailchimpGroupAgoraRelation::select('agora_product_id', 'mailchimp_group_cat_id')->orderBy('id', 'asc')->get()->toArray(); |
||
| 245 | $allGroups = $this->mailchimp->get("lists/$this->list_id/interest-categories"); //Get all the groups(interest-categories for a list) |
||
| 246 | foreach ($allGroups['categories'] as $key => $value) { |
||
| 247 | $display[] = (['id'=> $value->id, 'title'=>$value->title]); |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->addProductInterestFieldsToAgora(); //add all the fields in Product Section of Groups to the db |
||
| 251 | $group_fields = $this->groups->where('list_id', $this->list_id) |
||
| 252 | ->select('category_name', 'category_option_id', 'category_id')->get()->toArray(); |
||
| 253 | // dd($group_fields[0]); |
||
| 254 | $relations = MailchimpGroupAgoraRelation::where('id', '!=', 0) |
||
| 255 | ->select('agora_product_id', 'mailchimp_group_cat_id') |
||
| 256 | ->orderBy('id', 'asc')->get()->toArray(); |
||
| 257 | $productList = []; |
||
| 258 | $categoryList = []; |
||
| 259 | if (count($relations) != 0) { |
||
| 260 | foreach ($relations as $key => $value) { |
||
| 261 | $categoryList[] = $this->groups->where('category_option_id', $value['mailchimp_group_cat_id'])->pluck('category_name')->first(); |
||
| 262 | $productList[] = Product::where('id', $value['agora_product_id'])->pluck('name')->first(); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | $isPaidYesId = MailchimpFieldAgoraRelation::first()->pluck('is_paid_yes')->toArray(); |
||
| 266 | $selectedIsPaid[] = $isPaidYesId ? MailchimpGroup::where('category_option_id',$isPaidYesId)->pluck('category_id')->first() :''; |
||
| 267 | $status = StatusSetting::select('mailchimp_product_status','mailchimp_ispaid_status')->first(); |
||
| 268 | return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model2', 'model', 'agoraProducts', 'display', 'selectedProducts', 'relations', 'group_fields', 'categoryList', 'productList','status','selectedIsPaid')); |
||
| 269 | } catch (Exception $ex) { |
||
| 270 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | public function addInterestFieldsToAgora(Request $request, $value) |
||
| 275 | { |
||
| 276 | $groupInterests = $this->mailchimp->get("lists/$this->list_id/interest-categories/$value/interests?count=20"); |
||
| 277 | echo '<option value="">Choose a Category</option>'; |
||
| 278 | if (count($groupInterests) > 0) { |
||
| 279 | foreach ($groupInterests['interests'] as $key=>$value) { |
||
| 280 | $fields[] = (['category_id' => $value->category_id, |
||
| 281 | 'list_id' => $value->list_id, |
||
| 282 | 'category_option_id' => $value->id, |
||
| 283 | 'category_option_name' => $value->name, |
||
| 284 | ]); |
||
| 285 | } |
||
| 286 | foreach ($fields as $field) { |
||
| 287 | $selectedCategory = MailchimpGroupAgoraRelation::where('mailchimp_group_cat_id', $field['category_option_id'])->pluck('mailchimp_group_cat_id')->first(); |
||
| 288 | echo '<option value='.$field['category_option_id'].'>'.$field['category_option_name'].'</option>'; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | } |
||
| 293 | |||
| 294 | public function addProductInterestFieldsToAgora() |
||
| 311 | ]); |
||
| 312 | |||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | |||
| 320 | public function postMapField(Request $request) |
||
| 321 | { |
||
| 322 | try { |
||
| 323 | $this->relation->fill($request->input())->save(); |
||
| 324 | |||
| 325 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 326 | } catch (Exception $ex) { |
||
| 327 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | public function postGroupMapField(Request $request) |
||
| 332 | { |
||
| 333 | try { |
||
| 334 | MailchimpGroupAgoraRelation::where('id', '!=', 0)->delete(); |
||
| 335 | foreach ($request->row as $key => $value) { |
||
| 336 | MailchimpGroupAgoraRelation::create(['agora_product_id'=> $value[0], |
||
| 337 | 'mailchimp_group_cat_id' => $value[1], ]); |
||
| 338 | } |
||
| 339 | |||
| 340 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 341 | } catch (Exception $ex) { |
||
| 342 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | public function postIsPaidMapField(Request $request) |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 |