1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
4
|
|
|
* @date 07 October 2016 |
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
6
|
|
|
*/ |
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
8
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Exception; |
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Event; |
11
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\ClickEvent; |
12
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\CustomEvent; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provides methods for working with Optimizely events. |
16
|
|
|
*/ |
17
|
|
|
class Events |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Optimizely API Client. |
21
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct($client) |
29
|
|
|
{ |
30
|
6 |
|
$this->client = $client; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get all events for a Project |
35
|
|
|
* @param integer $projectId |
36
|
|
|
* @param integer $includeClassic |
37
|
|
|
* @param integer $page |
38
|
|
|
* @param integer $perPage |
39
|
|
|
* @return Result |
40
|
|
|
* @throws Exception |
41
|
|
|
*/ |
42
|
1 |
|
public function listAll($projectId, $includeClassic, $page=1, $perPage=25) |
43
|
|
|
{ |
44
|
1 |
|
if ($page<0) { |
45
|
|
|
throw new Exception('Invalid page number passed', |
46
|
|
|
Exception::CODE_INVALID_ARG); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if ($perPage<0) { |
50
|
|
|
throw new Exception('Invalid page size passed', |
51
|
|
|
Exception::CODE_INVALID_ARG); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$result = $this->client->sendApiRequest('/events', |
55
|
|
|
array( |
56
|
1 |
|
'project_id'=>$projectId, |
57
|
1 |
|
'include_classic'=>$includeClassic, |
58
|
1 |
|
'page'=>$page, |
59
|
1 |
|
'per_page'=>$perPage |
60
|
|
|
)); |
61
|
|
|
|
62
|
1 |
|
$events = array(); |
63
|
1 |
|
foreach ($result->getDecodedJsonData() as $eventInfo) { |
64
|
1 |
|
$event = new Event($eventInfo); |
65
|
1 |
|
$events[] = $event; |
66
|
|
|
} |
67
|
1 |
|
$result->setPayload($events); |
68
|
|
|
|
69
|
1 |
|
return $result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get Event by ID |
74
|
|
|
* @param type $eventId |
75
|
|
|
* @return Result |
76
|
|
|
* @throws Exception |
77
|
|
|
*/ |
78
|
1 |
|
public function get($eventId) |
79
|
|
|
{ |
80
|
1 |
|
if (!is_int($eventId)) { |
81
|
|
|
throw new Exception("Integer event ID expected, while got '$eventId'", |
82
|
|
|
Exception::CODE_INVALID_ARG); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$result = $this->client->sendApiRequest("/events/$eventId"); |
86
|
|
|
|
87
|
1 |
|
$event = new Event($result->getDecodedJsonData()); |
88
|
1 |
|
$result->setPayload($event); |
89
|
|
|
|
90
|
1 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Creates a new click event. |
95
|
|
|
* @param integer $pageId |
96
|
|
|
* @param ClickEvent $event |
97
|
|
|
* @return Result |
98
|
|
|
* @throws Exception |
99
|
|
|
*/ |
100
|
1 |
View Code Duplication |
public function createClickEvent($pageId, $event) |
|
|
|
|
101
|
|
|
{ |
102
|
1 |
|
if (!($event instanceOf ClickEvent)) { |
103
|
|
|
throw new Exception("Expected argument of type ClickEvent", |
104
|
|
|
Exception::CODE_INVALID_ARG); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$postData = $event->toArray(); |
108
|
|
|
|
109
|
1 |
|
$result = $this->client->sendApiRequest("/pages/$pageId/events", array(), 'POST', |
110
|
1 |
|
$postData); |
111
|
|
|
|
112
|
1 |
|
$event = new ClickEvent($result->getDecodedJsonData()); |
113
|
1 |
|
$result->setPayload($event); |
114
|
|
|
|
115
|
1 |
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates a new custom event. |
120
|
|
|
* @param integer $projectId |
121
|
|
|
* @param CustomEvent $event |
122
|
|
|
* @return Result |
123
|
|
|
* @throws Exception |
124
|
|
|
*/ |
125
|
1 |
View Code Duplication |
public function createCustomEvent($projectId, $event) |
|
|
|
|
126
|
|
|
{ |
127
|
1 |
|
if (!($event instanceOf CustomEvent)) { |
128
|
|
|
throw new Exception("Expected argument of type CustomEvent", |
129
|
|
|
Exception::CODE_INVALID_ARG); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$postData = $event->toArray(); |
133
|
|
|
|
134
|
1 |
|
$result = $this->client->sendApiRequest("/projects/$projectId/custom_events", array(), 'POST', |
135
|
1 |
|
$postData); |
136
|
|
|
|
137
|
1 |
|
$event = new CustomEvent($result->getDecodedJsonData()); |
138
|
1 |
|
$result->setPayload($event); |
139
|
|
|
|
140
|
1 |
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Updates the given click event. |
145
|
|
|
* @param integer $pageId |
146
|
|
|
* @param integer $eventId |
147
|
|
|
* @param ClickEvent $event |
148
|
|
|
* @return Result |
149
|
|
|
* @throws Exception |
150
|
|
|
*/ |
151
|
1 |
View Code Duplication |
public function updateClickEvent($pageId, $eventId, $event) |
|
|
|
|
152
|
|
|
{ |
153
|
1 |
|
if (!($event instanceOf ClickEvent)) { |
154
|
|
|
throw new Exception("Expected argument of type ClickEvent", |
155
|
|
|
Exception::CODE_INVALID_ARG); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
$postData = $event->toArray(); |
159
|
|
|
|
160
|
1 |
|
$result = $this->client->sendApiRequest("/pages/$pageId/events/$eventId", array(), 'PATCH', |
161
|
1 |
|
$postData); |
162
|
|
|
|
163
|
1 |
|
$event = new ClickEvent($result->getDecodedJsonData()); |
164
|
1 |
|
$result->setPayload($event); |
165
|
|
|
|
166
|
1 |
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Updates the given custom event. |
171
|
|
|
* @param integer $projectId |
172
|
|
|
* @param integer $eventId |
173
|
|
|
* @param CustomEvent $event |
174
|
|
|
* @return Result |
175
|
|
|
* @throws Exception |
176
|
|
|
*/ |
177
|
1 |
View Code Duplication |
public function updateCustomEvent($projectId, $eventId, $event) |
|
|
|
|
178
|
|
|
{ |
179
|
1 |
|
if (!($event instanceOf CustomEvent)) { |
180
|
|
|
throw new Exception("Expected argument of type CustomEvent", |
181
|
|
|
Exception::CODE_INVALID_ARG); |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
$postData = $event->toArray(); |
185
|
|
|
|
186
|
1 |
|
$result = $this->client->sendApiRequest("/projects/$projectId/custom_events/$eventId", array(), 'PATCH', |
187
|
1 |
|
$postData); |
188
|
|
|
|
189
|
1 |
|
$event = new CustomEvent($result->getDecodedJsonData()); |
190
|
1 |
|
$result->setPayload($event); |
191
|
|
|
|
192
|
1 |
|
return $result; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.