1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
4
|
|
|
* @date 05 October 2016 |
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
6
|
|
|
*/ |
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
8
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Project; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Provides methods for working with Optimizely projects. |
13
|
|
|
*/ |
14
|
|
|
class Projects |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Optimizely API Client. |
18
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct($client) |
26
|
|
|
{ |
27
|
4 |
|
$this->client = $client; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get a list of all the Projects in your account, with associated metadata. |
32
|
|
|
* @param integer $page Page number (zero-based). |
33
|
|
|
* @param integer $perPage Count of projects per page (10 by default). |
34
|
|
|
* @return array |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
1 |
View Code Duplication |
public function listAll($page=0, $perPage=10) |
|
|
|
|
38
|
|
|
{ |
39
|
1 |
|
if ($page<0) { |
40
|
|
|
throw new \Exception('Invalid page number passed'); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
if ($perPage<0 || $perPage>100) { |
44
|
|
|
throw new \Exception('Invalid page size passed'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$response = $this->client->sendApiRequest('/projects', |
48
|
|
|
array( |
49
|
1 |
|
'page'=>$page, |
50
|
|
|
'per_page'=>$perPage |
51
|
1 |
|
)); |
52
|
|
|
|
53
|
1 |
|
$projects = array(); |
54
|
1 |
|
foreach ($response as $projectInfo) { |
55
|
1 |
|
$project = new Project($projectInfo); |
56
|
1 |
|
$projects[] = $project; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
return $projects; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get metadata for a single Project. |
64
|
|
|
* @param integer $projectId |
65
|
|
|
* @return Project |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
1 |
|
public function get($projectId) |
69
|
|
|
{ |
70
|
1 |
|
if (!is_int($projectId)) { |
71
|
|
|
throw new \Exception("Integer project ID expected, while got '$projectId'"); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
if ($projectId<0) { |
75
|
|
|
throw new \Exception("A positive project ID expected"); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$response = $this->client->sendApiRequest("/projects/$projectId"); |
79
|
|
|
|
80
|
1 |
|
$project = new Project($response); |
81
|
|
|
|
82
|
1 |
|
return $project; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create a new Project in your account. |
87
|
|
|
* @param Project $project Project meta information. |
88
|
|
|
* @return Project Created project. |
89
|
|
|
*/ |
90
|
1 |
View Code Duplication |
public function create($project) |
|
|
|
|
91
|
|
|
{ |
92
|
1 |
|
if (!($project instanceOf Project)) { |
93
|
|
|
throw new \Exception("Expected argument of type Project"); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$postData = $project->toArray(); |
97
|
|
|
|
98
|
1 |
|
$response = $this->client->sendApiRequest("/projects", array(), 'POST', |
99
|
1 |
|
$postData, array(201)); |
100
|
|
|
|
101
|
1 |
|
$project = new Project($response); |
102
|
|
|
|
103
|
1 |
|
return $project; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update a Project. |
108
|
|
|
* @param integer $projectId |
109
|
|
|
* @param Project $project |
110
|
|
|
* @return Project Updated project. |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
1 |
View Code Duplication |
public function update($projectId, $project) |
|
|
|
|
114
|
|
|
{ |
115
|
1 |
|
if (!is_int($projectId)) { |
116
|
|
|
throw new \Exception("Integer project ID expected, while got '$projectId'"); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
if ($projectId<0) { |
120
|
|
|
throw new \Exception("A positive project ID expected"); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
if (!($project instanceOf Project)) { |
124
|
|
|
throw new \Exception("Expected argument of type Project"); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$postData = $project->toArray(); |
128
|
|
|
|
129
|
1 |
|
$response = $this->client->sendApiRequest("/projects/$projectId", array(), |
130
|
1 |
|
'PATCH', $postData, array(200)); |
131
|
|
|
|
132
|
1 |
|
$project = new Project($response); |
133
|
|
|
|
134
|
1 |
|
return $project; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
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.