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://git.io/Jejj2 for the canonical source repository |
||
11 | * @license https://git.io/Jejjr |
||
12 | * @copyright © 2022 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 | 2 | public static function checkCredentials(): bool |
|
45 | { |
||
46 | 2 | return strlen(self::apiKey()) > 0; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return the ReportingCloud API key from a PHP constant or environmental variable |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | 188 | public static function apiKey(): string |
|
55 | { |
||
56 | 188 | return self::getValueFromConstOrEnvVar(self::API_KEY); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Return the ReportingCloud base URI from a PHP constant or environmental variable |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 76 | public static function baseUri(): string |
|
65 | { |
||
66 | 76 | return self::getValueFromConstOrEnvVar(self::BASE_URI); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Return error message explaining how to configure PHP constant or environmental variables |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 2 | public static function errorMessage(): string |
|
75 | { |
||
76 | // phpcs:disable |
||
77 | |||
78 | 2 | $format = <<<END |
|
79 | |||
80 | \e[41m\e[1mError: ReportingCloud API key not defined.\e[0m |
||
81 | |||
82 | In order to execute \e[32m%s\e[0m, you must first set your ReportingCloud API key. |
||
83 | |||
84 | There are two ways in which you can do this: |
||
85 | |||
86 | 1) Define a PHP constant: |
||
87 | |||
88 | \e[1mdefine('%s', '%s');\e[0m |
||
89 | |||
90 | 2) Set an environmental variable (for example in .bashrc) |
||
91 | |||
92 | \e[1mexport %s='%s'\e[0m |
||
93 | |||
94 | Note, these instructions apply only to the demo scripts and phpunit tests. |
||
95 | |||
96 | When you use ReportingCloud in your application, set the API key in your constructor or using the 'setApiKey(string \$apiKey):self' method. |
||
97 | |||
98 | For an example, see \e[32m'/demo/instantiation.php'\e[0m. |
||
99 | |||
100 | For further assistance and customer service please refer to: |
||
101 | |||
102 | https://www.reporting.cloud |
||
103 | |||
104 | |||
105 | 2 | END; |
|
106 | // phpcs:enable |
||
107 | |||
108 | 2 | $filename = $_SERVER['argv'][0] ?? ''; |
|
109 | 2 | $filename = realpath($filename); |
|
110 | |||
111 | 2 | if (is_bool($filename)) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
112 | $name = 'this script'; |
||
113 | } else { |
||
114 | 2 | $base = dirname(__FILE__, 3); |
|
115 | 2 | $file = str_replace($base, '', $filename); |
|
116 | 2 | $name = sprintf("'%s'", $file); |
|
117 | } |
||
118 | |||
119 | 2 | $key = self::API_KEY; |
|
120 | 2 | $value = 'your-api-key'; |
|
121 | |||
122 | 2 | $ret = sprintf($format, $name, $key, $value, $key, $value); |
|
123 | |||
124 | 2 | return wordwrap($ret, 80); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Dump information about a variable |
||
129 | * |
||
130 | * @param mixed $value |
||
131 | * |
||
132 | */ |
||
133 | 2 | public static function dump($value): void |
|
134 | { |
||
135 | 2 | /** @scrutinizer ignore-call */ var_dump($value); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Write a line to the console |
||
140 | * |
||
141 | * @param string $format |
||
142 | * @param mixed ...$args |
||
143 | */ |
||
144 | 6 | public static function writeLn(string $format = '', ...$args): void |
|
145 | { |
||
146 | 6 | if (count($args) > 0) { |
|
147 | 4 | echo vsprintf($format, $args); |
|
148 | } else { |
||
149 | 2 | echo $format; |
|
150 | } |
||
151 | |||
152 | 6 | echo PHP_EOL; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Return a value from a PHP constant or environmental variable |
||
157 | * |
||
158 | * @param string $key |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 188 | private static function getValueFromConstOrEnvVar(string $key): string |
|
163 | { |
||
164 | 188 | $value = self::getValueFromConst($key); |
|
165 | |||
166 | 188 | if (strlen($value) > 0) { |
|
167 | return $value; |
||
168 | } |
||
169 | |||
170 | 188 | $value = self::getValueFromEnvVar($key); |
|
171 | |||
172 | 188 | if (strlen($value) > 0) { |
|
173 | 188 | return $value; |
|
174 | } |
||
175 | |||
176 | 78 | return ''; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Return a value from a PHP constant |
||
181 | * |
||
182 | * @param string $key |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 188 | private static function getValueFromConst(string $key): string |
|
187 | { |
||
188 | 188 | if (defined($key)) { |
|
189 | $value = constant($key); |
||
190 | if (is_string($value)) { |
||
191 | $value = trim($value); |
||
192 | if (strlen($value) > 0) { |
||
193 | return $value; |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | 188 | return ''; |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * Return a value from an environmental variable |
||
203 | * |
||
204 | * @param string $key |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 188 | private static function getValueFromEnvVar(string $key): string |
|
209 | { |
||
210 | 188 | $value = getenv($key); |
|
211 | |||
212 | 188 | if (is_string($value)) { |
|
0 ignored issues
–
show
|
|||
213 | 188 | $value = trim($value); |
|
214 | 188 | if (strlen($value) > 0) { |
|
215 | 188 | return $value; |
|
216 | } |
||
217 | } |
||
218 | |||
219 | 78 | return ''; |
|
220 | } |
||
221 | } |
||
222 |