BaseMailChimpController   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 37
eloc 93
dl 0
loc 161
rs 9.44
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getListById() 0 8 2
A getLists() 0 8 2
A getMergeFields() 0 8 2
A addListsToAgora() 0 21 5
A mailChimpSettings() 0 10 2
A postMailChimpSettings() 0 15 2
B updateSubscriberForPaidProduct() 0 32 11
B updateSubscriberForFreeProduct() 0 35 11
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Common\StatusSetting;
7
use Illuminate\Http\Request;
8
9
class BaseMailChimpController extends Controller
10
{
11
    public function getLists()
12
    {
13
        try {
14
            $result = $this->mailchimp->request('lists');
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");
26
27
            return $result;
28
        } catch (Exception $ex) {
29
            return redirect()->back()->with('fails', $ex->getMessage());
30
        }
31
    }
32
33
    //Update to Mailchimp For Free Product
34
    public function updateSubscriberForFreeProduct($email, $productid)
35
    {
36
        try {
37
            $productGroupId = '';
38
            $interestGroupIdForNo = '';
39
            $interestGroupIdForYes = '';
40
            $merge_fields = $this->field($email);
41
            $hash = md5($email);
42
            $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
43
            $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
44
            if ($isPaidStatus == 1) {
45
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
46
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
47
            }
48
            if ($productStatusStatus == 1) {
49
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
50
                ->pluck('mailchimp_group_cat_id')->first();
51
            }
52
            if ($interestGroupIdForNo && $productGroupId) {
53
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
54
                    'interests'         => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false, $productGroupId =>true],
55
                ]);
56
            //refer to https://us7.api.mailchimp.com/playground
57
            } elseif ($interestGroupIdForNo && $productGroupId == null) {
58
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
59
                    'interests'         => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false],
60
                ]);
61
            //refer to https://us7.api.mailchimp.com/playground
62
            } 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...
63
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
64
                    'interests'         => [$productGroupId =>true],
65
                ]);
66
            }
67
        } catch (Exception $ex) {
68
            $exe = json_decode($ex->getMessage(), true);
69
        }
70
    }
71
72
    public function updateSubscriberForPaidProduct($email, $productid)
73
    {
74
        try {
75
            $merge_fields = $this->field($email);
76
            $hash = md5($email);
77
            $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
78
            $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
79
            if ($isPaidStatus == 1) {
80
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
81
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
82
            }
83
            if ($productStatusStatus == 1) {
84
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
85
                ->pluck('mailchimp_group_cat_id')->first();
86
            }
87
            if ($interestGroupIdForNo && $productGroupId) {
88
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
89
                    'interests'         => [$interestGroupIdForNo => false, $interestGroupIdForYes=>true, $productGroupId =>true],
90
                    //refer to https://us7.api.mailchimp.com/playground
91
                ]);
92
            } elseif ($interestGroupIdForNo && $productGroupId == null) {
93
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
94
                    'interests'         => [$interestGroupIdForNo => false, $interestGroupIdForYes=>true],
95
                ]);
96
            //refer to https://us7.api.mailchimp.com/playground
97
            } 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...
98
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
99
                    'interests'         => [$productGroupId =>true],
100
                ]);
101
            }
102
            //refer to https://us7.api.mailchimp.com/playground
103
        } catch (Exception $ex) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
104
        }
105
    }
106
107
    public function getMergeFields()
108
    {
109
        try {
110
            $result = $this->mailchimp->get("lists/$this->list_id/merge-fields?count=20");
111
112
            return $result;
113
        } catch (Exception $ex) {
114
            return redirect()->back()->with('fails', $ex->getMessage());
115
        }
116
    }
117
118
    public function mailChimpSettings()
119
    {
120
        try {
121
            $set = $this->mailchimp_set;
122
            $allists = $this->mailchimp->get('lists?count=20')['lists'];
123
            $selectedList[] = $set->list_id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$selectedList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $selectedList = array(); before regardless.
Loading history...
124
125
            return view('themes.default1.common.mailchimp.settings', compact('set', 'allists', 'selectedList'));
126
        } catch (Exception $ex) {
127
            return redirect()->back()->with('fails', $ex->getMessage());
128
        }
129
    }
130
131
    public function postMailChimpSettings(Request $request)
132
    {
133
        $this->validate($request, [
134
            'api_key' => 'required',
135
            'list_id' => 'required',
136
        ]);
137
138
        try {
139
            $this->mailchimp_set->first()->update(['subscribe_status'=> $request->input('subscribe_status'),
140
                'list_id'                                            => $request->input('list_id'), ]);
141
            $this->addListsToAgora();
142
143
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
144
        } catch (Exception $ex) {
145
            return redirect()->back()->with('fails', $ex->getMessage());
146
        }
147
    }
148
149
    public function addListsToAgora()
150
    {
151
        try {
152
            $lists = $this->getLists();
153
            $agora_lists = $this->lists->get();
154
            if (count($agora_lists) > 0) {
155
                foreach ($agora_lists as $agora) {
156
                    $agora->delete();
157
                }
158
            }
159
            foreach ($lists['lists'] as $list) {
160
                $name = $list->name;
161
                $list_id = $list->id;
162
                $this->lists->create([
163
                    'name'    => $name,
164
                    'list_id' => $list_id,
165
                ]);
166
            }
167
            //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora'));
168
        } catch (Exception $ex) {
169
            return redirect()->back()->with('fails', $ex->getMessage());
170
        }
171
    }
172
}
173