Completed
Push — development ( 2be1cb...034e49 )
by Ashutosh
11:52
created

updateSubscriberForPaidProduct()   B

Complexity

Conditions 11
Paths 34

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 34
rs 7.3166
c 0
b 0
f 0
cc 11
nc 34
nop 2

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use Illuminate\Http\Request;
6
use App\Model\Common\StatusSetting;
7
use App\Http\Controllers\Controller;
8
9
class BaseMailChimpController extends Controller
10
{
11
	public function getLists()
12
    {
13
        try {
14
            $result = $this->mailchimp->request('lists');
0 ignored issues
show
Bug Best Practice introduced by
The property mailchimp does not exist on App\Http\Controllers\Com...BaseMailChimpController. Did you maybe forget to declare it?
Loading history...
15
16
            return $result;
17
        } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Common\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
18
            return redirect()->back()->with('fails', $ex->getMessage());
19
        }
20
    }
21
22
    public function getListById()
23
    {
24
        try {
25
            $result = $this->mailchimp->request("lists/$this->list_id");
0 ignored issues
show
Bug Best Practice introduced by
The property list_id does not exist on App\Http\Controllers\Com...BaseMailChimpController. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property mailchimp does not exist on App\Http\Controllers\Com...BaseMailChimpController. Did you maybe forget to declare it?
Loading history...
26
27
            return $result;
28
        } catch (Exception $ex) {
29
            return redirect()->back()->with('fails', $ex->getMessage());
30
        }
31
    }
32
33
34
        //Update to Mailchimp For Free Product
35
    public function updateSubscriberForFreeProduct($email, $productid)
36
    {
37
        try {
38
            $productGroupId = '';
39
            $interestGroupIdForNo = '';
40
            $interestGroupIdForYes = '';
41
            $merge_fields = $this->field($email);
42
            $hash = md5($email);
43
            $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
44
            $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
45
           if ($isPaidStatus ==1) {
46
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
47
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
48
            }
49
            if ($productStatusStatus ==1) {
50
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
51
                ->pluck('mailchimp_group_cat_id')->first();
52
            }
53
            if ($interestGroupIdForNo  &&  $productGroupId) {
54
                 $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
55
                 'interests'         => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false, $productGroupId =>true],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

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.

Loading history...
56
                  ]);
57
                 //refer to https://us7.api.mailchimp.com/playground
58
                
59
            } elseif ($interestGroupIdForNo  &&  $productGroupId ==null) {
60
                 $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
61
                 'interests'         => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false],
62
                  ]);
63
                 //refer to https://us7.api.mailchimp.com/playground
64
            } elseif ($productGroupId && $interestGroupIdForNo==null || $interestGroupIdForYes==null) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($productGroupId && $int...stGroupIdForYes == null, Probably Intended Meaning: $productGroupId && ($int...tGroupIdForYes == null)
Loading history...
65
                  $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
66
                 'interests'         => [$productGroupId =>true],
67
                  ]);
68
            }
69
        } catch (Exception $ex) {
70
            $exe = json_decode($ex->getMessage(), true);
71
        }
72
    }
73
74
    public function updateSubscriberForPaidProduct($email, $productid)
75
    {
76
        try {
77
            $merge_fields = $this->field($email);
78
               $hash = md5($email);
79
             $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
80
             $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
81
            if ($isPaidStatus ==1) {
82
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
83
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
84
            }
85
            if ($productStatusStatus ==1) {
86
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
87
                ->pluck('mailchimp_group_cat_id')->first();
88
            }
89
            if ($interestGroupIdForNo && $productGroupId) {
90
                 $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
91
                 'interests'         => [$interestGroupIdForNo => false, $interestGroupIdForYes=>true, $productGroupId =>true],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

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.

Loading history...
92
                 //refer to https://us7.api.mailchimp.com/playground
93
                 ]);
94
            } elseif ($interestGroupIdForNo && $productGroupId ==null) {
95
                 $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
96
                 'interests'         => [$interestGroupIdForNo => false, $interestGroupIdForYes=>true],
97
                  ]);
98
                 //refer to https://us7.api.mailchimp.com/playground
99
            } elseif ($productGroupId && $interestGroupIdForNo==null || $interestGroupIdForYes==null) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($productGroupId && $int...stGroupIdForYes == null, Probably Intended Meaning: $productGroupId && ($int...tGroupIdForYes == null)
Loading history...
100
                  $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
101
                 'interests'         => [$productGroupId =>true],
102
                  ]);
103
            }
104
                 //refer to https://us7.api.mailchimp.com/playground
105
            
106
        } catch (Exception $ex) {
107
            $exe = json_decode($ex->getMessage(), true);
108
        }
109
    }
110
111
    public function getMergeFields()
112
    {
113
        try {
114
            $result = $this->mailchimp->get("lists/$this->list_id/merge-fields?count=20");
0 ignored issues
show
Bug Best Practice introduced by
The property mailchimp does not exist on App\Http\Controllers\Com...BaseMailChimpController. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property list_id does not exist on App\Http\Controllers\Com...BaseMailChimpController. Did you maybe forget to declare it?
Loading history...
115
116
            return $result;
117
        } catch (Exception $ex) {
118
            return redirect()->back()->with('fails', $ex->getMessage());
119
        }
120
    }
121
}
122