Completed
Push — master ( 92664a...c5cfd9 )
by Jonathan
04:54
created

Helper::writeLnToc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 3
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 © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud\Console;
14
15
/**
16
 * ReportingCloud console helper (used only for tests and demos)
17
 *
18
 * @package TxTextControl\ReportingCloud
19
 * @author  Jonathan Maron (@JonathanMaron)
20
 */
21
class Helper
22
{
23
    /**
24
     * Line length in characters (used to wrap long lines)
25
     */
26
    const LINE_LENGTH = 80;
27
28
    /**
29
     * Name of username PHP constant or environmental variables
30
     *
31
     * @const REPORTING_CLOUD_USERNAME
32
     */
33
    const USERNAME = 'REPORTING_CLOUD_USERNAME';
34
35
    /**
36
     * Name of password PHP constant or environmental variables
37
     *
38
     * @const REPORTING_CLOUD_PASSWORD
39
     */
40
    const PASSWORD = 'REPORTING_CLOUD_PASSWORD';
41
42
    /**
43
     * Return error message explaining how to configure PHP constant or environmental variables
44
     *
45
     * @return string
46
     */
47
    public static function errorMessage()
48
    {
49
        $ret = <<<END
50
    
51
Error: ReportingCloud username and/or password not defined.
52
53
In order to execute this script, you must first set your ReportingCloud username and password.
54
55
There are two ways in which you can do this:
56
57
1) Define the following PHP constants:
58
59
    define('REPORTING_CLOUD_USERNAME', 'your-username');
60
    define('REPORTING_CLOUD_PASSWORD', 'your-password');
61
62
2) Set environmental variables (for example in .bashrc)
63
    
64
    export REPORTING_CLOUD_USERNAME='your-username'
65
    export REPORTING_CLOUD_PASSWORD='your-password'
66
67
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'.
68
69
For further assistance and customer service please refer to:
70
71
    http://www.reporting.cloud
72
73
74
END;
75
76
        return wordwrap($ret, 80);
77
    }
78
79
    /**
80
     * Return the ReportingCloud username
81
     *
82
     * @return null|string
83
     */
84
    public static function username()
85
    {
86
        $username = self::variable(self::USERNAME);
87
88
        return $username;
89
    }
90
91
    /**
92
     * Return the ReportingCloud password
93
     *
94
     * @return null|string
95
     */
96
    public static function password()
97
    {
98
        $password = self::variable(self::PASSWORD);
99
100
        return $password;
101
    }
102
103
    /**
104
     * Check ReportingCloud credentials, which have been defined in environment variables, otherwise terminate script
105
     * execution with error code 1
106
     */
107
    public static function checkCredentials()
108
    {
109
        if (null === self::username() || null === self::password()) {
110
            echo self::errorMessage();
111
            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...
112
        }
113
114
        return true;
115
    }
116
117
    /**
118
     * Return the value of the PHP constant or environmental variable
119
     *
120
     * @param string $variable Variable
121
     *
122
     * @return null|string
123
     */
124
    protected static function variable($variable)
125
    {
126
        $ret = null;
127
128
        if (defined($variable)) {
129
            $value = constant($variable);
130
        } else {
131
            $value = getenv($variable);
132
        }
133
134
        $value = trim($value);
135
136
        if (strlen($value) > 0) {
137
            $ret = $value;
138
        }
139
140
        return $ret;
141
    }
142
143
    /**
144
     * Print line, wrapped at self::LINE_LENGTH th character
145
     *
146
     * @param string $string String
147
     * @return string
148
     */
149
    public static function writeLn($string)
150
    {
151
        print wordwrap($string, self::LINE_LENGTH);
152
    }
153
154
    /**
155
     * Print result line like in a table of contents i.e.:
156
     *
157
     * n: XXX YYY ZZZ....ZZZ
158
     *
159
     * @param integer $counter    Counter
160
     * @param string  $testString Test string
161
     * @param string  $testResult Test result
162
     */
163
    public static function writeLnToc($counter, $testString, $testResult)
164
    {
165
        $lineLength = self::LINE_LENGTH;
166
        $padding    = $lineLength - (4 + strlen(TEST_PASS));
167
        $counter    = sprintf('%2s: ', $counter);
168
        $testString = str_pad($testString, $padding, '.', STR_PAD_RIGHT);
169
170
        printf('%s%s%s%s', $counter, $testString, $testResult, PHP_EOL);
171
    }
172
173
}