1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
4
|
|
|
* @date 06 October 2016 |
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
6
|
|
|
*/ |
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
8
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign; |
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\CampaignResults; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides methods for working with Optimizely campaigns. |
14
|
|
|
*/ |
15
|
|
|
class Campaigns |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Optimizely API Client. |
19
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
*/ |
26
|
6 |
|
public function __construct($client) |
27
|
|
|
{ |
28
|
6 |
|
$this->client = $client; |
29
|
6 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the list of campaigns. |
33
|
|
|
* @param integer $projectId |
34
|
|
|
* @param integer $page |
35
|
|
|
* @param integer $perPage |
36
|
|
|
* @return array[Campaign] |
|
|
|
|
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
1 |
|
public function listAll($projectId, $page=0, $perPage=10) |
40
|
|
|
{ |
41
|
1 |
|
if (!is_int($projectId)) { |
42
|
|
|
throw new \Exception("Integer project ID expected, while got '$projectId'"); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if ($projectId<0) { |
46
|
|
|
throw new \Exception("Expected positive integer project ID"); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if ($page<0) { |
50
|
|
|
throw new \Exception('Invalid page number passed'); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
if ($perPage<0) { |
54
|
|
|
throw new \Exception('Invalid page size passed'); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$response = $this->client->sendApiRequest('/campaigns', |
58
|
|
|
array( |
59
|
1 |
|
'project_id'=>$projectId, |
60
|
1 |
|
'page'=>$page, |
61
|
|
|
'per_page'=>$perPage |
62
|
1 |
|
)); |
63
|
|
|
|
64
|
1 |
|
$campaigns = array(); |
65
|
1 |
|
foreach ($response as $campaignInfo) { |
66
|
1 |
|
$campaign = new Campaign($campaignInfo); |
67
|
1 |
|
$campaigns[] = $campaign; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
return $campaigns; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Reads a campaign. |
75
|
|
|
* @param integer $campaignId |
76
|
|
|
* @return Campaign |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
public function get($campaignId) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
if (!is_int($campaignId)) { |
82
|
|
|
throw new \Exception("Integer campaign ID expected, while got '$campaignId'"); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$response = $this->client->sendApiRequest("/campaigns/$campaignId"); |
86
|
|
|
|
87
|
1 |
|
$campaign = new Campaign($response); |
88
|
|
|
|
89
|
1 |
|
return $campaign; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get campaign results |
94
|
|
|
* @param integer $campaignId The id for the campaign you want results for |
95
|
|
|
* @param string $starTime The earliest time to count events in results. Defaults to the time that the campaign was first activated. |
|
|
|
|
96
|
|
|
* @param string $endTime The latest time to count events in results. Defaults to the time the campaign was last active or the current time if the campaign is still running. |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
1 |
|
public function getResults($campaignId, $startTime = null, $endTime = null) |
100
|
|
|
{ |
101
|
1 |
|
if (!is_int($campaignId)) { |
102
|
|
|
throw new \Exception("Integer campaign ID expected, while got '$campaignId'"); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
if ($campaignId<0) { |
106
|
|
|
throw new \Exception("Expected positive integer campaign ID"); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$response = $this->client->sendApiRequest("/campaigns/$campaignId/results", |
110
|
|
|
array( |
111
|
1 |
|
'campaign_id' => $campaignId, |
112
|
1 |
|
'start_time' => $startTime, |
113
|
|
|
'end_time' => $endTime |
114
|
1 |
|
)); |
115
|
|
|
|
116
|
1 |
|
$results = new CampaignResults($response); |
117
|
|
|
|
118
|
1 |
|
return $results; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create a new Campaign in your account |
123
|
|
|
* @param Campaign $campaign |
124
|
|
|
*/ |
125
|
1 |
|
public function create($campaign) |
126
|
|
|
{ |
127
|
1 |
|
if (!($campaign instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign)) { |
128
|
|
|
throw new \Exception("Expected argument of type Campaign"); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
$postData = $campaign->toArray(); |
132
|
|
|
|
133
|
1 |
|
$response = $this->client->sendApiRequest("/campaigns", array(), 'POST', |
134
|
1 |
|
$postData, array(201)); |
135
|
|
|
|
136
|
1 |
|
return new Campaign($response); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Update a Campaign |
141
|
|
|
* @param integer $campaignId |
142
|
|
|
* @param Campaign $campaign |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
1 |
|
public function update($campaignId, $campaign) |
146
|
|
|
{ |
147
|
1 |
|
if (!($campaign instanceOf \WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign)) { |
148
|
|
|
throw new \Exception("Expected argument of type Campaign"); |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
$postData = $campaign->toArray(); |
152
|
|
|
|
153
|
1 |
|
$response = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'PATCH', |
154
|
1 |
|
$postData, array(200)); |
155
|
|
|
|
156
|
1 |
|
return new Campaign($response); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Delete Campaign by ID |
161
|
|
|
* @param integer $campaignId |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
1 |
|
public function delete($campaignId) |
165
|
|
|
{ |
166
|
1 |
|
$response = $this->client->sendApiRequest("/campaigns/$campaignId", array(), 'DELETE', |
|
|
|
|
167
|
1 |
|
array(), array(200)); |
168
|
1 |
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
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.