Passed
Branch development-2.0 (2bf301)
by Jonathan
05:03
created

Helper::varExportToFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 21
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Console;
16
17
/**
18
 * ReportingCloud console helper (used only for tests and demos)
19
 *
20
 * @package TxTextControl\ReportingCloud
21
 * @author  Jonathan Maron (@JonathanMaron)
22
 */
23
class Helper extends AbstractHelper
24
{
25
    /**
26
     * Check that either the API key has been defined in environment variables
27
     *
28
     * @return bool
29
     */
30
    public static function checkCredentials(): bool
31
    {
32
        if (null !== self::apiKey()) {
33
            return true;
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * Return the value of the PHP constant or environmental variable
41
     *
42
     * @param string $variable Variable
43
     *
44
     * @return string|null
45
     */
46
    protected static function variable(string $variable): ?string
47
    {
48
        $ret = null;
49
50
        if (defined($variable)) {
51
            $value = constant($variable);
52
        } else {
53
            $value = getenv($variable);
54
        }
55
56
        $value = trim($value);
57
58
        if (strlen($value) > 0) {
59
            $ret = $value;
60
        }
61
62
        return $ret;
63
    }
64
65
    /**
66
     * Return the ReportingCloud API key
67
     *
68
     * @return string|null
69
     */
70
    public static function apiKey(): ?string
71
    {
72
        return self::variable(self::API_KEY);
73
    }
74
75
    /**
76
     * Return error message explaining how to configure PHP constant or environmental variables
77
     *
78
     * @return string
79
     */
80
    public static function errorMessage(): string
81
    {
82
        $ret
83
            = <<<END
84
85
Error: ReportingCloud API key not defined.
86
87
In order to execute this script, you must first set your ReportingCloud
88
API key.
89
90
There are two ways in which you can do this:
91
92
1) Define the following PHP constant:
93
94
    define('REPORTING_CLOUD_API_KEY', 'your-api-key');
95
96
2) Set environmental variable (for example in .bashrc)
97
    
98
    export REPORTING_CLOUD_API_KEY='your-api-key'
99
100
Note, these instructions apply only to the demo scripts and phpunit tests.
101
When you use ReportingCloud in your application, set credentials in your
102
constructor or using the setApiKey(\$apiKey). For an example, see 
103
'/demo/instantiation.php'.
104
105
For further assistance and customer service please refer to:
106
107
    https://www.reporting.cloud
108
109
END;
110
111
        return $ret;
112
    }
113
}
114