Completed
Push — master ( f9d716...4c8558 )
by Ben
08:54 queued 04:26
created

MailingList::unsubscribe()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 8
nop 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     $interests  Array of interest ids
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
        array $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) array_flip($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
     * @throws EmailAddressNotSubscribed
102
     * @throws \Exception
103
     *
104
     * @return \Illuminate\Support\Collection Info about the subscriber
105
     */
106
    public function subscriberInfo($email)
107
    {
108
        try {
109
            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...
110
        } catch (\Exception $e) {
111
            # Email address isn't on this list
112
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
113
                throw new EmailAddressNotSubscribed;
114
            }
115
116
            throw $e;
117
        }
118
    }
119
120
    /**
121
     * @param string    $email
122
     * @param string    $firstName
123
     * @param string    $lastName
124
     * @param array     $interests  Array of interest ids
125
     * @param array     $extras     Additional fields to be passed to the Mailchimp API
126
     *
127
     * @throws EmailAddressNotSubscribed
128
     * @throws \Exception
129
     *
130
     * @return boolean
131
     */
132
    public function updateSubscriber(
133
        $email,
134
        $firstName = null,
135
        $lastName = null,
136
        array $interests = null,
137
        array $extras = []
138
    ) {
139
        try {
140
            $data = $extras;
141
            $mergeFields = [];
142
            if ($firstName !== null) {
143
                $mergeFields['FNAME'] = $firstName;
144
            }
145
            if ($lastName !== null) {
146
                $mergeFields['LNAME'] = $lastName;
147
            }
148
149
            if (count($mergeFields) > 0) {
150
                $data['merge_fields'] = (object) $mergeFields;
151
            }
152
            if ($interests !== null) {
153
                $data['interests'] = (object) array_flip($interests);
154
            }
155
156
            $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...
157
158
            return $result->has('id') && strlen($result->get('id')) > 0;
159
        } catch (\Exception $e) {
160
            # Email address isn't on this list
161
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
162
                throw new EmailAddressNotSubscribed;
163
            }
164
165
            throw $e;
166
        }
167
    }
168
169
    /**
170
     * @param $email
171
     *
172
     * @throws EmailAddressNotSubscribed
173
     * @throws \Exception
174
     *
175
     * @return boolean
176
     */
177
    public function unsubscribe($email)
178
    {
179
        try {
180
            $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...
181
                'status' => 'unsubscribed'
182
            ]);
183
184
            return $result->has('id') && strlen($result->get('id')) > 0;
185
        } catch (\Exception $e) {
186
            # Email address isn't on this list
187
            if (str_contains($e->getMessage(), 'Resource Not Found')) {
188
                throw new EmailAddressNotSubscribed;
189
            }
190
191
            throw $e;
192
        }
193
    }
194
195
    /**
196
     * @throws \Exception
197
     *
198
     * @return array
199
     */
200
    public function interestCategories()
201
    {
202
        $result = $this->api->get('lists/'.$this->id().'/interest-categories');
203
204
        return $result->get('categories');
205
    }
206
207
    /**
208
     * @param $interestCategoryId
209
     *
210
     * @throws \Exception
211
     *
212
     * @return array
213
     */
214
    public function interests($interestCategoryId)
215
    {
216
        $result = $this->api->get('lists/'.$this->id().'/interest-categories/'.$interestCategoryId.'/interests');
217
218
        return $result->get('interests');
219
    }
220
}