Completed
Pull Request — master (#113)
by vijay
105:56 queued 52:59
created

MailChimpController::addSubscriberByClientPanel()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 15
nc 7
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Common\Mailchimp\MailchimpField;
7
use App\Model\Common\Mailchimp\MailchimpFieldAgoraRelation;
8
use App\Model\Common\Mailchimp\MailchimpLists;
9
use App\Model\Common\Mailchimp\MailchimpSetting;
10
use App\User;
11
use Exception;
12
use Illuminate\Http\Request;
13
14
class MailChimpController extends Controller
15
{
16
    protected $mail_api_key;
17
    protected $mailchimp;
18
    protected $mailchimp_field_model;
19
    protected $mailchimp_set;
20
    protected $list_id;
21
    protected $lists;
22
    protected $relation;
23
24
    public function __construct()
25
    {
26
        $mailchimp_set = new MailchimpSetting();
27
        $this->mailchimp_set = $mailchimp_set->firstOrFail();
28
        $this->mail_api_key = $this->mailchimp_set->api_key;
29
        $this->list_id = $this->mailchimp_set->list_id;
30
31
        $mailchimp_filed_model = new MailchimpField();
32
        $this->mailchimp_field_model = $mailchimp_filed_model;
33
34
        $lists = new MailchimpLists();
35
        $this->lists = $lists;
36
37
        $relation = new MailchimpFieldAgoraRelation();
38
        $this->relation = $relation->firstOrFail();
39
40
        $this->mailchimp = new \Mailchimp\Mailchimp($this->mail_api_key);
41
    }
42
43 View Code Duplication
    public function getLists()
44
    {
45
        try {
46
            $result = $this->mailchimp->request('lists');
47
48
            return $result;
49
        } catch (Exception $ex) {
50
            dd($ex);
51
52
            return redirect()->back()->with('fails', $ex->getMessage());
53
        }
54
    }
55
56 View Code Duplication
    public function getListById()
57
    {
58
        try {
59
            $result = $this->mailchimp->request("lists/$this->list_id");
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 addSubscriber($email)
70
    {
71
        try {
72
            $merge_fields = $this->field($email);
73
            //dd($merge_fields);
74
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
75
                'status'        => $this->mailchimp_set->subscribe_status,
76
                'email_address' => $email,
77
                'merge_fields'  => $merge_fields,
78
            ]);
79
80
            return $result;
81
        } catch (Exception $ex) {
82
            $exe = json_decode($ex->getMessage(), true);
83
            if ($exe['status'] == 400) {
84
                throw new Exception("$email is already subscribed to newsletter", 400);
85
            }
86
        }
87
    }
88
89
    public function addSubscriberByClientPanel(Request $request)
90
    {
91
        $this->validate($request, [
92
            'email' => 'required|email',
93
        ]);
94
        try {
95
            $email = $request->input('email');
96
            $result = $this->mailchimp->post("lists/$this->list_id/members", [
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
                'status'        => $this->mailchimp_set->subscribe_status,
98
                'email_address' => $email,
99
100
            ]);
101
102
            return redirect()->back()->with('success', 'email added to mailchimp');
103
        } catch (Exception $ex) {
104
            $exe = json_decode($ex->getMessage(), true);
105
            if ($exe['status'] == 400) {
106
                $error = "$email is already subscribed to newsletter";
107
108
                return redirect()->back()->with('warning', $error);
109
            }
110
111
            return redirect()->back()->with('fails', $ex->getMessage());
112
        }
113
    }
114
115
    public function field($email)
116
    {
117
        try {
118
            $user = new User();
119
            $user = $user->where('email', $email)->first();
120
            if ($user) {
121
                $relation = $this->relation;
122
                $merge_fields = [];
123
                if ($relation->first_name) {
124
                    //dd($user->first_name);
125
                    $merge_fields[$relation->first_name] = $user->first_name;
126
                }
127
                if ($relation->last_name) {
128
                    $merge_fields[$relation->last_name] = $user->last_name;
129
                }
130
                if ($relation->company) {
131
                    $merge_fields[$relation->company] = $user->company;
132
                }
133
                if ($relation->mobile) {
134
                    $merge_fields[$relation->mobile] = $user->mobile;
135
                }
136
                if ($relation->address) {
137
                    $merge_fields[$relation->address] = $user->address;
138
                }
139
                if ($relation->town) {
140
                    $merge_fields[$relation->town] = $user->town;
141
                }
142
                if ($relation->state) {
143
                    $merge_fields[$relation->state] = $user->state;
144
                }
145
                if ($relation->zip) {
146
                    $merge_fields[$relation->zip] = $user->zip;
147
                }
148
                if ($relation->active) {
149
                    $merge_fields[$relation->active] = $user->active;
150
                }
151
                if ($relation->role) {
152
                    $merge_fields[$relation->role] = $user->role;
153
                }
154
155
                return $merge_fields;
156
            } else {
157
                return redirect()->back()->with('fails', 'user not found');
158
            }
159
        } catch (Exception $ex) {
160
            //dd($ex);
161
            return redirect()->back()->with('fails', $ex->getMessage());
162
        }
163
    }
164
165 View Code Duplication
    public function getMergeFields()
166
    {
167
        try {
168
            $result = $this->mailchimp->get("lists/$this->list_id/merge-fields");
169
170
            return $result;
171
        } catch (Exception $ex) {
172
            dd($ex);
173
174
            return redirect()->back()->with('fails', $ex->getMessage());
175
        }
176
    }
177
178
    public function addFieldsToAgora()
179
    {
180
        try {
181
            $fields = $this->getMergeFields($this->list_id);
0 ignored issues
show
Unused Code introduced by
The call to MailChimpController::getMergeFields() has too many arguments starting with $this->list_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
182
            //dd($fields);
183
            $mailchimp_field_in_agora = $this->mailchimp_field_model->get();
184
            if (count($mailchimp_field_in_agora) > 0) {
185
                foreach ($mailchimp_field_in_agora as $field) {
186
                    $field->delete();
187
                }
188
            }
189
            foreach ($fields['merge_fields'] as $key => $value) {
190
                $merge_id = $value->merge_id;
191
                $name = $value->name;
192
                $type = $value->type;
193
                $required = $value->required;
194
                $list_id = $value->list_id;
195
                $tag = $value->tag;
196
197
                $this->mailchimp_field_model->create([
198
                    'merge_id' => $merge_id,
199
                    'tag'      => $tag,
200
                    'name'     => $name,
201
                    'type'     => $type,
202
                    'required' => $required,
203
                    'list_id'  => $list_id,
204
                ]);
205
            }
206
        } catch (Exception $ex) {
207
            dd($ex);
208
209
            return redirect()->back()->with('fails', $ex->getMessage());
210
        }
211
    }
212
213
    public function addListsToAgora()
214
    {
215
        try {
216
            $lists = $this->getLists();
217
            $agora_lists = $this->lists->get();
218
            if (count($agora_lists) > 0) {
219
                foreach ($agora_lists as $agora) {
220
                    $agora->delete();
221
                }
222
            }
223
            foreach ($lists['lists'] as $list) {
224
                $name = $list->name;
225
                $list_id = $list->id;
226
                $this->lists->create([
227
                    'name'    => $name,
228
                    'list_id' => $list_id,
229
                ]);
230
            }
231
            //return redirect()->back()->with('success', \Lang::get('message.mailchimp-list-added-to-agora'));
232
        } catch (Exception $ex) {
233
            dd($ex);
234
235
            return redirect()->back()->with('fails', $ex->getMessage());
236
        }
237
    }
238
239
    public function mailChimpSettings()
240
    {
241
        try {
242
            $set = $this->mailchimp_set;
243
            $lists = $this->lists->lists('name', 'list_id')->toArray();
244
245
            return view('themes.default1.common.mailchimp.settings', compact('set', 'lists'));
246
        } catch (Exception $ex) {
247
            dd($ex);
248
249
            return redirect()->back()->with('fails', $ex->getMessage());
250
        }
251
    }
252
253
    public function postMailChimpSettings(Request $request)
254
    {
255
        $this->validate($request, [
256
            'api_key' => 'required',
257
            //'list_id'=>'required',
258
        ]);
259
        try {
260
            $this->mailchimp_set->fill($request->input())->save();
261
            $this->addListsToAgora();
262
263
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
264
        } catch (Exception $ex) {
265
            dd($ex);
266
267
            return redirect()->back()->with('fails', $ex->getMessage());
268
        }
269
    }
270
271 View Code Duplication
    public function mapField()
272
    {
273
        try {
274
            $model = $this->relation;
275
            $this->addFieldsToAgora();
276
            $mailchimp_fields = $this->mailchimp_field_model->where('list_id', $this->list_id)->lists('name', 'tag')->toArray();
277
278
            return view('themes.default1.common.mailchimp.map', compact('mailchimp_fields', 'model'));
279
        } catch (Exception $ex) {
280
            dd($ex);
281
282
            return redirect()->back()->with('fails', $ex->getMessage());
283
        }
284
    }
285
286 View Code Duplication
    public function postMapField(Request $request)
287
    {
288
        try {
289
            $this->relation->fill($request->input())->save();
290
291
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
292
        } catch (Exception $ex) {
293
            dd($ex);
294
295
            return redirect()->back()->with('fails', $ex->getMessage());
296
        }
297
    }
298
}
299