Completed
Push — master ( c5b239...344269 )
by Ben
03:28 queued 01:04
created

MailingList   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 25
c 6
b 1
f 1
lcom 1
cbo 3
dl 0
loc 210
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A id() 0 4 1
A isSubscribed() 0 15 3
B subscribe() 0 29 5
A subscriberInfo() 0 13 3
B updateSubscriber() 0 27 6
A unsubscribe() 0 17 4
A interestCategories() 0 6 1
A interests() 0 6 1
1
<?php
2
3
namespace Easychimp;
4
5
use Mailchimp\Mailchimp;
6
7
class MailingList
8
{
9
10
    /** @var Mailchimp */
11
    protected $api;
12
13
    /** @var string */
14
    protected $id;
15
16
    /** @var string */
17
    protected $support;
18
19
    public function __construct(Mailchimp $api, Support $support, $id)
20
    {
21
        $this->api = $api;
22
        $this->support = $support;
0 ignored issues
show
Documentation Bug introduced by
It seems like $support of type object<Easychimp\Support> is incompatible with the declared type string of property $support.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
        $this->id = $id;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function id()
30
    {
31
        return $this->id;
32
    }
33
34
    /**
35
     * @param $email
36
     *
37
     * @throws \Exception
38
     *
39
     * @return boolean
40
     */
41
    public function isSubscribed($email)
42
    {
43
        try {
44
            $result = $this->api->get('lists/'.$this->id().'/members/'.$this->support->hashEmail($email));
0 ignored issues
show
Bug introduced by
The method hashEmail cannot be called on $this->support (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
46
            return $result->get('status') == 'subscribed';
47
        } catch (\Exception $e) {
48
            # Email address isn't on this list
49
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
50
                return false;
51
            }
52
53
            throw $e;
54
        }
55
    }
56
57
    /**
58
     * @param string        $email
59
     * @param string        $firstName
60
     * @param string        $lastName
61
     * @param array|object  $interests  Properties/Keys are interest ids and values are boolean
62
     * @param array         $extras     Additional fields to be passed to the Mailchimp API
63
     *
64
     * @throws \Exception
65
     *
66
     * @return boolean
67
     */
68
    public function subscribe(
69
        $email,
70
        $firstName = null,
71
        $lastName = null,
72
        $interests = null,
73
        array $extras = []
74
    ) {
75
        $mergeFields = [];
76
        if ($firstName !== null) {
77
            $mergeFields['FNAME'] = $firstName;
78
        }
79
80
        if ($lastName !== null) {
81
            $mergeFields['LNAME'] = $lastName;
82
        }
83
        $data = array_merge([
84
            'email_address' => $email,
85
            'status'        => 'subscribed',
86
            'merge_fields'  => (object) $mergeFields
87
        ], $extras);
88
89
        if ($interests !== null) {
90
            $data['interests'] = (object) $interests;
91
        }
92
93
        $result = $this->api->post('lists/'.$this->id().'/members', $data);
94
95
        return $result->has('id') && strlen($result->get('id')) > 0;
96
    }
97
98
    /**
99
     * @param $email
100
     *
101
     * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
102
     *
103
     * @throws EmailAddressNotSubscribed
104
     * @throws \Exception
105
     *
106
     * @return \Illuminate\Support\Collection Info about the subscriber
107
     */
108
    public function subscriberInfo($email)
109
    {
110
        try {
111
            return $this->api->get('lists/'.$this->id().'/members/'.$this->support->hashEmail($email));
0 ignored issues
show
Bug introduced by
The method hashEmail cannot be called on $this->support (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
112
        } catch (\Exception $e) {
113
            # Email address isn't on this list
114
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
115
                throw new EmailAddressNotSubscribed;
116
            }
117
118
            throw $e;
119
        }
120
    }
121
122
    /**
123
     * Updates a subscriber if the email address is already
124
     * on the list, or create the subscriber
125
     *
126
     * @param string        $email
127
     * @param string        $firstName
128
     * @param string        $lastName
129
     * @param array|object  $interests  Properties/Keys are interest ids and values are boolean
130
     * @param array         $extras     Additional fields to be passed to the Mailchimp API
131
     *
132
     * @throws EmailAddressNotSubscribed
133
     * @throws \Exception
134
     *
135
     * @return boolean
136
     */
137
    public function updateSubscriber(
138
        $email,
139
        $firstName = null,
140
        $lastName = null,
141
        $interests = null,
142
        array $extras = []
143
    ) {
144
        $data = $extras;
145
        $mergeFields = [];
146
        if ($firstName !== null) {
147
            $mergeFields['FNAME'] = $firstName;
148
        }
149
        if ($lastName !== null) {
150
            $mergeFields['LNAME'] = $lastName;
151
        }
152
153
        if (count($mergeFields) > 0) {
154
            $data['merge_fields'] = (object) $mergeFields;
155
        }
156
        if ($interests !== null) {
157
            $data['interests'] = (object) $interests;
158
        }
159
160
        $result = $this->api->put('lists/'.$this->id().'/members/'.$this->support->hashEmail($email), $data);
0 ignored issues
show
Bug introduced by
The method hashEmail cannot be called on $this->support (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
161
162
        return $result->has('id') && strlen($result->get('id')) > 0;
163
    }
164
165
    /**
166
     * @param $email
167
     *
168
     * @throws EmailAddressNotSubscribed
169
     * @throws \Exception
170
     *
171
     * @return boolean
172
     */
173
    public function unsubscribe($email)
174
    {
175
        try {
176
            $result = $this->api->patch('lists/'.$this->id().'/members/'.$this->support->hashEmail($email), [
0 ignored issues
show
Bug introduced by
The method hashEmail cannot be called on $this->support (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177
                'status' => 'unsubscribed'
178
            ]);
179
180
            return $result->has('id') && strlen($result->get('id')) > 0;
181
        } catch (\Exception $e) {
182
            # Email address isn't on this list
183
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
184
                throw new EmailAddressNotSubscribed;
185
            }
186
187
            throw $e;
188
        }
189
    }
190
191
    /**
192
     * @throws \Exception
193
     *
194
     * @return array
195
     */
196
    public function interestCategories()
197
    {
198
        $result = $this->api->get('lists/'.$this->id().'/interest-categories');
199
200
        return $result->get('categories');
201
    }
202
203
    /**
204
     * @param $interestCategoryId
205
     *
206
     * @throws \Exception
207
     *
208
     * @return array
209
     */
210
    public function interests($interestCategoryId)
211
    {
212
        $result = $this->api->get('lists/'.$this->id().'/interest-categories/'.$interestCategoryId.'/interests');
213
214
        return $result->get('interests');
215
    }
216
}