CampaignMonitorSubscriberGateway::hasStatus()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 17
c 5
b 0
f 0
dl 0
loc 26
rs 9.3888
cc 5
nc 8
nop 3
1
<?php
2
3
namespace MailMotor\Bundle\CampaignMonitorBundle\Gateway;
4
5
use MailMotor\Bundle\MailMotorBundle\Gateway\SubscriberGateway;
6
use MailMotor\Bundle\MailMotorBundle\Helper\Subscriber;
7
8
/**
9
 * CampaignMonitor Subscriber Gateway
10
 *
11
 * @author Jeroen Desloovere <[email protected]>
12
 */
13
class CampaignMonitorSubscriberGateway implements SubscriberGateway
14
{
15
    /** @var \CS_REST_Subscribers - The external CreateSend API for CampaignMonitor */
16
    protected $api;
17
18
    /** @var string */
19
    protected $listId;
20
21
    public function __construct(string $apiKey, string $listId)
22
    {
23
        $this->listId = $listId;
24
25
        // Define API
26
        $this->api = new \CS_REST_Subscribers(
27
            $listId,
28
            array(
29
                'api_key' => $apiKey,
30
            )
31
        );
32
    }
33
34
    public function exists(string $email, string $listId): bool
35
    {
36
        try {
37
            // Will set list id when it's different then the default listId
38
            $this->setListId($listId);
39
40
            /** @var \CS_REST_Wrapper_Result $result A successful response will be empty */
41
            $result = $this->api->get($email);
42
43
            return $result->was_successful();
44
        } catch (\Exception $e) {
45
            return false;
46
        }
47
    }
48
49
    public function getInterests(string $listId): array
50
    {
51
        // Campaign monitor has no interests functionality
52
        return array();
53
    }
54
55
    public function hasStatus(string $email, string $listId, string $status): bool
56
    {
57
        try {
58
            // Will set list id when it's different then the default listId
59
            $this->setListId($listId);
60
61
            /** @var \CS_REST_Wrapper_Result $result A successful response will be empty */
62
            $result = $this->api->get($email);
63
64
            if (!property_exists($result->response, 'State')) {
65
                return false;
66
            }
67
68
            switch ($status) {
69
                case Subscriber::MEMBER_STATUS_UNSUBSCRIBED:
70
                    return (in_array($result->response->State, array('Unconfirmed', 'Unsubscribed', 'Bounced', 'Deleted')));
71
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72
                case Subscriber::MEMBER_STATUS_SUBSCRIBED:
73
                    return ($result->response->State === 'Active');
74
                    break;
75
                default:
76
                    return false;
77
                    break;
78
            }
79
        } catch (\Exception $e) {
80
            return false;
81
        }
82
    }
83
84
    public function ping(string $listId): bool
85
    {
86
        try {
87
            // Will set list id when it's different then the default listId
88
            $this->setListId($listId);
89
90
            /** @var \CS_REST_Wrapper_Result $result A successful response will be empty */
91
            $result = $this->api->get('[email protected]');
92
93
            return $result->http_status_code !== 401;
94
        } catch (\Exception $e) {
95
            return false;
96
        }
97
    }
98
99
    /**
100
     * Subscribe
101
     *
102
     * @param string $email
103
     * @param string $listId
104
     * @param string $language
105
     * @param array $mergeFields
106
     * @param array $interests The array is like: ['9AS489SQF' => true, '4SDF8S9DF1' => false]
107
     * @param bool $doubleOptin Members need to validate their emailAddress before they get added to the list
108
     * @return boolean
109
     */
110
    public function subscribe(
111
        string $email,
112
        string $listId,
113
        string $language,
114
        array $mergeFields,
115
        array $interests,
116
        bool $doubleOptin
117
    ): bool {
118
        // Will set list id when it's different then the default listId
119
        $this->setListId($listId);
120
121
        // Define name
122
        $name = array_key_exists('FNAME', $mergeFields) ? $mergeFields['FNAME'] : '';
123
124
        // Init custom fields
125
        $customFields = array();
126
127
        // Loop all merge fields
128
        foreach ($mergeFields as $key => $value) {
129
            // Add custom field
130
            $customFields[] = [
131
                'Key' => $key,
132
                'Value' => $value,
133
            ];
134
        }
135
136
        /** @var \CS_REST_Wrapper_Result $result A successful response will be empty */
137
        $result = $this->api->add(array(
138
            'EmailAddress' => $email,
139
            'Name' => $name,
140
            'CustomFields' => $customFields,
141
            'Resubscribe' => true,
142
            'RestartSubscriptionBasedAutoResponders' => $doubleOptin,
143
        ));
144
145
        return $result->was_successful();
146
    }
147
148
    public function unsubscribe(string $email, string $listId): bool
149
    {
150
        // Will set list id when it's different then the default listId
151
        $this->setListId($listId);
152
153
        /** @var \CS_REST_Wrapper_Result $result A successful response will be empty */
154
        $result = $this->api->unsubscribe($email);
155
156
        return $result->was_successful();
157
    }
158
159
    private function setListId(string $listId): void
160
    {
161
        // We only set the list id, when another list id is given
162
        if ($listId !== $this->listId) {
163
            // Set list id
164
            $this->api->set_list_id($listId);
165
        }
166
    }
167
}
168