1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ReportingCloud PHP Wrapper |
5
|
|
|
* |
6
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
7
|
|
|
* |
8
|
|
|
* @link http://www.reporting.cloud to learn more about ReportingCloud |
9
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
10
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
11
|
|
|
* @copyright © 2018 Text Control GmbH |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace TxTextControl\ReportingCloud\Console; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ReportingCloud console helper (used only for tests and demos) |
18
|
|
|
* |
19
|
|
|
* @package TxTextControl\ReportingCloud |
20
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
21
|
|
|
*/ |
22
|
|
|
class Helper |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Line length in characters (used to wrap long lines) |
26
|
|
|
*/ |
27
|
|
|
const LINE_LENGTH = 80; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Name of API key PHP constant or environmental variables |
31
|
|
|
* |
32
|
|
|
* @const REPORTING_CLOUD_API_KEY |
33
|
|
|
*/ |
34
|
|
|
const API_KEY = 'REPORTING_CLOUD_API_KEY'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Name of username PHP constant or environmental variables |
38
|
|
|
* |
39
|
|
|
* @const REPORTING_CLOUD_USERNAME |
40
|
|
|
*/ |
41
|
|
|
const USERNAME = 'REPORTING_CLOUD_USERNAME'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Name of password PHP constant or environmental variables |
45
|
|
|
* |
46
|
|
|
* @const REPORTING_CLOUD_PASSWORD |
47
|
|
|
*/ |
48
|
|
|
const PASSWORD = 'REPORTING_CLOUD_PASSWORD'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check ReportingCloud credentials, which have been defined in environment variables, otherwise terminate script |
52
|
|
|
* execution with error code 1 |
53
|
|
|
*/ |
54
|
|
|
public static function checkCredentials() |
55
|
|
|
{ |
56
|
|
|
if (null !== self::apiKey()) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (null !== self::username() && null !== self::password()) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
echo self::errorMessage(); |
65
|
|
|
die(1); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return the value of the PHP constant or environmental variable |
70
|
|
|
* |
71
|
|
|
* @param string $variable Variable |
72
|
|
|
* |
73
|
|
|
* @return null|string |
74
|
|
|
*/ |
75
|
|
|
protected static function variable($variable) |
76
|
|
|
{ |
77
|
|
|
$ret = null; |
78
|
|
|
|
79
|
|
|
if (defined($variable)) { |
80
|
|
|
$value = constant($variable); |
81
|
|
|
} else { |
82
|
|
|
$value = getenv($variable); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$value = trim($value); |
86
|
|
|
|
87
|
|
|
if (strlen($value) > 0) { |
88
|
|
|
$ret = $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $ret; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the ReportingCloud API key |
96
|
|
|
* |
97
|
|
|
* @return null|string |
98
|
|
|
*/ |
99
|
|
|
public static function apiKey() |
100
|
|
|
{ |
101
|
|
|
$apiKey = self::variable(self::API_KEY); |
102
|
|
|
|
103
|
|
|
return $apiKey; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the ReportingCloud username |
108
|
|
|
* |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
|
|
public static function username() |
112
|
|
|
{ |
113
|
|
|
$username = self::variable(self::USERNAME); |
114
|
|
|
|
115
|
|
|
return $username; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return the ReportingCloud password |
120
|
|
|
* |
121
|
|
|
* @return null|string |
122
|
|
|
*/ |
123
|
|
|
public static function password() |
124
|
|
|
{ |
125
|
|
|
$password = self::variable(self::PASSWORD); |
126
|
|
|
|
127
|
|
|
return $password; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// @codingStandardsIgnoreStart |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return error message explaining how to configure PHP constant or environmental variables |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public static function errorMessage() |
138
|
|
|
{ |
139
|
|
|
$ret |
140
|
|
|
= <<<END |
141
|
|
|
|
142
|
|
|
Error: ReportingCloud username and/or password not defined. |
143
|
|
|
|
144
|
|
|
In order to execute this script, you must first set your ReportingCloud username and password. |
145
|
|
|
|
146
|
|
|
There are two ways in which you can do this: |
147
|
|
|
|
148
|
|
|
1) Define the following PHP constants: |
149
|
|
|
|
150
|
|
|
define('REPORTING_CLOUD_USERNAME', 'your-username'); |
151
|
|
|
define('REPORTING_CLOUD_PASSWORD', 'your-password'); |
152
|
|
|
|
153
|
|
|
2) Set environmental variables (for example in .bashrc) |
154
|
|
|
|
155
|
|
|
export REPORTING_CLOUD_USERNAME='your-username' |
156
|
|
|
export REPORTING_CLOUD_PASSWORD='your-password' |
157
|
|
|
|
158
|
|
|
Note, these instructions apply only to the demo scripts and phpunit tests. When you use ReportingCloud in your application, set credentials in your constructor, using the setApiKey(\$apiKey) or the setUsername(\$username) and setPassword(\$password) methods. For an example, see '/demo/instantiation.php'. |
159
|
|
|
|
160
|
|
|
For further assistance and customer service please refer to: |
161
|
|
|
|
162
|
|
|
http://www.reporting.cloud |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
END; |
166
|
|
|
|
167
|
|
|
return wordwrap($ret, 80); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// @codingStandardsIgnoreEnd |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Print line, wrapped at self::LINE_LENGTH th character |
174
|
|
|
* |
175
|
|
|
* @param string $string String |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public static function writeLn($string) |
180
|
|
|
{ |
181
|
|
|
print wordwrap($string, self::LINE_LENGTH); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Print result line like in a table of contents i.e.: |
186
|
|
|
* |
187
|
|
|
* n: XXX YYY ZZZ....ZZZ |
188
|
|
|
* |
189
|
|
|
* @param int $counter Counter |
190
|
|
|
* @param string $testString Test string |
191
|
|
|
* @param string $testResult Test result |
192
|
|
|
*/ |
193
|
|
|
public static function writeLnToc($counter, $testString, $testResult) |
194
|
|
|
{ |
195
|
|
|
$lineLength = self::LINE_LENGTH; |
196
|
|
|
$padding = $lineLength - (4 + strlen(TEST_PASS)); |
|
|
|
|
197
|
|
|
$counter = sprintf('%2s: ', $counter); |
198
|
|
|
$testString = str_pad($testString, $padding, '.', STR_PAD_RIGHT); |
199
|
|
|
|
200
|
|
|
printf('%s%s%s%s', $counter, $testString, $testResult, PHP_EOL); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.