Passed
Branch backend-2.5 (281b0d)
by Jonathan
14:44
created

ConsoleUtils::baseUri()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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://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
     * 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 3
    public static function checkCredentials(): bool
45
    {
46 3
        if (null !== self::apiKey()) {
47 3
            return true;
48
        }
49
50 3
        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 274
    public static function apiKey(): ?string
59
    {
60 274
        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 283
    public static function baseUri(): ?string
69
    {
70 283
        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 3
    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 3
        $filename = (string) $_SERVER['argv'][0] ?? '';
106 3
        $filename = realpath($filename);
107
108 3
        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 3
            $base = dirname(__FILE__, 3);
112 3
            $file = str_replace($base, '', $filename);
113 3
            $name = sprintf("'%s'", $file);
114
        }
115
116 3
        $key   = self::API_KEY;
117 3
        $value = 'your-api-key';
118
119 3
        $ret = sprintf($format, $name, $key, $value, $key, $value);
120 3
        $ret = wordwrap($ret, 80);
121
122 3
        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
     * @psalm-suppress ForbiddenCode
132
     */
133 3
    public static function dump($array): void
134
    {
135 3
        var_dump($array);
136 3
    }
137
138
    /**
139
     * Write a line to the console
140
     *
141
     * @param string $format
142
     * @param mixed  ...$args
143
     */
144 9
    public static function writeLn(string $format = '', ...$args): void
145
    {
146 9
        $args = (array) $args;
147
148 9
        if (count($args) > 0) {
149 6
            echo vsprintf($format, $args);
150
        } else {
151 3
            echo $format;
152
        }
153
154 9
        echo PHP_EOL;
155 9
    }
156
157
    /**
158
     * Return a value from a PHP constant or environmental variable
159
     *
160
     * @param $key
161
     *
162
     * @return string|null
163
     */
164 283
    protected static function getValueFromConstOrEnvVar($key): ?string
165
    {
166 283
        if (defined($key)) {
167
            $ret = (string) constant($key);
168
            $ret = trim($ret);
169
170
            return $ret;
171
        }
172
173 283
        if (getenv($key)) {
174 283
            $ret = (string) getenv($key);
175 283
            $ret = trim($ret);
176
177 283
            return $ret;
178
        }
179
180 9
        return null;
181
    }
182
}
183