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\Resource\v2\Event; |
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\ClickEvent; |
11
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\CustomEvent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Provides methods for working with Optimizely events. |
15
|
|
|
*/ |
16
|
|
|
class Events |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Optimizely API Client. |
20
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct($client) |
28
|
|
|
{ |
29
|
6 |
|
$this->client = $client; |
30
|
6 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get all events for a Project |
34
|
|
|
* @param integer $projectId |
35
|
|
|
* @param integer $includeClassic |
36
|
|
|
* @param integer $page |
37
|
|
|
* @param integer $perPage |
38
|
|
|
* @return array[Event] |
|
|
|
|
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
1 |
View Code Duplication |
public function listAll($projectId, $includeClassic, $page=0, $perPage=10) |
|
|
|
|
42
|
|
|
{ |
43
|
1 |
|
if ($page<0) { |
44
|
|
|
throw new \Exception('Invalid page number passed'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if ($perPage<0) { |
48
|
|
|
throw new \Exception('Invalid page size passed'); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$response = $this->client->sendApiRequest('/events', |
52
|
|
|
array( |
53
|
1 |
|
'project_id'=>$projectId, |
54
|
1 |
|
'include_classic'=>$includeClassic, |
55
|
1 |
|
'page'=>$page, |
56
|
|
|
'per_page'=>$perPage |
57
|
1 |
|
)); |
58
|
|
|
|
59
|
1 |
|
$events = array(); |
60
|
1 |
|
foreach ($response as $eventInfo) { |
61
|
1 |
|
$event = new Event($eventInfo); |
62
|
1 |
|
$events[] = $event; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
return $events; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get Event by ID |
70
|
|
|
* @param type $eventId |
71
|
|
|
* @return Event |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
1 |
View Code Duplication |
public function get($eventId) |
|
|
|
|
75
|
|
|
{ |
76
|
1 |
|
if (!is_int($eventId)) { |
77
|
|
|
throw new \Exception("Integer event ID expected, while got '$eventId'"); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$response = $this->client->sendApiRequest("/events/$eventId"); |
81
|
|
|
|
82
|
1 |
|
$event = new Event($response); |
83
|
|
|
|
84
|
1 |
|
return $event; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Creates a new click event. |
89
|
|
|
* @param integer $pageId |
90
|
|
|
* @param ClickEvent $event |
91
|
|
|
*/ |
92
|
1 |
|
public function createClickEvent($pageId, $event) |
93
|
|
|
{ |
94
|
1 |
|
if (!($event instanceOf ClickEvent)) { |
95
|
|
|
throw new \Exception("Expected argument of type ClickEvent"); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$postData = $event->toArray(); |
99
|
|
|
|
100
|
1 |
|
$response = $this->client->sendApiRequest("/pages/$pageId/click_events", array(), 'POST', |
101
|
1 |
|
$postData, array(201)); |
102
|
|
|
|
103
|
1 |
|
return new ClickEvent($response); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a new custom event. |
108
|
|
|
* @param integer $pageId |
109
|
|
|
* @param CustomEvent $event |
110
|
|
|
*/ |
111
|
1 |
|
public function createCustomEvent($pageId, $event) |
112
|
|
|
{ |
113
|
1 |
|
if (!($event instanceOf CustomEvent)) { |
114
|
|
|
throw new \Exception("Expected argument of type CustomEvent"); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$postData = $event->toArray(); |
118
|
|
|
|
119
|
1 |
|
$response = $this->client->sendApiRequest("/pages/$pageId/custom_events", array(), 'POST', |
120
|
1 |
|
$postData, array(201)); |
121
|
|
|
|
122
|
1 |
|
return new CustomEvent($response); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Updates the given click event. |
127
|
|
|
* @param integer $pageId |
128
|
|
|
* @param integer $eventId |
129
|
|
|
* @param ClickEvent $event |
130
|
|
|
* @throws \Exception |
131
|
|
|
*/ |
132
|
1 |
View Code Duplication |
public function updateClickEvent($pageId, $eventId, $event) |
|
|
|
|
133
|
|
|
{ |
134
|
1 |
|
if (!($event instanceOf ClickEvent)) { |
135
|
|
|
throw new \Exception("Expected argument of type ClickEvent"); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
$postData = $event->toArray(); |
139
|
|
|
|
140
|
1 |
|
$response = $this->client->sendApiRequest("/pages/$pageId/click_events/$eventId", array(), 'PATCH', |
141
|
1 |
|
$postData, array(200)); |
142
|
|
|
|
143
|
1 |
|
return new ClickEvent($response); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Updates the given custom event. |
148
|
|
|
* @param integer $pageId |
149
|
|
|
* @param integer $eventId |
150
|
|
|
* @param CustomEvent $event |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
1 |
View Code Duplication |
public function updateCustomEvent($pageId, $eventId, $event) |
|
|
|
|
154
|
|
|
{ |
155
|
1 |
|
if (!($event instanceOf CustomEvent)) { |
156
|
|
|
throw new \Exception("Expected argument of type CustomEvent"); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$postData = $event->toArray(); |
160
|
|
|
|
161
|
1 |
|
$response = $this->client->sendApiRequest("/pages/$pageId/custom_events/$eventId", array(), 'PATCH', |
162
|
1 |
|
$postData, array(200)); |
163
|
|
|
|
164
|
1 |
|
return new CustomEvent($response); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.