/
ThreeScaleAuthorizeResponse.php
183 lines (160 loc) · 3.77 KB
/
ThreeScaleAuthorizeResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Defines ThreeScaleAuthorizeResponse and ThreeScaleAuthorizeResponseUsage classes.
*
* @copyright 2010 3scale networks S.L.
*/
require_once(dirname(__FILE__) . '/ThreeScaleResponse.php');
/**
* Object that wraps responses from successful authorize calls.
*/
class ThreeScaleAuthorizeResponse extends ThreeScaleResponse {
private $usageReports = array();
private $plan;
/**
* @internal Set plan name.
*/
public function setPlan($plan) {
$this->plan = $plan;
}
/**
* Get name of the plan the application is signed up to.
*
* @return string
*/
public function getPlan() {
return $this->plan;
}
/**
* @internal Add usage report entry.
*/
public function addUsageReport() {
$usageReport = new ThreeScaleAuthorizeResponseUsageReport;
array_push($this->usageReports, $usageReport);
return $usageReport;
}
/**
* Get list of usage report entries.
*
* There will be one entry per each usage limit defined on the plan the used
* is signed up to.
*
* @see ThreeScaleAuthorizeResponseUsage
*/
public function getUsageReports() {
return $this->usageReports;
}
}
/**
* Object with information about application's usage and how close it is to meeting
* the limits.
*
* One object of this class always corresponds to one usage limit defined on the plan.
*/
class ThreeScaleAuthorizeResponseUsageReport {
private $metric;
private $period;
private $periodStart;
private $periodEnd;
private $parsedPeriodStart;
private $parsedPeriodEnd;
/**
* Name of the metric the usage limit is defined for.
*
* @returns string
*/
public function getMetric() {
return $this->metric;
}
/**
* @internal set the metric.
*/
public function setMetric($metric) {
$this->metric = $metric;
return $this;
}
/**
* The period of the usage limit.
*
* @returns string
*
* This returns symbolic value: "year", "month", "week", "day", "hour" or "minute".
*/
public function getPeriod() {
return $this->period;
}
/**
* @internal set the period.
*/
public function setPeriod($period) {
$this->period = $period;
return $this;
}
/**
* Start of the current period of the usage limit, as unix timestamp.
*
* @return int (unit timestamp)
*/
public function getPeriodStart() {
if (is_null($this->parsedPeriodStart)) {
$this->parsedPeriodStart = strtotime($this->periodStart);
}
return $this->parsedPeriodStart;
}
/**
* End of the current period of the usage limit, as unix timestamp.
*
* @return int (unit timestamp)
*/
public function getPeriodEnd() {
if (is_null($this->parsedPeriodEnd)) {
$this->parsedPeriodEnd = strtotime($this->periodEnd);
}
return $this->parsedPeriodEnd;
}
/**
* @internal set start and end of the period.
*/
public function setPeriodInterval($start, $end) {
$this->periodStart = $start;
$this->periodEnd = $end;
return $this;
}
/**
* The value the application alredy spent in the current period of the usage limit.
*
* @return int
*/
public function getCurrentValue() {
return $this->currentValue;
}
/**
* @internal set current value.
*/
public function setCurrentValue($value) {
$this->currentValue = $value;
return $this;
}
/**
* Maximum value allowed by the usage limit for the current period.
*
* @return int
*/
public function getMaxValue() {
return $this->maxValue;
}
/**
* @internal set max value.
*/
public function setMaxValue($value) {
$this->maxValue = $value;
return $this;
}
/**
* Is the usage limit corresponding to this report exceeded?
*/
public function isExceeded() {
return $this->getCurrentValue() > $this->getMaxValue();
}
}
?>