Completed
Pull Request — master (#1)
by Ben
04:36
created

MailingList::exists()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 4
nop 0
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
     * @throws \Exception
36
     *
37
     * @return boolean
38
     */
39
    public function exists()
40
    {
41
        try {
42
            $this->api->get('lists/'.$this->id());
43
        } catch (\Exception $e) {
44
            if (starts_with($e->getMessage(), '{')) {
45
                $json = json_decode($e->getMessage());
46
                if ($json->status == 404) {
47
                    return false;
48
                }
49
            }
50
51
        }
52
53
        return true;
54
    }
55
56
    /**
57
     * @param $email
58
     *
59
     * @throws \Exception
60
     *
61
     * @return boolean
62
     */
63
    public function isSubscribed($email)
64
    {
65
        try {
66
            $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...
67
68
            return $result->get('status') == 'subscribed';
69
        } catch (\Exception $e) {
70
            # Email address isn't on this list
71
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
72
                return false;
73
            }
74
75
            throw $e;
76
        }
77
    }
78
79
    /**
80
     * @param string        $email
81
     * @param string        $firstName
82
     * @param string        $lastName
83
     * @param array|object  $interests  Properties/Keys are interest ids and values are boolean
84
     * @param array         $extras     Additional fields to be passed to the Mailchimp API
85
     *
86
     * @throws \Exception
87
     *
88
     * @return boolean
89
     */
90
    public function subscribe(
91
        $email,
92
        $firstName = null,
93
        $lastName = null,
94
        $interests = null,
95
        array $extras = []
96
    ) {
97
        $mergeFields = [];
98
        if ($firstName !== null) {
99
            $mergeFields['FNAME'] = $firstName;
100
        }
101
102
        if ($lastName !== null) {
103
            $mergeFields['LNAME'] = $lastName;
104
        }
105
        $data = array_merge([
106
            'email_address' => $email,
107
            'status'        => 'subscribed',
108
            'merge_fields'  => (object) $mergeFields
109
        ], $extras);
110
111
        if ($interests !== null) {
112
            $data['interests'] = (object) $interests;
113
        }
114
115
        $result = $this->api->post('lists/'.$this->id().'/members', $data);
116
117
        return $result->has('id') && strlen($result->get('id')) > 0;
118
    }
119
120
    /**
121
     * @param $email
122
     *
123
     * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
124
     *
125
     * @throws EmailAddressNotSubscribed
126
     * @throws \Exception
127
     *
128
     * @return \Illuminate\Support\Collection Info about the subscriber
129
     */
130
    public function subscriberInfo($email)
131
    {
132
        try {
133
            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...
134
        } catch (\Exception $e) {
135
            # Email address isn't on this list
136
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
137
                throw new EmailAddressNotSubscribed;
138
            }
139
140
            throw $e;
141
        }
142
    }
143
144
    /**
145
     * Updates a subscriber if the email address is already
146
     * on the list, or create the subscriber
147
     *
148
     * @param string        $email
149
     * @param string        $firstName
150
     * @param string        $lastName
151
     * @param array|object  $interests  Properties/Keys are interest ids and values are boolean
152
     * @param array         $extras     Additional fields to be passed to the Mailchimp API
153
     *
154
     * @throws EmailAddressNotSubscribed
155
     * @throws \Exception
156
     *
157
     * @return boolean
158
     */
159
    public function updateSubscriber(
160
        $email,
161
        $firstName = null,
162
        $lastName = null,
163
        $interests = null,
164
        array $extras = []
165
    ) {
166
        $data = array_merge([
167
            'status_if_new' => 'subscribed',
168
            'email_address' => $email
169
        ], $extras);
170
        $mergeFields = [];
171
        if ($firstName !== null) {
172
            $mergeFields['FNAME'] = $firstName;
173
        }
174
        if ($lastName !== null) {
175
            $mergeFields['LNAME'] = $lastName;
176
        }
177
178
        if (count($mergeFields) > 0) {
179
            $data['merge_fields'] = (object) $mergeFields;
180
        }
181
        if ($interests !== null) {
182
            $data['interests'] = (object) $interests;
183
        }
184
185
        $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...
186
187
        return $result->has('id') && strlen($result->get('id')) > 0;
188
    }
189
190
    /**
191
     * @param $email
192
     *
193
     * @throws EmailAddressNotSubscribed
194
     * @throws \Exception
195
     *
196
     * @return boolean
197
     */
198
    public function unsubscribe($email)
199
    {
200
        try {
201
            $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...
202
                'status' => 'unsubscribed'
203
            ]);
204
205
            return $result->has('id') && strlen($result->get('id')) > 0;
206
        } catch (\Exception $e) {
207
            # Email address isn't on this list
208
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
209
                throw new EmailAddressNotSubscribed;
210
            }
211
212
            throw $e;
213
        }
214
    }
215
216
    /**
217
     * @throws \Exception
218
     *
219
     * @return array
220
     */
221
    public function interestCategories()
222
    {
223
        $result = $this->api->get('lists/'.$this->id().'/interest-categories');
224
225
        return $result->get('categories');
226
    }
227
228
    /**
229
     * @param $interestCategoryId
230
     *
231
     * @throws \Exception
232
     *
233
     * @return array
234
     */
235
    public function interests($interestCategoryId)
236
    {
237
        $result = $this->api->get('lists/'.$this->id().'/interest-categories/'.$interestCategoryId.'/interests');
238
239
        return $result->get('interests');
240
    }
241
}