| Total Complexity | 45 |
| Total Lines | 341 |
| 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 |
||
| 18 | class MailChimpController extends Controller |
||
| 19 | { |
||
| 20 | protected $mail_api_key; |
||
| 21 | protected $mailchimp; |
||
| 22 | protected $mailchimp_field_model; |
||
| 23 | protected $mailchimp_set; |
||
| 24 | protected $list_id; |
||
| 25 | protected $lists; |
||
| 26 | protected $relation; |
||
| 27 | |||
| 28 | public function __construct() |
||
| 29 | { |
||
| 30 | $mailchimp_set = new MailchimpSetting(); |
||
| 31 | $this->mailchimp_set = $mailchimp_set->firstOrFail(); |
||
| 32 | $this->mail_api_key = $this->mailchimp_set->api_key; |
||
| 33 | $this->list_id = $this->mailchimp_set->list_id; |
||
| 34 | $this->product_group_id = $this->mailchimp_set->group_id_products; |
||
|
|
|||
| 35 | |||
| 36 | $mailchimp_filed_model = new MailchimpField(); |
||
| 37 | $this->mailchimp_field_model = $mailchimp_filed_model; |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | $lists = new MailchimpLists(); |
||
| 42 | $this->lists = $lists; |
||
| 43 | |||
| 44 | $groups = new MailchimpGroup(); |
||
| 45 | $this->groups = $groups; |
||
| 46 | |||
| 47 | $relation = new MailchimpFieldAgoraRelation(); |
||
| 48 | $this->relation = $relation->firstOrFail(); |
||
| 49 | |||
| 50 | $groupRelation = new MailchimpGroupAgoraRelation(); |
||
| 51 | $this->groupRelation = $groupRelation; |
||
| 52 | |||
| 53 | $this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function getLists() |
||
| 57 | { |
||
| 58 | try { |
||
| 59 | $result = $this->mailchimp->request('lists'); |
||
| 60 | |||
| 61 | return $result; |
||
| 62 | } catch (Exception $ex) { |
||
| 63 | dd($ex); |
||
| 64 | |||
| 65 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getListById() |
||
| 70 | { |
||
| 71 | try { |
||
| 72 | $result = $this->mailchimp->request("lists/$this->list_id"); |
||
| 73 | |||
| 74 | return $result; |
||
| 75 | } catch (Exception $ex) { |
||
| 76 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public function addSubscriber($email) |
||
| 81 | { |
||
| 82 | try { |
||
| 83 | $merge_fields = $this->field($email); |
||
| 84 | $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No |
||
| 85 | $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
||
| 86 | $result = $this->mailchimp->post("lists/$this->list_id/members", [ |
||
| 87 | 'status' => $this->mailchimp_set->subscribe_status, |
||
| 88 | 'email_address' => $email, |
||
| 89 | 'merge_fields' => $merge_fields, |
||
| 90 | |||
| 91 | 'interests' => array( $interestGroupIdForNo => true , $interestGroupIdForYes=>false ), |
||
| 92 | |||
| 93 | ]); |
||
| 94 | |||
| 95 | return $result; |
||
| 96 | } catch (Exception $ex) { |
||
| 97 | $exe = json_decode($ex->getMessage(), true); |
||
| 98 | if ($exe['status'] == 400) { |
||
| 99 | throw new Exception("$email is already subscribed to newsletter", 400); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | //Update to Mailchimp For Free Product |
||
| 105 | public function updateSubscriberForFreeProduct($email,$productid) |
||
| 106 | { |
||
| 107 | try { |
||
| 108 | $merge_fields = $this->field($email); |
||
| 109 | $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No |
||
| 110 | $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
||
| 111 | $productGroupId = $this->groupRelation->where('agora_product_id',$productid) |
||
| 112 | ->pluck('mailchimp_group_cat_id')->first(); |
||
| 113 | $hash = md5($email); |
||
| 114 | $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash",[ |
||
| 115 | 'interests' => array( $interestGroupIdForNo => true , $interestGroupIdForYes=>false, $productGroupId =>true), |
||
| 116 | //refer to https://us7.api.mailchimp.com/playground |
||
| 117 | ]); |
||
| 118 | } catch (Exception $ex) { |
||
| 119 | $exe = json_decode($ex->getMessage(), true); |
||
| 120 | |||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | //Update to Mailchimp For Paid Product |
||
| 125 | public function updateSubscriberForPaidProduct($email,$productid) |
||
| 126 | { |
||
| 127 | try { |
||
| 128 | $merge_fields = $this->field($email); |
||
| 129 | $interestGroupIdForNo = $this->relation->is_paid_no ; //Interest GroupId for IsPaid Is No |
||
| 130 | $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes |
||
| 131 | $productGroupId = $this->groupRelation->where('agora_product_id',$productid) |
||
| 132 | ->pluck('mailchimp_group_cat_id')->first(); |
||
| 133 | $hash = md5($email); |
||
| 134 | $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash",[ |
||
| 135 | 'interests' => array( $interestGroupIdForNo => false , $interestGroupIdForYes=>true ,$productGroupId =>true), |
||
| 136 | //refer to https://us7.api.mailchimp.com/playground |
||
| 137 | ]); |
||
| 138 | } catch (Exception $ex) { |
||
| 139 | $exe = json_decode($ex->getMessage(), true); |
||
| 140 | |||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function addSubscriberByClientPanel(Request $request) |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | public function field($email) |
||
| 173 | { |
||
| 174 | try { |
||
| 175 | $user = new User(); |
||
| 176 | $setting = new Setting(); |
||
| 177 | $user = $user->where('email', $email)->first(); |
||
| 178 | if ($user) { |
||
| 179 | $fields = ['first_name', 'last_name', 'company', 'mobile', |
||
| 180 | 'address', 'town', 'state', 'zip', 'active', 'role','source' ]; |
||
| 181 | $relation = $this->relation; |
||
| 182 | $merge_fields = []; |
||
| 183 | foreach ($fields as $field) { |
||
| 184 | if ($relation->$field) { |
||
| 185 | $merge_fields[$relation->$field] = $user->$field; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | $merge_fields[$relation->source] = $setting->findorFail(1)->title; |
||
| 189 | return $merge_fields; |
||
| 190 | } else { |
||
| 191 | return redirect()->back()->with('fails', 'user not found'); |
||
| 192 | } |
||
| 193 | } catch (Exception $ex) { |
||
| 194 | //dd($ex); |
||
| 195 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getMergeFields() |
||
| 200 | { |
||
| 201 | try { |
||
| 202 | $result = $this->mailchimp->get("lists/$this->list_id/merge-fields"); |
||
| 203 | |||
| 204 | return $result; |
||
| 205 | } catch (Exception $ex) { |
||
| 206 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | public function addFieldsToAgora() |
||
| 211 | { |
||
| 212 | try { |
||
| 213 | /** @scrutinizer ignore-call */ |
||
| 214 | $fields = $this->getMergeFields($this->list_id); |
||
| 215 | $mailchimp_field_in_agora = $this->mailchimp_field_model->get(); |
||
| 216 | if (count($mailchimp_field_in_agora) > 0) { |
||
| 217 | foreach ($mailchimp_field_in_agora as $field) { |
||
| 218 | $field->delete(); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | foreach ($fields['merge_fields'] as $key => $value) { |
||
| 222 | $merge_id = $value->merge_id; |
||
| 223 | $name = $value->name; |
||
| 224 | $type = $value->type; |
||
| 225 | $required = $value->required; |
||
| 226 | $list_id = $value->list_id; |
||
| 227 | $tag = $value->tag; |
||
| 228 | |||
| 229 | $this->mailchimp_field_model->create([ |
||
| 230 | 'merge_id' => $merge_id, |
||
| 231 | 'tag' => $tag, |
||
| 232 | 'name' => $name, |
||
| 233 | 'type' => $type, |
||
| 234 | 'required' => $required, |
||
| 235 | 'list_id' => $list_id, |
||
| 236 | ]); |
||
| 237 | } |
||
| 238 | } catch (Exception $ex) { |
||
| 239 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | public function addListsToAgora() |
||
| 244 | { |
||
| 245 | try { |
||
| 246 | $lists = $this->getLists(); |
||
| 247 | $agora_lists = $this->lists->get(); |
||
| 248 | if (count($agora_lists) > 0) { |
||
| 249 | foreach ($agora_lists as $agora) { |
||
| 250 | $agora->delete(); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | foreach ($lists['lists'] as $list) { |
||
| 254 | $name = $list->name; |
||
| 255 | $list_id = $list->id; |
||
| 256 | $this->lists->create([ |
||
| 257 | 'name' => $name, |
||
| 258 | 'list_id' => $list_id, |
||
| 259 | ]); |
||
| 260 | } |
||
| 261 | //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora')); |
||
| 262 | } catch (Exception $ex) { |
||
| 263 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | public function mailChimpSettings() |
||
| 268 | { |
||
| 269 | try { |
||
| 270 | $set = $this->mailchimp_set; |
||
| 271 | $lists = $this->lists->pluck('name', 'list_id')->toArray(); |
||
| 272 | |||
| 273 | return view('themes.default1.common.mailchimp.settings', compact('set', 'lists')); |
||
| 274 | } catch (Exception $ex) { |
||
| 275 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | public function postMailChimpSettings(Request $request) |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | public function mapField() |
||
| 297 | { |
||
| 298 | try { |
||
| 299 | $model = $this->relation; |
||
| 300 | $model2 = $this->groupRelation; |
||
| 301 | $this->addFieldsToAgora(); |
||
| 302 | $mailchimp_fields = $this->mailchimp_field_model |
||
| 303 | ->where('list_id', $this->list_id)->pluck('name', 'tag')->toArray(); |
||
| 304 | $agoraProducts = Product::pluck('name','id')->toArray(); |
||
| 305 | $groupInterests =$this->mailchimp->get("lists/$this->list_id/interest-categories/$this->product_group_id/interests"); |
||
| 306 | //get all the fields for Product Group |
||
| 307 | if(count($groupInterests)>0){ |
||
| 308 | foreach ($groupInterests['interests'] as $key=>$value) { |
||
| 309 | |||
| 310 | $category_id = $value->category_id; |
||
| 311 | $list_id = $value->list_id; |
||
| 312 | $category_option_id = $value->id; |
||
| 313 | $category_option_name = $value->name; |
||
| 314 | $this->groups->create([ |
||
| 315 | 'category_id' => $category_id, |
||
| 316 | 'list_id' => $list_id, |
||
| 317 | 'category_option_id' => $category_option_id, |
||
| 318 | 'category_name' => $category_option_name, |
||
| 319 | ]); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | $product_group_fields = $this->groups->where('list_id', $this->list_id) |
||
| 324 | ->pluck('category_name','category_option_id','category_id')->toArray(); |
||
| 325 | $relations = MailchimpGroupAgoraRelation::where('id','!=',0) |
||
| 326 | ->select('agora_product_id','mailchimp_group_cat_id') |
||
| 327 | ->orderBy('id','asc')->get()->toArray(); |
||
| 328 | return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model2','model','product_group_fields','agoraProducts','relations')); |
||
| 329 | } catch (Exception $ex) { |
||
| 330 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | public function postMapField(Request $request) |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | public function postGroupMapField(Request $request) |
||
| 348 | { |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | } |
||
| 363 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.