1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\ActiveCampaign\Actions; |
4
|
|
|
|
5
|
|
|
use TestMonitor\ActiveCampaign\Resources\Tag; |
6
|
|
|
use TestMonitor\ActiveCampaign\Resources\Contact; |
7
|
|
|
use TestMonitor\ActiveCampaign\Resources\Automation; |
8
|
|
|
use TestMonitor\ActiveCampaign\Resources\ContactTag; |
9
|
|
|
use TestMonitor\ActiveCampaign\Resources\ContactAutomation; |
10
|
|
|
|
11
|
|
|
trait ManagesContacts |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get all contacts. |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public function contacts() |
19
|
|
|
{ |
20
|
|
|
return $this->transformCollection( |
|
|
|
|
21
|
|
|
$this->get('contacts'), |
|
|
|
|
22
|
|
|
Contact::class, |
23
|
|
|
'contacts' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Find contact by email. |
29
|
|
|
* |
30
|
|
|
* @param string $email |
31
|
|
|
* |
32
|
|
|
* @return Contact|null |
33
|
|
|
*/ |
34
|
|
|
public function findContact($email) |
35
|
|
|
{ |
36
|
|
|
$contacts = $this->transformCollection( |
|
|
|
|
37
|
|
|
$this->get('contacts', ['query' => ['email' => $email]]), |
|
|
|
|
38
|
|
|
Contact::class, |
39
|
|
|
'contacts' |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
return array_shift($contacts); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Find contact by ID. |
47
|
|
|
* |
48
|
|
|
* @param int $id |
49
|
|
|
* |
50
|
|
|
* @return Contact|null |
51
|
|
|
*/ |
52
|
|
|
public function findContactById($id) |
53
|
|
|
{ |
54
|
|
|
$contact = (object) $this->get('contacts/'.$id); |
55
|
|
|
return new Contact($contact->contact); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create new contact. |
60
|
|
|
* |
61
|
|
|
* @param string $email |
62
|
|
|
* @param string $firstName |
63
|
|
|
* @param string $lastName |
64
|
|
|
* @param int|null $orgid |
65
|
|
|
* |
66
|
|
|
* @return Contact|null |
67
|
|
|
*/ |
68
|
|
|
public function createContact($email, $firstName, $lastName, $orgid = null) |
69
|
|
|
{ |
70
|
|
|
$contacts = $this->transformCollection( |
|
|
|
|
71
|
|
|
$this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]), |
|
|
|
|
72
|
|
|
Contact::class |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
return array_shift($contacts); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find or create a contact. |
80
|
|
|
* |
81
|
|
|
* @param string $email |
82
|
|
|
* @param string $firstName |
83
|
|
|
* @param string $lastName |
84
|
|
|
* @param int|null $orgid |
85
|
|
|
* |
86
|
|
|
* @return Contact |
87
|
|
|
*/ |
88
|
|
|
public function findOrCreateContact($email, $firstName, $lastName, $orgid = null) |
89
|
|
|
{ |
90
|
|
|
$contact = $this->findContact($email); |
91
|
|
|
|
92
|
|
|
if ($contact instanceof Contact) { |
93
|
|
|
return $contact; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->createContact($email, $firstName, $lastName, $orgid); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Updates a contact. |
101
|
|
|
* |
102
|
|
|
* @param Contact|int|string $id |
103
|
|
|
* @param string|null $email |
104
|
|
|
* @param string|null $firstName |
105
|
|
|
* @param string|null $lastName |
106
|
|
|
* @param null $orgid |
107
|
|
|
* |
108
|
|
|
* @return Contact|null |
109
|
|
|
*/ |
110
|
|
|
public function updateContact($id, $email, $firstName, $lastName, $orgid = null) |
111
|
|
|
{ |
112
|
|
|
$id = $this->getContactId($id); |
113
|
|
|
|
114
|
|
|
$this->put('contacts/'.$id, ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]); |
|
|
|
|
115
|
|
|
|
116
|
|
|
return $this->findContactById($id); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Deletes a contact. |
121
|
|
|
* |
122
|
|
|
* @param Contact|int|string $contact |
123
|
|
|
*/ |
124
|
|
|
public function deleteContact($contact) |
125
|
|
|
{ |
126
|
|
|
$this->delete('contacts/'.$this->getContactId($contact)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get all automations of a contact. |
131
|
|
|
* |
132
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function contactAutomations(Contact $contact) |
137
|
|
|
{ |
138
|
|
|
return $this->transformCollection( |
|
|
|
|
139
|
|
|
$this->get("contacts/{$contact->id}/contactAutomations"), |
|
|
|
|
140
|
|
|
ContactAutomation::class, |
141
|
|
|
'contactAutomations' |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get all tags of a contact. |
147
|
|
|
* |
148
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function contactTags(Contact $contact) |
153
|
|
|
{ |
154
|
|
|
return $this->transformCollection( |
|
|
|
|
155
|
|
|
$this->get("contacts/{$contact->id}/contactTags"), |
|
|
|
|
156
|
|
|
ContactTag::class, |
157
|
|
|
'contactTags' |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Removing a automation from a contact. |
163
|
|
|
* |
164
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
165
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$contactAutomations = $this->contactAutomations($contact); |
170
|
|
|
|
171
|
|
|
$contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) { |
172
|
|
|
return $contactAutomation->automation == $automation->id; |
173
|
|
|
})); |
174
|
|
|
|
175
|
|
|
if (empty($contactAutomation)) { |
176
|
|
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->delete("contactAutomations/{$contactAutomation->id}"); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Removing all automations from a contact. |
184
|
|
|
* |
185
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
186
|
|
|
*/ |
187
|
|
|
public function removeAllAutomationsFromContact(Contact $contact) |
188
|
|
|
{ |
189
|
|
|
$contactAutomations = $this->contactAutomations($contact); |
190
|
|
|
|
191
|
|
|
foreach ($contactAutomations as $contactAutomation) { |
192
|
|
|
$this->delete("contactAutomations/{$contactAutomation->id}"); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Removing a tag from a contact. |
198
|
|
|
* |
199
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
200
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
public function removeTagFromContact(Contact $contact, Tag $tag) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$contactTags = $this->contactTags($contact); |
205
|
|
|
|
206
|
|
|
$contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) { |
207
|
|
|
return $contactTag->tag == $tag->id; |
208
|
|
|
})); |
209
|
|
|
|
210
|
|
|
if (empty($contactTag)) { |
211
|
|
|
return; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->delete("contactTags/{$contactTag->id}"); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Determines the contact ID. |
219
|
|
|
* |
220
|
|
|
* @param Contact|int|string $contact |
221
|
|
|
*/ |
222
|
|
|
protected function getContactId($contact) |
223
|
|
|
{ |
224
|
|
|
if ($contact instanceof Contact) { |
225
|
|
|
return $contact->id; |
226
|
|
|
} elseif (is_numeric($contact)) { |
227
|
|
|
return (int) $contact; |
228
|
|
|
} else { |
229
|
|
|
$contact = $this->findContact($contact); |
230
|
|
|
|
231
|
|
|
return $contact->id; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.