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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
public function findContactById($id) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$contacts = $this->transformCollection( |
|
|
|
|
55
|
|
|
$this->get('contacts', ['query' => ['id' => $id]]), |
|
|
|
|
56
|
|
|
Contact::class, |
57
|
|
|
'contacts' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return array_shift($contacts); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create new contact. |
65
|
|
|
* |
66
|
|
|
* @param string $email |
67
|
|
|
* @param string $firstName |
68
|
|
|
* @param string $lastName |
69
|
|
|
* @param int|null $orgid |
70
|
|
|
* |
71
|
|
|
* @return Contact|null |
72
|
|
|
*/ |
73
|
|
|
public function createContact($email, $firstName, $lastName, $orgid = null) |
74
|
|
|
{ |
75
|
|
|
$contacts = $this->transformCollection( |
|
|
|
|
76
|
|
|
$this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]), |
|
|
|
|
77
|
|
|
Contact::class |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return array_shift($contacts); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Find or create a contact. |
85
|
|
|
* |
86
|
|
|
* @param string $email |
87
|
|
|
* @param string $firstName |
88
|
|
|
* @param string $lastName |
89
|
|
|
* @param int|null $orgid |
90
|
|
|
* |
91
|
|
|
* @return Contact |
92
|
|
|
*/ |
93
|
|
|
public function findOrCreateContact($email, $firstName, $lastName, $orgid = null) |
94
|
|
|
{ |
95
|
|
|
$contact = $this->findContact($email); |
96
|
|
|
|
97
|
|
|
if ($contact instanceof Contact) { |
98
|
|
|
return $contact; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->createContact($email, $firstName, $lastName, $orgid); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get all automations of a contact. |
106
|
|
|
* |
107
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function contactAutomations(Contact $contact) |
112
|
|
|
{ |
113
|
|
|
return $this->transformCollection( |
|
|
|
|
114
|
|
|
$this->get("contacts/{$contact->id}/contactAutomations"), |
|
|
|
|
115
|
|
|
ContactAutomation::class, |
116
|
|
|
'contactAutomations' |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get all tags of a contact. |
122
|
|
|
* |
123
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function contactTags(Contact $contact) |
128
|
|
|
{ |
129
|
|
|
return $this->transformCollection( |
|
|
|
|
130
|
|
|
$this->get("contacts/{$contact->id}/contactTags"), |
|
|
|
|
131
|
|
|
ContactTag::class, |
132
|
|
|
'contactTags' |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Removing a automation from a contact. |
138
|
|
|
* |
139
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
140
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$contactAutomations = $this->contactAutomations($contact); |
145
|
|
|
|
146
|
|
|
$contactAutomation = current(array_filter($contactAutomations, function ($contactAutomation) use ($automation) { |
147
|
|
|
return $contactAutomation->automation == $automation->id; |
148
|
|
|
})); |
149
|
|
|
|
150
|
|
|
if (empty($contactAutomation)) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->delete("contactAutomations/{$contactAutomation->id}"); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Removing all automations from a contact. |
159
|
|
|
* |
160
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
161
|
|
|
*/ |
162
|
|
|
public function removeAllAutomationsFromContact(Contact $contact) |
163
|
|
|
{ |
164
|
|
|
$contactAutomations = $this->contactAutomations($contact); |
165
|
|
|
|
166
|
|
|
foreach ($contactAutomations as $contactAutomation) { |
167
|
|
|
$this->delete("contactAutomations/{$contactAutomation->id}"); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Removing a tag from a contact. |
173
|
|
|
* |
174
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
175
|
|
|
* @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function removeTagFromContact(Contact $contact, Tag $tag) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$contactTags = $this->contactTags($contact); |
180
|
|
|
|
181
|
|
|
$contactTag = current(array_filter($contactTags, function ($contactTag) use ($tag) { |
182
|
|
|
return $contactTag->tag == $tag->id; |
183
|
|
|
})); |
184
|
|
|
|
185
|
|
|
if (empty($contactTag)) { |
186
|
|
|
return; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->delete("contactTags/{$contactTag->id}"); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.