|
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
|
|
|
* Check that either the API key has been defined in environment variables |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public static function checkCredentials(): bool |
|
38
|
|
|
{ |
|
39
|
3 |
|
if (null !== self::apiKey()) { |
|
40
|
3 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return the ReportingCloud API key from a PHP constant or environmental variable |
|
48
|
|
|
* |
|
49
|
|
|
* @return string|null |
|
50
|
|
|
*/ |
|
51
|
217 |
|
public static function apiKey(): ?string |
|
52
|
|
|
{ |
|
53
|
217 |
|
$key = self::API_KEY; |
|
54
|
|
|
|
|
55
|
217 |
|
if (defined($key)) { |
|
56
|
|
|
$ret = (string) constant($key); |
|
57
|
|
|
$ret = trim($ret); |
|
58
|
|
|
return $ret; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
217 |
|
if (getenv($key)) { |
|
62
|
217 |
|
$ret = (string) getenv($key); |
|
63
|
217 |
|
$ret = trim($ret); |
|
64
|
217 |
|
return $ret; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return error message explaining how to configure PHP constant or environmental variables |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public static function errorMessage(): string |
|
76
|
|
|
{ |
|
77
|
|
|
$format = <<<END |
|
78
|
|
|
|
|
79
|
|
|
Error: ReportingCloud API key not defined. |
|
80
|
|
|
|
|
81
|
|
|
In order to execute %s, you must first set your ReportingCloud API key. |
|
82
|
|
|
|
|
83
|
|
|
There are two ways in which you can do this: |
|
84
|
|
|
|
|
85
|
|
|
1) Define the following PHP constant: |
|
86
|
|
|
|
|
87
|
|
|
define('%s', '%s'); |
|
88
|
|
|
|
|
89
|
|
|
2) Set environmental variable (for example in .bashrc) |
|
90
|
|
|
|
|
91
|
|
|
export %s='%s' |
|
92
|
|
|
|
|
93
|
|
|
Note, these instructions apply only to the demo scripts and phpunit tests. |
|
94
|
|
|
When you use ReportingCloud in your application, set credentials in your constructor |
|
95
|
|
|
or using the setApiKey(\$apiKey). For an example, see '/demo/instantiation.php'. |
|
96
|
|
|
|
|
97
|
|
|
For further assistance and customer service please refer to: |
|
98
|
|
|
|
|
99
|
|
|
https://www.reporting.cloud |
|
100
|
|
|
|
|
101
|
|
|
END; |
|
102
|
3 |
|
$filename = (string) $_SERVER['argv'][0] ?? ''; |
|
103
|
3 |
|
$filename = realpath($filename); |
|
104
|
|
|
|
|
105
|
3 |
|
if (is_bool($filename)) { |
|
|
|
|
|
|
106
|
|
|
$name = 'this script'; |
|
107
|
|
|
} else { |
|
108
|
3 |
|
$base = dirname(__FILE__, 3); |
|
109
|
3 |
|
$file = str_replace($base, '', $filename); |
|
110
|
3 |
|
$name = sprintf("'%s'", $file); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
$key = self::API_KEY; |
|
114
|
3 |
|
$value = 'your-api-key'; |
|
115
|
|
|
|
|
116
|
3 |
|
$ret = sprintf($format, $name, $key, $value, $key, $value); |
|
117
|
3 |
|
$ret = wordwrap($ret, 80); |
|
118
|
|
|
|
|
119
|
3 |
|
return $ret; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Dump information about a variable |
|
124
|
|
|
* (var_dump is wrapped to suppress psalm warning) |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed|null $array |
|
127
|
|
|
* |
|
128
|
|
|
* @psalm-suppress ForbiddenCode |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public static function dump($array): void |
|
131
|
|
|
{ |
|
132
|
3 |
|
var_dump($array); |
|
133
|
3 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Write a line to the console |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $format |
|
139
|
|
|
* @param mixed ...$args |
|
140
|
|
|
*/ |
|
141
|
9 |
|
public static function writeLn(string $format = '', ...$args): void |
|
142
|
|
|
{ |
|
143
|
9 |
|
$args = (array) $args; |
|
144
|
|
|
|
|
145
|
9 |
|
if (count($args) > 0) { |
|
146
|
6 |
|
echo vsprintf($format, $args); |
|
147
|
|
|
} else { |
|
148
|
3 |
|
echo $format; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
9 |
|
echo PHP_EOL; |
|
152
|
9 |
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|