Passed
Push — master ( 4355fd...f8d0d3 )
by Jonathan
06:59 queued 13s
created

Helper::varExportToFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
167
     * @param $values   Array of data
168
     *
169
     * @return bool|int
170
     */
171
    public static function varExportToFile($filename, $values)
172
    {
173
        $buffer = '<?php';
174
        $buffer .= PHP_EOL;
175
        $buffer .= PHP_EOL;
176
        $buffer .= 'return ';
177
        $buffer .= var_export($values, true);
178
        $buffer .= ';';
179
        $buffer .= PHP_EOL;
180
181
        return file_put_contents($filename, $buffer);
182
    }
183
}
184