Completed
Push — master ( 895009...d26bac )
by Jonathan
14:31
created

CliHelper::variable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 4
nop 1
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
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;
14
15
/**
16
 * ReportingCloud
17
 *
18
 * @package TxTextControl\ReportingCloud
19
 * @author  Jonathan Maron (@JonathanMaron)
20
 */
21
class CliHelper
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
        return self::variable(self::USERNAME);
82
    }
83
84
    /**
85
     * Return the ReportingCloud password
86
     *
87
     * @return null|string
88
     */
89
    public static function password()
90
    {
91
        return self::variable(self::PASSWORD);
92
    }
93
94
    /**
95
     * Return the value of the PHP constant or environmental variable
96
     *
97
     * @param string $variable Variable
98
     *
99
     * @return null|string
100
     */
101
    protected static function variable($variable)
102
    {
103
        $ret = null;
104
105
        if (defined($variable)) {
106
            $value = constant($variable);
107
            $value = trim($value);
108
        } else {
109
            $value = getenv($variable);
110
            $value = trim($value);
111
        }
112
113
        if (!empty($value)) {
114
            $ret = $value;
115
        }
116
117
        return $ret;
118
    }
119
120
}