Passed
Push — master ( 3bb42b...b98909 )
by Jonathan
22:57
created

Helper::apiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
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
     * Name of username PHP constant or environmental variables
26
     *
27
     * @const REPORTING_CLOUD_USERNAME
28
     */
29
    const USERNAME = 'REPORTING_CLOUD_USERNAME';
30
31
    /**
32
     * Name of password PHP constant or environmental variables
33
     *
34
     * @const REPORTING_CLOUD_PASSWORD
35
     */
36
    const PASSWORD = 'REPORTING_CLOUD_PASSWORD';
37
38
    /**
39
     * Check that either the username and password have been defined in environment variables
40
     *
41
     * @return bool
42
     */
43
    public static function checkCredentials()
44
    {
45
        if (null !== self::username() && null !== self::password()) {
46
            return true;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Return the value of the PHP constant or environmental variable
54
     *
55
     * @param string $variable Variable
56
     *
57
     * @return string|null
58
     */
59
    protected static function variable($variable)
60
    {
61
        $ret = null;
62
63
        if (defined($variable)) {
64
            $value = constant($variable);
65
        } else {
66
            $value = getenv($variable);
67
        }
68
69
        $value = trim($value);
70
71
        if (strlen($value) > 0) {
72
            $ret = $value;
73
        }
74
75
        return $ret;
76
    }
77
78
    /**
79
     * Return the ReportingCloud username
80
     *
81
     * @return string|null
82
     */
83
    public static function username()
84
    {
85
        return self::variable(self::USERNAME);
86
    }
87
88
    /**
89
     * Return the ReportingCloud password
90
     *
91
     * @return string|null
92
     */
93
    public static function password()
94
    {
95
        return self::variable(self::PASSWORD);
96
    }
97
98
    /**
99
     * Return error message explaining how to configure PHP constant or environmental variables
100
     *
101
     * @return string
102
     */
103
    public static function errorMessage()
104
    {
105
        $ret
106
            = <<<END
107
108
Error: ReportingCloud username and/or password not defined.
109
110
In order to execute this script, you must first set your ReportingCloud
111
username and password.
112
113
There are two ways in which you can do this:
114
115
1) Define the following PHP constants:
116
117
    define('REPORTING_CLOUD_USERNAME', 'your-username');
118
    define('REPORTING_CLOUD_PASSWORD', 'your-password');
119
120
2) Set environmental variables (for example in .bashrc)
121
    
122
    export REPORTING_CLOUD_USERNAME='your-username'
123
    export REPORTING_CLOUD_PASSWORD='your-password'
124
125
Note, these instructions apply only to the demo scripts and phpunit tests.
126
When you use ReportingCloud in your application, set credentials in your
127
constructor, using the setApiKey(\$apiKey) or the setUsername(\$username) and
128
setPassword(\$password) methods. For an example, see '/demo/instantiation.php'.
129
130
For further assistance and customer service please refer to:
131
132
    http://www.reporting.cloud
133
134
END;
135
136
        return $ret;
137
    }
138
139
    /**
140
     * Export variables to file
141
     *
142
     * @param string $filename Name of file to which to write
143
     * @param array  $values   Array of data
144
     *
145
     * @return bool|int
146
     */
147
    public static function varExportToFile($filename, array $values)
148
    {
149
        $buffer = '<?php';
150
        $buffer .= PHP_EOL;
151
        $buffer .= PHP_EOL;
152
        $buffer .= 'return ';
153
        $buffer .= var_export($values, true);
154
        $buffer .= ';';
155
        $buffer .= PHP_EOL;
156
157
        return file_put_contents($filename, $buffer);
158
    }
159
}
160