1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\LiftClient\Manager; |
4
|
|
|
|
5
|
|
|
use Acquia\LiftClient\Entity\Entity; |
6
|
|
|
use Acquia\LiftClient\Entity\Goal; |
7
|
|
|
use Acquia\LiftClient\Entity\GoalAddResponse; |
8
|
|
|
use Acquia\LiftClient\Exception\LiftSdkException; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
|
11
|
|
|
class GoalManager extends ManagerBase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
protected $queryParameters = [ |
17
|
|
|
'global' => null, |
18
|
|
|
'limit_by_site' => null, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Valid boolean query strings. |
23
|
|
|
*/ |
24
|
|
|
private $validBooleanQueryStrings = [ |
25
|
|
|
'true', |
26
|
|
|
'false', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get a list of Goals. |
31
|
|
|
* |
32
|
|
|
* Example of how to structure the $options parameter: |
33
|
|
|
* <code> |
34
|
|
|
* $options = [ |
35
|
|
|
* 'global' => 'false', |
36
|
|
|
* 'limit_by_site' => 'true', |
37
|
|
|
* ]; |
38
|
|
|
* </code> |
39
|
|
|
* Note: the options "global" and "limit_by_site" work together to determine |
40
|
|
|
* what goals are coming back. Please see the truth table on following page. |
41
|
|
|
* |
42
|
|
|
* @see http://docs.decision-api.acquia.com/#goals_get |
43
|
|
|
* |
44
|
|
|
* @param array $options |
45
|
|
|
* |
46
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
47
|
|
|
* |
48
|
|
|
* @return \Acquia\LiftClient\Entity\Goal[] |
49
|
|
|
*/ |
50
|
18 |
View Code Duplication |
public function query($options = []) |
51
|
|
|
{ |
52
|
12 |
|
$url = '/goals'; |
53
|
18 |
|
$url .= $this->getQueryString($options); |
54
|
|
|
|
55
|
|
|
// Now make the request. |
56
|
6 |
|
$request = new Request('GET', $url); |
57
|
6 |
|
$data = $this->getResponseJson($request); |
58
|
|
|
|
59
|
|
|
// Get them as Goal objects |
60
|
3 |
|
$goals = []; |
61
|
3 |
|
foreach ($data as $dataItem) { |
62
|
3 |
|
$goals[] = new Goal($dataItem); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
3 |
|
return $goals; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a specific goal. |
70
|
|
|
* |
71
|
|
|
* @see http://docs.decision-api.acquia.com/#goals__goal_id__get |
72
|
|
|
* |
73
|
|
|
* @param array $id |
74
|
|
|
* |
75
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
76
|
|
|
* |
77
|
|
|
* @return \Acquia\LiftClient\Entity\Goal |
78
|
|
|
*/ |
79
|
6 |
|
public function get($id) |
80
|
|
|
{ |
81
|
6 |
|
$url = "/goals/{$id}"; |
82
|
|
|
|
83
|
|
|
// Now make the request. |
84
|
6 |
|
$request = new Request('GET', $url); |
85
|
6 |
|
$data = $this->getResponseJson($request); |
86
|
|
|
|
87
|
3 |
|
return new Goal($data); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add a goal. |
92
|
|
|
* |
93
|
|
|
* @see http://docs.decision-api.acquia.com/#goals_post |
94
|
|
|
* |
95
|
|
|
* @param \Acquia\LiftClient\Entity\Goal $goal |
96
|
|
|
* |
97
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
98
|
|
|
* |
99
|
|
|
* @return \Acquia\LiftClient\Entity\GoalAddResponse |
100
|
|
|
*/ |
101
|
9 |
|
public function add(Goal $goal) |
102
|
|
|
{ |
103
|
|
|
// goals only supports adding a list of goals |
104
|
|
|
// we do not want to support that in the SDK for consistency reasons, so |
105
|
|
|
// we convert it to an array here. |
106
|
9 |
|
$goals = new Entity([$goal->getArrayCopy()]); |
107
|
9 |
|
$body = $goals->json(); |
108
|
9 |
|
$url = '/goals'; |
109
|
9 |
|
$request = new Request('POST', $url, [], $body); |
110
|
9 |
|
$data = $this->getResponseJson($request); |
111
|
|
|
|
112
|
6 |
|
return new GoalAddResponse($data); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Deletes a goal by ID. |
117
|
|
|
* |
118
|
|
|
* @see http://docs.decision-api.acquia.com/#goals__goal_id__delete |
119
|
|
|
* |
120
|
|
|
* @param string $id |
121
|
|
|
* |
122
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
6 |
|
public function delete($id) |
127
|
|
|
{ |
128
|
6 |
|
$url = "/goals/{$id}"; |
129
|
6 |
|
$this->client->delete($url); |
130
|
|
|
|
131
|
3 |
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get query string of using the options. |
136
|
|
|
* |
137
|
|
|
* @param $options The options |
138
|
|
|
* |
139
|
|
|
* @throws \Acquia\LiftClient\Exception\LiftSdkException |
140
|
|
|
* |
141
|
|
|
* @return string The query string |
142
|
|
|
*/ |
143
|
12 |
|
protected function getQueryString($options) { |
144
|
12 |
View Code Duplication |
if (isset($options['global']) && !in_array($options['global'], $this->validBooleanQueryStrings)) { |
|
|
|
|
145
|
3 |
|
throw new LiftSdkException('The "global" parameter must be a string value of "true" or "false", or absent.'); |
146
|
|
|
} |
147
|
9 |
View Code Duplication |
if (isset($options['limit_by_site']) && !in_array($options['limit_by_site'], $this->validBooleanQueryStrings)) { |
|
|
|
|
148
|
3 |
|
throw new LiftSdkException('The "limit_by_site" parameter must be a string value of "true" or "false", or absent.'); |
149
|
|
|
} |
150
|
|
|
|
151
|
6 |
|
return parent::getQueryString($options); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.