Passed
Branch qa (cee064)
by Jonathan
14:50
created

ConsoleUtils::apiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 © 2021 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
        if (null !== self::apiKey()) {
47 2
            return true;
48
        }
49
50 2
        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 188
    public static function apiKey(): ?string
59
    {
60 188
        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 194
    public static function baseUri(): ?string
69
    {
70 194
        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 2
    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 2
        $filename = $_SERVER['argv'][0] ?? '';
106 2
        $filename = realpath($filename);
107
108 2
        if (is_bool($filename)) {
0 ignored issues
show
introduced by
The condition is_bool($filename) is always false.
Loading history...
109
            $name = 'this script';
110
        } else {
111 2
            $base = dirname(__FILE__, 3);
112 2
            $file = str_replace($base, '', $filename);
113 2
            $name = sprintf("'%s'", $file);
114
        }
115
116 2
        $key   = self::API_KEY;
117 2
        $value = 'your-api-key';
118
119 2
        $ret = sprintf($format, $name, $key, $value, $key, $value);
120 2
        $ret = wordwrap($ret, 80);
121
122 2
        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
     */
132 2
    public static function dump($array): void
133
    {
134 2
        var_dump($array);
135 2
    }
136
137
    /**
138
     * Write a line to the console
139
     *
140
     * @param string $format
141
     * @param mixed  ...$args
142
     */
143 6
    public static function writeLn(string $format = '', ...$args): void
144
    {
145 6
        if (count($args) > 0) {
146 4
            echo vsprintf($format, $args);
147
        } else {
148 2
            echo $format;
149
        }
150
151 6
        echo PHP_EOL;
152 6
    }
153
154
    /**
155
     * Return a value from a PHP constant or environmental variable
156
     *
157
     * @param string $key
158
     *
159
     * @return string|null
160
     */
161 194
    private static function getValueFromConstOrEnvVar(string $key): ?string
162
    {
163 194
        $ret = self::getValueFromConst($key);
164
165 194
        if (null !== $ret) {
166
            return $ret;
167
        }
168
169 194
        $ret = self::getValueFromEnvVar($key);
170
171 194
        if (null !== $ret) {
172 188
            return $ret;
173
        }
174
175 194
        return null;
176
    }
177
178
    /**
179
     * Return a value from a PHP constant
180
     *
181
     * @param string $key
182
     *
183
     * @return string|null
184
     */
185 194
    private static function getValueFromConst(string $key): ?string
186
    {
187 194
        if (defined($key)) {
188
            $ret = (string) constant($key);
189
            $ret = trim($ret);
190
            if (strlen($ret) > 0) {
191
                return $ret;
192
            }
193
        }
194
195 194
        return null;
196
    }
197
198
    /**
199
     * Return a value from an environmental variable
200
     *
201
     * @param string $key
202
     *
203
     * @return string|null
204
     */
205 194
    private static function getValueFromEnvVar(string $key): ?string
206
    {
207 194
        $env = getenv($key);
208
209 194
        if (is_string($env)) {
0 ignored issues
show
introduced by
The condition is_string($env) is always true.
Loading history...
210 188
            $ret = trim($env);
211 188
            if (strlen($ret) > 0) {
212 188
                return $ret;
213
            }
214
        }
215
216 194
        return null;
217
    }
218
}
219