Completed
Push — master ( 3fc782...8ee3b7 )
by Jonathan
03:44
created

Helper::username()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 © 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
     * Name of username PHP constant or environmental variables
25
     *
26
     * @const REPORTING_CLOUD_USERNAME
27
     */
28
    const USERNAME = 'REPORTING_CLOUD_USERNAME';
29
30
    /**
31
     * Name of password PHP constant or environmental variables
32
     *
33
     * @const REPORTING_CLOUD_PASSWORD
34
     */
35
    const PASSWORD = 'REPORTING_CLOUD_PASSWORD';
36
37
    /**
38
     * Return error message explaining how to configure PHP constant or environmental variables
39
     *
40
     * @return string
41
     */
42
    public static function errorMessage()
43
    {
44
        $ret = <<<END
45
    
46
Error: ReportingCloud username and/or password not defined.
47
48
In order to execute this script, you must first set your ReportingCloud username and password.
49
50
There are two ways in which you can do this:
51
52
1) Define the following PHP constants:
53
54
    define('REPORTING_CLOUD_USERNAME', 'your-username');
55
    define('REPORTING_CLOUD_PASSWORD', 'your-password');
56
57
2) Set environmental variables (for example in .bashrc)
58
    
59
    export REPORTING_CLOUD_USERNAME='your-username'
60
    export REPORTING_CLOUD_PASSWORD='your-password'
61
62
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'.
63
64
For further assistance and customer service please refer to:
65
66
    http://www.reporting.cloud
67
68
69
END;
70
71
        return wordwrap($ret, 80);
72
    }
73
74
    /**
75
     * Return the ReportingCloud username
76
     *
77
     * @return null|string
78
     */
79
    public static function username()
80
    {
81
        $username = self::variable(self::USERNAME);
82
83
        return $username;
84
    }
85
86
    /**
87
     * Return the ReportingCloud password
88
     *
89
     * @return null|string
90
     */
91
    public static function password()
92
    {
93
        $password = self::variable(self::PASSWORD);
94
95
        return $password;
96
    }
97
98
    /**
99
     * Check credentials have been define in environment variables, otherwise exit with error code 1
100
     */
101
    public static function checkCredentials()
102
    {
103
        if (null === self::username() || null === self::password()) {
104
            echo self::errorMessage();
105
            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...
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * Return the value of the PHP constant or environmental variable
113
     *
114
     * @param string $variable Variable
115
     *
116
     * @return null|string
117
     */
118
    protected static function variable($variable)
119
    {
120
        $ret = null;
121
122
        if (defined($variable)) {
123
            $value = constant($variable);
124
        } else {
125
            $value = getenv($variable);
126
        }
127
128
        $value = trim($value);
129
130
        if (strlen($value) > 0) {
131
            $ret = $value;
132
        }
133
134
        return $ret;
135
    }
136
137
}