1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP SDK |
6
|
|
|
* |
7
|
|
|
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud\Stdlib; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ReportingCloud console helper (used only for tests and demos) |
19
|
|
|
* |
20
|
|
|
* @package TxTextControl\ReportingCloud |
21
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
22
|
|
|
*/ |
23
|
|
|
class ConsoleUtils extends AbstractStdlib |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Name of PHP constant or environmental variable storing API key |
27
|
|
|
* |
28
|
|
|
* @const REPORTING_CLOUD_API_KEY |
29
|
|
|
*/ |
30
|
|
|
public const API_KEY = 'REPORTING_CLOUD_API_KEY'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Name of PHP constant or environmental variable storing base URI |
34
|
|
|
* |
35
|
|
|
* @const REPORTING_CLOUD_BASE_URI |
36
|
|
|
*/ |
37
|
|
|
public const BASE_URI = 'REPORTING_CLOUD_BASE_URI'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check that either the API key has been defined in environment variables |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
3 |
|
public static function checkCredentials(): bool |
45
|
|
|
{ |
46
|
3 |
|
if (null !== self::apiKey()) { |
47
|
3 |
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return the ReportingCloud API key from a PHP constant or environmental variable |
55
|
|
|
* |
56
|
|
|
* @return string|null |
57
|
|
|
*/ |
58
|
274 |
|
public static function apiKey(): ?string |
59
|
|
|
{ |
60
|
274 |
|
return self::getValueFromConstOrEnvVar(self::API_KEY); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the ReportingCloud base URI from a PHP constant or environmental variable |
65
|
|
|
* |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
283 |
|
public static function baseUri(): ?string |
69
|
|
|
{ |
70
|
283 |
|
return self::getValueFromConstOrEnvVar(self::BASE_URI); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return error message explaining how to configure PHP constant or environmental variables |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
3 |
|
public static function errorMessage(): string |
79
|
|
|
{ |
80
|
|
|
$format = <<<END |
81
|
|
|
|
82
|
|
|
Error: ReportingCloud API key not defined. |
83
|
|
|
|
84
|
|
|
In order to execute %s, you must first set your ReportingCloud API key. |
85
|
|
|
|
86
|
|
|
There are two ways in which you can do this: |
87
|
|
|
|
88
|
|
|
1) Define the following PHP constant: |
89
|
|
|
|
90
|
|
|
define('%s', '%s'); |
91
|
|
|
|
92
|
|
|
2) Set environmental variable (for example in .bashrc) |
93
|
|
|
|
94
|
|
|
export %s='%s' |
95
|
|
|
|
96
|
|
|
Note, these instructions apply only to the demo scripts and phpunit tests. |
97
|
|
|
When you use ReportingCloud in your application, set credentials in your constructor |
98
|
|
|
or using the setApiKey(\$apiKey). For an example, see '/demo/instantiation.php'. |
99
|
|
|
|
100
|
|
|
For further assistance and customer service please refer to: |
101
|
|
|
|
102
|
|
|
https://www.reporting.cloud |
103
|
|
|
|
104
|
|
|
END; |
105
|
3 |
|
$filename = (string) $_SERVER['argv'][0] ?? ''; |
106
|
3 |
|
$filename = realpath($filename); |
107
|
|
|
|
108
|
3 |
|
if (is_bool($filename)) { |
|
|
|
|
109
|
|
|
$name = 'this script'; |
110
|
|
|
} else { |
111
|
3 |
|
$base = dirname(__FILE__, 3); |
112
|
3 |
|
$file = str_replace($base, '', $filename); |
113
|
3 |
|
$name = sprintf("'%s'", $file); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$key = self::API_KEY; |
117
|
3 |
|
$value = 'your-api-key'; |
118
|
|
|
|
119
|
3 |
|
$ret = sprintf($format, $name, $key, $value, $key, $value); |
120
|
3 |
|
$ret = wordwrap($ret, 80); |
121
|
|
|
|
122
|
3 |
|
return $ret; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Dump information about a variable |
127
|
|
|
* (var_dump is wrapped to suppress psalm warning) |
128
|
|
|
* |
129
|
|
|
* @param mixed|null $array |
130
|
|
|
* |
131
|
|
|
* @psalm-suppress ForbiddenCode |
132
|
|
|
*/ |
133
|
3 |
|
public static function dump($array): void |
134
|
|
|
{ |
135
|
3 |
|
var_dump($array); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Write a line to the console |
140
|
|
|
* |
141
|
|
|
* @param string $format |
142
|
|
|
* @param mixed ...$args |
143
|
|
|
*/ |
144
|
9 |
|
public static function writeLn(string $format = '', ...$args): void |
145
|
|
|
{ |
146
|
9 |
|
$args = (array) $args; |
147
|
|
|
|
148
|
9 |
|
if (count($args) > 0) { |
149
|
6 |
|
echo vsprintf($format, $args); |
150
|
|
|
} else { |
151
|
3 |
|
echo $format; |
152
|
|
|
} |
153
|
|
|
|
154
|
9 |
|
echo PHP_EOL; |
155
|
9 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return a value from a PHP constant or environmental variable |
159
|
|
|
* |
160
|
|
|
* @param $key |
161
|
|
|
* |
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
283 |
|
private static function getValueFromConstOrEnvVar($key): ?string |
165
|
|
|
{ |
166
|
283 |
|
$ret = self::getValueFromConst($key); |
167
|
|
|
|
168
|
283 |
|
if (null !== $ret) { |
169
|
|
|
return $ret; |
170
|
|
|
} |
171
|
|
|
|
172
|
283 |
|
$ret = self::getValueFromEnvVar($key); |
173
|
|
|
|
174
|
283 |
|
if (null !== $ret) { |
175
|
283 |
|
return $ret; |
176
|
|
|
} |
177
|
|
|
|
178
|
9 |
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return a value from a PHP constant |
183
|
|
|
* |
184
|
|
|
* @param $key |
185
|
|
|
* |
186
|
|
|
* @return string|null |
187
|
|
|
*/ |
188
|
283 |
|
private static function getValueFromConst($key): ?string |
189
|
|
|
{ |
190
|
283 |
|
if (defined($key)) { |
191
|
|
|
$ret = (string) constant($key); |
192
|
|
|
$ret = trim($ret); |
193
|
|
|
if (!empty($ret)) { |
194
|
|
|
return $ret; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
283 |
|
return null; |
199
|
|
|
} |
200
|
|
|
/** |
201
|
|
|
* Return a value from an environmental variable |
202
|
|
|
* |
203
|
|
|
* @param $key |
204
|
|
|
* |
205
|
|
|
* @return string|null |
206
|
|
|
*/ |
207
|
283 |
|
private static function getValueFromEnvVar($key): ?string |
208
|
|
|
{ |
209
|
283 |
|
if (getenv($key)) { |
210
|
283 |
|
$ret = (string) getenv($key); |
211
|
283 |
|
$ret = trim($ret); |
212
|
283 |
|
if (!empty($ret)) { |
213
|
283 |
|
return $ret; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
9 |
|
return null; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|