Passed
Branch development (fdf8f8)
by Jonathan
05:11
created

Helper::apiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud\Console;
15
16
/**
17
 * ReportingCloud console helper (used only for tests and demos)
18
 *
19
 * @package TxTextControl\ReportingCloud
20
 * @author  Jonathan Maron (@JonathanMaron)
21
 */
22
class Helper
23
{
24
    /**
25
     * Line length in characters (used to wrap long lines)
26
     */
27
    const LINE_LENGTH = 80;
28
29
    /**
30
     * Name of username PHP constant or environmental variables
31
     *
32
     * @const REPORTING_CLOUD_USERNAME
33
     */
34
    const USERNAME = 'REPORTING_CLOUD_USERNAME';
35
36
    /**
37
     * Name of password PHP constant or environmental variables
38
     *
39
     * @const REPORTING_CLOUD_PASSWORD
40
     */
41
    const PASSWORD = 'REPORTING_CLOUD_PASSWORD';
42
43
    /**
44
     * Name of API key PHP constant or environmental variables
45
     *
46
     * @const REPORTING_CLOUD_API_KEY
47
     */
48
    const API_KEY = 'REPORTING_CLOUD_API_KEY';
49
50
    /**
51
     * Check ReportingCloud credentials, which have been defined in environment variables, otherwise terminate script
52
     * execution with error code 1
53
     */
54
    public static function checkCredentials()
55
    {
56
        if (null !== self::apiKey()) {
57
            return true;
58
        }
59
60
        if (null !== self::username() && null !== self::password()) {
61
            return true;
62
        }
63
64
        echo self::errorMessage();
65
        die(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkCredentials() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
66
    }
67
68
    /**
69
     * Return the ReportingCloud username
70
     *
71
     * @return null|string
72
     */
73
    public static function username()
74
    {
75
        $username = self::variable(self::USERNAME);
76
77
        return $username;
78
    }
79
80
    /**
81
     * Return the value of the PHP constant or environmental variable
82
     *
83
     * @param string $variable Variable
84
     *
85
     * @return null|string
86
     */
87
    protected static function variable($variable)
88
    {
89
        $ret = null;
90
91
        if (defined($variable)) {
92
            $value = constant($variable);
93
        } else {
94
            $value = getenv($variable);
95
        }
96
97
        $value = trim($value);
98
99
        if (strlen($value) > 0) {
100
            $ret = $value;
101
        }
102
103
        return $ret;
104
    }
105
106
    /**
107
     * Return the ReportingCloud password
108
     *
109
     * @return null|string
110
     */
111
    public static function password()
112
    {
113
        $password = self::variable(self::PASSWORD);
114
115
        return $password;
116
    }
117
118
    /**
119
     * Return the ReportingCloud API key
120
     *
121
     * @return null|string
122
     */
123
    public static function apiKey()
124
    {
125
        $apiKey = self::variable(self::API_KEY);
126
127
        return $apiKey;
128
    }
129
130
    // @codingStandardsIgnoreStart
131
132
    /**
133
     * Return error message explaining how to configure PHP constant or environmental variables
134
     *
135
     * @return string
136
     */
137
    public static function errorMessage()
138
    {
139
        $ret
140
            = <<<END
141
    
142
Error: ReportingCloud username and/or password not defined.
143
144
In order to execute this script, you must first set your ReportingCloud username and password.
145
146
There are two ways in which you can do this:
147
148
1) Define the following PHP constants:
149
150
    define('REPORTING_CLOUD_USERNAME', 'your-username');
151
    define('REPORTING_CLOUD_PASSWORD', 'your-password');
152
153
2) Set environmental variables (for example in .bashrc)
154
    
155
    export REPORTING_CLOUD_USERNAME='your-username'
156
    export REPORTING_CLOUD_PASSWORD='your-password'
157
158
Note, these instructions apply only to the phpunit and demo scripts. When you use ReportingCloud in your application, set the username and password in your constructor or using the setUsername(\$username) and setPassword(\$password) methods. For an example of this case, see 'demo/instantiation.php'.
159
160
For further assistance and customer service please refer to:
161
162
    http://www.reporting.cloud
163
164
165
END;
166
167
        return wordwrap($ret, 80);
168
    }
169
170
    // @codingStandardsIgnoreEnd
171
172
    /**
173
     * Print line, wrapped at self::LINE_LENGTH th character
174
     *
175
     * @param string $string String
176
     *
177
     * @return string
178
     */
179
    public static function writeLn($string)
180
    {
181
        print wordwrap($string, self::LINE_LENGTH);
182
    }
183
184
    /**
185
     * Print result line like in a table of contents i.e.:
186
     *
187
     * n: XXX YYY ZZZ....ZZZ
188
     *
189
     * @param int    $counter    Counter
190
     * @param string $testString Test string
191
     * @param string $testResult Test result
192
     */
193
    public static function writeLnToc($counter, $testString, $testResult)
194
    {
195
        $lineLength = self::LINE_LENGTH;
196
        $padding    = $lineLength - (4 + strlen(TEST_PASS));
197
        $counter    = sprintf('%2s: ', $counter);
198
        $testString = str_pad($testString, $padding, '.', STR_PAD_RIGHT);
199
200
        printf('%s%s%s%s', $counter, $testString, $testResult, PHP_EOL);
201
    }
202
}
203