Passed
Push — main ( dd9121...32d064 )
by Yaroslav
15:45
created

CampaignMonitor::addMemberToList()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 40
ccs 0
cts 34
cp 0
crap 30
rs 9.1768
1
<?php
2
3
namespace MailMarketing\Drivers;
4
5
use Illuminate\Support\Arr;
6
use MailMarketing\Entities\CampaignMonitorResponse;
7
use MailMarketing\Entities\ResponseInterface;
8
use MailMarketing\MailMarketingException;
9
10
/**
11
 * @see https://www.campaignmonitor.com/api/
12
 */
13
class CampaignMonitor implements MailMarketingInterface
14
{
15
16
    protected array $auth;
17
    protected array $config;
18
19 2
    public function __construct(array $config)
20
    {
21 2
        if (!($config['key'] ?? false)) {
22 1
            throw new \InvalidArgumentException('CampaignMonitor API key not present');
23
        }
24
25 1
        $this->auth = ['api_key' => $config['key']];
26 1
        unset($config['key']);
27
28 1
        $this->config = $config;
29
    }
30
31
32
    /**
33
     * @param string|null $type
34
     * @return \CS_REST_Wrapper_Base|\CS_REST_Lists|\CS_REST_Subscribers
35
     */
36 1
    public function makeClient(?string $type = null): \CS_REST_Wrapper_Base
37
    {
38 1
        return match ($type) {
39 1
            'lists'       => new \CS_REST_Lists('', $this->auth),
40 1
            'subscribers' => new \CS_REST_Subscribers('', $this->auth),
41 1
            default       => throw new \InvalidArgumentException("Client type [{$type}] is invalid.")
42 1
        };
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 1
    public function client(?string $type = null)
49
    {
50 1
        return $this->makeClient($type);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 1
    public function addList(string $name, array $data = []): ResponseInterface
57
    {
58 1
        $client = Arr::get($data, 'client_id') ?: Arr::get($this->config, 'client_id');
59 1
        if (!$client) {
60
            throw new \InvalidArgumentException('Client is required.');
61
        }
62 1
        $config = array_merge(Arr::get($this->config, 'list'), $data, ['Title' => $name]);
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Arr::...($this->config, 'list') can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $config = array_merge(/** @scrutinizer ignore-type */ Arr::get($this->config, 'list'), $data, ['Title' => $name]);
Loading history...
63
64
65 1
        return CampaignMonitorResponse::init($this->client('lists')->create($client, $config));
0 ignored issues
show
Bug introduced by
The method create() does not exist on CS_REST_Wrapper_Base. It seems like you code against a sub-type of CS_REST_Wrapper_Base such as CS_REST_Campaigns or CS_REST_Lists or CS_REST_Templates or CS_REST_Clients or CS_REST_Segments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        return CampaignMonitorResponse::init($this->client('lists')->/** @scrutinizer ignore-call */ create($client, $config));
Loading history...
66
    }
67
68
    public function addMembersToList($listId, $members, array $data = []): ResponseInterface
69
    {
70
        $subscribers = [];
71
        foreach ($members as $member) {
72
            $subscribers[] = [
73
                'EmailAddress' => Arr::get($member, 'email', ''),
74
                'Name'         => implode(' ', array_filter([
75
                    Arr::get($member, 'first_name', ''),
76
                    Arr::get($member, 'last_name', ''),
77
                ])),
78
                'MobileNumber' => Arr::get($member, 'phone', ''),
79
                'CustomFields' => [
80
                    [
81
                        'Key'   => 'address',
82
                        'Value' => Arr::get($member, 'address', ''),
83
                        'Clear' => !Arr::get($member, 'address', ''),
84
                    ],
85
                ],
86
                'ConsentToTrack'   => 'Yes',
87
                'ConsentToSendSms' => 'Yes',
88
            ];
89
        }
90
91
        $client = $this->client('subscribers');
92
        $client->set_list_id($listId);
0 ignored issues
show
Bug introduced by
The method set_list_id() does not exist on CS_REST_Wrapper_Base. It seems like you code against a sub-type of CS_REST_Wrapper_Base such as CS_REST_Subscribers or CS_REST_Lists. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        $client->/** @scrutinizer ignore-call */ 
93
                 set_list_id($listId);
Loading history...
93
94
        return CampaignMonitorResponse::init($client->import($subscribers, Arr::get($data, 'resubscribe', true)));
0 ignored issues
show
Bug introduced by
The method import() does not exist on CS_REST_Wrapper_Base. It seems like you code against a sub-type of CS_REST_Wrapper_Base such as CS_REST_Subscribers. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        return CampaignMonitorResponse::init($client->/** @scrutinizer ignore-call */ import($subscribers, Arr::get($data, 'resubscribe', true)));
Loading history...
95
    }
96
97
    public function addMemberToList($listId, $member, array $data = []): ResponseInterface
98
    {
99
        $email = Arr::get($member, 'email');
100
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
101
            throw new MailMarketingException("Email '{$email}' not valid");
102
        }
103
        $customFields     = [];
104
        $customFieldsData = Arr::get($member, 'custom_fields', null);
105
        if (is_array($customFieldsData) && !empty($customFieldsData)) {
106
            foreach ($customFieldsData as $key => $value) {
107
                $customFields[] = [
108
                    'Key'   => $key,
109
                    'Value' => $value,
110
                ];
111
            }
112
        }
113
        $subscriber = [
114
            'EmailAddress' => $email,
115
            'Name'         => implode(' ', array_filter([
116
                Arr::get($member, 'first_name', ''),
117
                Arr::get($member, 'last_name', ''),
118
            ])),
119
            'MobileNumber' => Arr::get($member, 'phone', ''),
120
            'CustomFields' => [
121
                [
122
                    'Key'   => 'address',
123
                    'Value' => Arr::get($member, 'address', ''),
124
                    'Clear' => !Arr::get($member, 'address', ''),
125
                ],
126
                ...$customFields,
127
            ],
128
            'Resubscribe'      => Arr::get($data, 'resubscribe', true),
129
            'ConsentToTrack'   => 'Yes',
130
            'ConsentToSendSms' => 'Yes',
131
        ];
132
133
        $client = $this->client('subscribers');
134
        $client->set_list_id($listId);
135
136
        return CampaignMonitorResponse::init($client->add($subscriber));
0 ignored issues
show
Bug introduced by
The method add() does not exist on CS_REST_Wrapper_Base. It seems like you code against a sub-type of CS_REST_Wrapper_Base such as CS_REST_People or CS_REST_Subscribers or CS_REST_Administrators. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        return CampaignMonitorResponse::init($client->/** @scrutinizer ignore-call */ add($subscriber));
Loading history...
137
    }
138
139
    public function removeMemberFromList($listId, $member, array $data = []): ResponseInterface
140
    {
141
        $email = Arr::get($member, 'email');
142
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
143
            throw new MailMarketingException("Email '{$email}' not valid");
144
        }
145
146
        $client = $this->client('subscribers');
147
        $client->set_list_id($listId);
148
149
        return CampaignMonitorResponse::init($client->delete($email));
0 ignored issues
show
Bug introduced by
The method delete() does not exist on CS_REST_Wrapper_Base. Did you maybe mean delete_request()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        return CampaignMonitorResponse::init($client->/** @scrutinizer ignore-call */ delete($email));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
    }
151
152
    public function getMemberInfoForList($listId, string $email): ResponseInterface
153
    {
154
        $client = $this->client('subscribers');
155
        $client->set_list_id($listId);
156
157
        return CampaignMonitorResponse::init($client->get($email));
0 ignored issues
show
Bug introduced by
The method get() does not exist on CS_REST_Wrapper_Base. It seems like you code against a sub-type of CS_REST_Wrapper_Base such as CS_REST_People or CS_REST_Subscribers or CS_REST_Administrators or CS_REST_Lists or CS_REST_Templates or CS_REST_Clients or CS_REST_Segments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        return CampaignMonitorResponse::init($client->/** @scrutinizer ignore-call */ get($email));
Loading history...
158
    }
159
160
    public function manageMemberTags($listId, string $email, array $tags): ResponseInterface
161
    {
162
        throw new MailMarketingException('CampaignMonitor not support tags');
163
    }
164
165
    public function getMemberTags($listId, string $email): ResponseInterface
166
    {
167
        throw new MailMarketingException('CampaignMonitor not support tags');
168
    }
169
}
170