|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* ReportingCloud PHP Wrapper |
|
6
|
|
|
* |
|
7
|
|
|
* PHP wrapper 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
|
|
|
private 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
|
|
|
public static function checkCredentials(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
if (null !== self::apiKey()) { |
|
40
|
|
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
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
|
|
|
public static function apiKey(): ?string |
|
52
|
|
|
{ |
|
53
|
|
|
$key = self::API_KEY; |
|
54
|
|
|
|
|
55
|
|
|
if (defined($key)) { |
|
56
|
|
|
$value = constant($key); |
|
57
|
|
|
} else { |
|
58
|
|
|
$value = getenv($key); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$value = trim($value); |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($value)) { |
|
64
|
|
|
return $value; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return error message explaining how to configure PHP constant or environmental variables |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function errorMessage(): string |
|
76
|
|
|
{ |
|
77
|
|
|
$format |
|
78
|
|
|
= <<<END |
|
79
|
|
|
|
|
80
|
|
|
Error: ReportingCloud API key not defined. |
|
81
|
|
|
|
|
82
|
|
|
In order to execute %s, you must first set your ReportingCloud API key. |
|
83
|
|
|
|
|
84
|
|
|
There are two ways in which you can do this: |
|
85
|
|
|
|
|
86
|
|
|
1) Define the following PHP constant: |
|
87
|
|
|
|
|
88
|
|
|
define('%s', '%s'); |
|
89
|
|
|
|
|
90
|
|
|
2) Set environmental variable (for example in .bashrc) |
|
91
|
|
|
|
|
92
|
|
|
export %s='%s' |
|
93
|
|
|
|
|
94
|
|
|
Note, these instructions apply only to the demo scripts and phpunit tests. |
|
95
|
|
|
When you use ReportingCloud in your application, set credentials in your constructor |
|
96
|
|
|
or using the setApiKey(\$apiKey). For an example, see '/demo/instantiation.php'. |
|
97
|
|
|
|
|
98
|
|
|
For further assistance and customer service please refer to: |
|
99
|
|
|
|
|
100
|
|
|
https://www.reporting.cloud |
|
101
|
|
|
|
|
102
|
|
|
END; |
|
103
|
|
|
$filename = $_SERVER['argv'][0] ?? ''; |
|
104
|
|
|
$filename = realpath($filename); |
|
105
|
|
|
|
|
106
|
|
|
if (empty($filename)) { |
|
107
|
|
|
$name = 'this script'; |
|
108
|
|
|
} else { |
|
109
|
|
|
$base = realpath(__DIR__ . '/../../'); |
|
110
|
|
|
$file = str_replace($base, '', $filename); |
|
111
|
|
|
$name = sprintf("'%s'", $file); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$key = self::API_KEY; |
|
115
|
|
|
$value = 'your-api-key'; |
|
116
|
|
|
|
|
117
|
|
|
$ret = sprintf($format, $name, $key, $value, $key, $value); |
|
118
|
|
|
$ret = wordwrap($ret, 80); |
|
119
|
|
|
|
|
120
|
|
|
return $ret; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|