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

BaseMailChimpController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 28
eloc 63
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMergeFields() 0 8 2
B updateSubscriberForPaidProduct() 0 34 11
A getListById() 0 8 2
A getLists() 0 8 2
B updateSubscriberForFreeProduct() 0 36 11
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