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
|
|
|
* Name of username PHP constant or environmental variables |
26
|
|
|
* |
27
|
|
|
* @const REPORTING_CLOUD_API_KEY |
28
|
|
|
*/ |
29
|
|
|
const API_KEY = 'REPORTING_CLOUD_API_KEY'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check that either the API key has been defined in environment variables |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public static function checkCredentials() |
37
|
|
|
{ |
38
|
|
|
if (null !== self::apiKey()) { |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return the value of the PHP constant or environmental variable |
47
|
|
|
* |
48
|
|
|
* @param string $variable Variable |
49
|
|
|
* |
50
|
|
|
* @return string|null |
51
|
|
|
*/ |
52
|
|
|
protected static function variable($variable) |
53
|
|
|
{ |
54
|
|
|
$ret = null; |
55
|
|
|
|
56
|
|
|
if (defined($variable)) { |
57
|
|
|
$value = constant($variable); |
58
|
|
|
} else { |
59
|
|
|
$value = getenv($variable); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$value = trim($value); |
63
|
|
|
|
64
|
|
|
if (strlen($value) > 0) { |
65
|
|
|
$ret = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $ret; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the ReportingCloud API key |
73
|
|
|
* |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
|
|
public static function apiKey() |
77
|
|
|
{ |
78
|
|
|
return self::variable(self::API_KEY); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return error message explaining how to configure PHP constant or environmental variables |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function errorMessage() |
87
|
|
|
{ |
88
|
|
|
$ret |
89
|
|
|
= <<<END |
90
|
|
|
|
91
|
|
|
Error: ReportingCloud API key not defined. |
92
|
|
|
|
93
|
|
|
In order to execute this script, you must first set your ReportingCloud |
94
|
|
|
API key. |
95
|
|
|
|
96
|
|
|
There are two ways in which you can do this: |
97
|
|
|
|
98
|
|
|
1) Define the following PHP constant: |
99
|
|
|
|
100
|
|
|
define('REPORTING_CLOUD_API_KEY', 'your-api-key'); |
101
|
|
|
|
102
|
|
|
2) Set environmental variable (for example in .bashrc) |
103
|
|
|
|
104
|
|
|
export REPORTING_CLOUD_API_KEY='your-api-key' |
105
|
|
|
|
106
|
|
|
Note, these instructions apply only to the demo scripts and phpunit tests. |
107
|
|
|
When you use ReportingCloud in your application, set credentials in your |
108
|
|
|
constructor or using the setApiKey(\$apiKey). For an example, see |
109
|
|
|
'/demo/instantiation.php'. |
110
|
|
|
|
111
|
|
|
For further assistance and customer service please refer to: |
112
|
|
|
|
113
|
|
|
http://www.reporting.cloud |
114
|
|
|
|
115
|
|
|
END; |
116
|
|
|
|
117
|
|
|
return $ret; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Export variables to file |
122
|
|
|
* |
123
|
|
|
* @param string $filename Name of file to which to write |
124
|
|
|
* @param array $values Array of data |
125
|
|
|
* |
126
|
|
|
* @return bool|int |
127
|
|
|
*/ |
128
|
|
|
public static function varExportToFile($filename, array $values) |
129
|
|
|
{ |
130
|
|
|
$buffer = '<?php'; |
131
|
|
|
$buffer .= PHP_EOL; |
132
|
|
|
$buffer .= PHP_EOL; |
133
|
|
|
$buffer .= 'return '; |
134
|
|
|
$buffer .= var_export($values, true); |
135
|
|
|
$buffer .= ';'; |
136
|
|
|
$buffer .= PHP_EOL; |
137
|
|
|
|
138
|
|
|
return file_put_contents($filename, $buffer); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// ----------------------------------------------------------------------------------------------------------------- |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Deprecated methods |
145
|
|
|
*/ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the ReportingCloud username |
149
|
|
|
* |
150
|
|
|
* @return null |
151
|
|
|
*/ |
152
|
|
|
public static function username() |
153
|
|
|
{ |
154
|
|
|
$format = '"%s" is deprecated - use an %s::apiKey instead'; |
155
|
|
|
$message = sprintf($format, __METHOD__, __CLASS__); |
156
|
|
|
trigger_error($message, E_USER_DEPRECATED); |
157
|
|
|
|
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return the ReportingCloud password |
163
|
|
|
* |
164
|
|
|
* @return null |
165
|
|
|
*/ |
166
|
|
|
public static function password() |
167
|
|
|
{ |
168
|
|
|
$format = '"%s" is deprecated - use an %s::apiKey instead'; |
169
|
|
|
$message = sprintf($format, __METHOD__, __CLASS__); |
170
|
|
|
trigger_error($message, E_USER_DEPRECATED); |
171
|
|
|
|
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|