1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TestMonitor\ActiveCampaign\Actions; |
4
|
|
|
|
5
|
|
|
use TestMonitor\ActiveCampaign\Resources\Contact; |
6
|
|
|
use TestMonitor\ActiveCampaign\Resources\ContactsList; |
7
|
|
|
|
8
|
|
|
trait ManagesLists |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Returns all lists |
12
|
|
|
* |
13
|
|
|
* @return ContactsList[] |
14
|
|
|
*/ |
15
|
|
|
public function lists() |
16
|
|
|
{ |
17
|
|
|
return $this->transformCollection( |
|
|
|
|
18
|
|
|
$this->get('lists'), |
|
|
|
|
19
|
|
|
ContactsList::class, |
20
|
|
|
'lists' |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns list by ID |
26
|
|
|
* |
27
|
|
|
* @param string $name |
|
|
|
|
28
|
|
|
* |
29
|
|
|
* @return ContactsList|null |
30
|
|
|
*/ |
31
|
|
|
public function getList($id) |
32
|
|
|
{ |
33
|
|
|
try |
34
|
|
|
{ |
35
|
|
|
$lists = $this->get('lists/' . $id); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if (isset($lists['list']) && count($lists['list'])) |
38
|
|
|
{ |
39
|
|
|
return new ContactsList($lists['list']); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
catch (\TestMonitor\ActiveCampaign\Exceptions\NotFoundException $e) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Finds list by it's name or URL-safe name |
51
|
|
|
* |
52
|
|
|
* @param string $name name of list to find |
53
|
|
|
* |
54
|
|
|
* @return null|ContactsList |
55
|
|
|
*/ |
56
|
|
|
public function findList($name) |
57
|
|
|
{ |
58
|
|
|
$lists = $this->transformCollection( |
|
|
|
|
59
|
|
|
$this->get('lists', ['query' => ['filters[name]' => $name]]), |
|
|
|
|
60
|
|
|
ContactsList::class, |
61
|
|
|
'lists' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return array_shift($lists); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates a new list |
69
|
|
|
* |
70
|
|
|
* @param string $name Name of the list to create |
71
|
|
|
* @param string $senderUrl The website URL this list is for. |
72
|
|
|
* @param array $params other options to create list |
73
|
|
|
* |
74
|
|
|
* @return ContactsList |
75
|
|
|
*/ |
76
|
|
|
public function createList($name, $senderUrl, $params = []) |
77
|
|
|
{ |
78
|
|
|
$params['name'] = $name; |
79
|
|
|
if (!isset($params['stringid'])) |
80
|
|
|
{ |
81
|
|
|
$params['stringid'] = strtolower(preg_replace('/[^A-Z0-9]+/i', '-', $name)); |
82
|
|
|
} |
83
|
|
|
$params['sender_url'] = $senderUrl; |
84
|
|
|
$params['sender_reminder'] = 'You signed up for my mailing list.'; |
85
|
|
|
|
86
|
|
|
$lists = $this->transformCollection( |
|
|
|
|
87
|
|
|
$this->post('lists', ['json' => ['list' => $params]]), |
|
|
|
|
88
|
|
|
ContactsList::class |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return array_shift($lists); |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Removes list |
97
|
|
|
* |
98
|
|
|
* @param int $id ID of the list to delete |
99
|
|
|
* |
100
|
|
|
* @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException |
101
|
|
|
*/ |
102
|
|
|
public function deleteList($id) |
103
|
|
|
{ |
104
|
|
|
$this->delete('lists/' . $id); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Adds contact to the list |
109
|
|
|
* |
110
|
|
|
* @param int $contactId ID of contact to add to list |
111
|
|
|
* @param int $listId ID of list to add contact to |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function subscribe($contactId, $listId) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->post('contactLists', ['json' => [ |
|
|
|
|
116
|
|
|
'contactList' => [ |
117
|
|
|
'list' => $listId, |
118
|
|
|
'contact' => $contactId, |
119
|
|
|
'status' => 1, |
120
|
|
|
]]]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Remove contact from the list |
125
|
|
|
* |
126
|
|
|
* @param int $contactId ID of contact to remove from list |
127
|
|
|
* @param int $listId ID of list to remove contact from |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function unsubscribe($contactId, $listId) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->post('contactLists', ['json' => [ |
|
|
|
|
132
|
|
|
'contactList' => [ |
133
|
|
|
'list' => $listId, |
134
|
|
|
'contact' => $contactId, |
135
|
|
|
'status' => 2, |
136
|
|
|
]]]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get all contacts related to the list. |
141
|
|
|
* |
142
|
|
|
* @return Contact[] |
143
|
|
|
*/ |
144
|
|
|
public function contactsByList($listId) |
145
|
|
|
{ |
146
|
|
|
return $this->transformCollection( |
|
|
|
|
147
|
|
|
$this->get('contacts', ['query' => ['listid' => $listId]]), |
|
|
|
|
148
|
|
|
Contact::class, |
149
|
|
|
'contacts' |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
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.