Completed
Push — development ( c2ff46...95cb59 )
by Ashutosh
28:29 queued 18:30
created

BaseMailChimpController::addListsToAgora()   A

Complexity

Conditions 5
Paths 22

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 21
rs 9.4888
c 0
b 0
f 0
cc 5
nc 22
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Common\StatusSetting;
7
8
class BaseMailChimpController extends Controller
9
{
10
    public function getLists()
11
    {
12
        try {
13
            $result = $this->mailchimp->request('lists');
14
15
            return $result;
16
        } 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...
17
            return redirect()->back()->with('fails', $ex->getMessage());
18
        }
19
    }
20
21
    public function getListById()
22
    {
23
        try {
24
            $result = $this->mailchimp->request("lists/$this->list_id");
25
26
            return $result;
27
        } catch (Exception $ex) {
28
            return redirect()->back()->with('fails', $ex->getMessage());
29
        }
30
    }
31
32
    //Update to Mailchimp For Free Product
33
    public function updateSubscriberForFreeProduct($email, $productid)
34
    {
35
        try {
36
            $productGroupId = '';
37
            $interestGroupIdForNo = '';
38
            $interestGroupIdForYes = '';
39
            $merge_fields = $this->field($email);
40
            $hash = md5($email);
41
            $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
42
            $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
43
            if ($isPaidStatus == 1) {
44
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
45
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
46
            }
47
            if ($productStatusStatus == 1) {
48
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
49
                ->pluck('mailchimp_group_cat_id')->first();
50
            }
51
            if ($interestGroupIdForNo && $productGroupId) {
52
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
53
                 '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...
54
                  ]);
55
            //refer to https://us7.api.mailchimp.com/playground
56
            } elseif ($interestGroupIdForNo && $productGroupId == null) {
57
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
58
                 'interests'         => [$interestGroupIdForNo => true, $interestGroupIdForYes=>false],
59
                  ]);
60
            //refer to https://us7.api.mailchimp.com/playground
61
            } 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...
62
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
63
                 'interests'         => [$productGroupId =>true],
64
                  ]);
65
            }
66
        } catch (Exception $ex) {
67
            $exe = json_decode($ex->getMessage(), true);
68
        }
69
    }
70
71
    public function updateSubscriberForPaidProduct($email, $productid)
72
    {
73
        try {
74
            $merge_fields = $this->field($email);
75
            $hash = md5($email);
76
            $isPaidStatus = StatusSetting::select()->value('mailchimp_ispaid_status');
77
            $productStatusStatus = StatusSetting::select()->value('mailchimp_product_status');
78
            if ($isPaidStatus == 1) {
79
                $interestGroupIdForNo = $this->relation->is_paid_no; //Interest GroupId for IsPaid Is No
80
                $interestGroupIdForYes = $this->relation->is_paid_yes; //Interest GroupId for IsPaid Is Yes
81
            }
82
            if ($productStatusStatus == 1) {
83
                $productGroupId = $this->groupRelation->where('agora_product_id', $productid)
84
                ->pluck('mailchimp_group_cat_id')->first();
85
            }
86
            if ($interestGroupIdForNo && $productGroupId) {
87
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
88
                 '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...
89
                 //refer to https://us7.api.mailchimp.com/playground
90
                 ]);
91
            } elseif ($interestGroupIdForNo && $productGroupId == null) {
92
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
93
                 'interests'         => [$interestGroupIdForNo => false, $interestGroupIdForYes=>true],
94
                  ]);
95
            //refer to https://us7.api.mailchimp.com/playground
96
            } 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...
97
                $result = $this->mailchimp->patch("lists/$this->list_id/members/$hash", [
98
                 'interests'         => [$productGroupId =>true],
99
                  ]);
100
            }
101
            //refer to https://us7.api.mailchimp.com/playground
102
        } catch (Exception $ex) {
103
            $exe = json_decode($ex->getMessage(), true);
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
            return view('themes.default1.common.mailchimp.settings', compact('set','allists','selectedList'));
125
        } catch (Exception $ex) {
126
            return redirect()->back()->with('fails', $ex->getMessage());
127
        }
128
    }
129
130
    public function postMailChimpSettings(Request $request)
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Common\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
131
    {
132
        $this->validate($request, [
133
            'api_key' => 'required',
134
            'list_id'=>'required',
135
        ]);
136
137
        try {
138
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