GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Builder::setIO()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dbtlr\PHPEnvBuilder;
4
5
use Dbtlr\PHPEnvBuilder\Exception\AskException;
6
use Dbtlr\PHPEnvBuilder\Exception\ConfigurationException;
7
use Dbtlr\PHPEnvBuilder\Exception\WritableException;
8
use Dbtlr\PHPEnvBuilder\IOHandler\CLImateHandler;
9
use Dbtlr\PHPEnvBuilder\IOHandler\IOHandlerInterface;
10
use League\CLImate\CLImate;
11
12
class Builder
13
{
14
    /** @var array */
15
    protected $questions = [];
16
17
    /** @var array */
18
    protected $config = [];
19
20
    /** @var string */
21
    protected $envFile;
22
23
    /** @var EnvLoader */
24
    protected $envLoader;
25
26
    /** @var EnvWriter */
27
    protected $envWriter;
28
29
    /** @var IO */
30
    protected $io;
31
32
    /** @var array */
33
    protected $answers = [];
34
35
36
    /**
37
     * Application constructor.
38
     *
39
     * This builds with the CLImateInterface as a default, but this
40
     * can be overridden by the setIOHandler() method.
41
     *
42
     * @throws ConfigurationException
43
     * @param string $envFile
44
     * @param array $config
45
     */
46 34
    public function __construct(string $envFile, array $config = [])
47
    {
48 34
        $this->envFile = $envFile;
49
50
        // Set the config to it's defaults.
51 34
        $this->config = [
52
            'verbose' => false,
53
            'loadEnv' => true,
54
        ];
55
56 34
        $this->setConfig($config);
57 34
        $this->setEnvLoader(new EnvLoader($envFile));
58 34
        $this->setEnvWriter(new EnvWriter(dirname($envFile), basename($envFile)));
59 34
        $this->setIO(new IO(new CLImateHandler(new CLImate())));
60 34
    }
61
62
    /**
63
     * Set some of the optional config elements.
64
     *
65
     * @throws ConfigurationException
66
     * @param array $config
67
     */
68 34
    public function setConfig(array $config)
69
    {
70 34
        foreach ($config as $key => $value) {
71
            switch ($key) {
72 20
                case 'verbose':
73 18
                case 'loadEnv':
74 18
                    if (!is_bool($value)) {
75 2
                        throw new ConfigurationException($key, 'This should be a boolean value.');
76
                    }
77
78 16
                    $this->config[$key] = $value;
79 16
                    break;
80
81
                default:
82 18
                    throw new ConfigurationException($key, 'This is an unknown configuration option.');
83
            }
84
        }
85 34
    }
86
87
    /**
88
     * Get the Builder's configuration options.
89
     *
90
     * @return array
91
     */
92 2
    public function getConfig()
93
    {
94 2
        return $this->config;
95
    }
96
97
    /**
98
     * Set the EnvLoader.
99
     *
100
     * @param EnvLoader $envLoader
101
     */
102 34
    public function setEnvLoader(EnvLoader $envLoader)
103
    {
104 34
        $this->envLoader = $envLoader;
105 34
    }
106
107
    /**
108
     * Add in an IOHanderInterface object.
109
     *
110
     * @param IOHandlerInterface $ioHandler
111
     */
112 14
    public function setIOHandler(IOHandlerInterface $ioHandler)
113
    {
114 14
        $this->io->setHandler($ioHandler);
115 14
    }
116
117
    /**
118
     * Set the IO.
119
     *
120
     * @param IO $io
121
     */
122 34
    public function setIO(IO $io)
123
    {
124 34
        $this->io = $io;
125 34
    }
126
127
    /**
128
     * Override the EnvWriter.
129
     *
130
     * @param EnvWriter $envWriter
131
     */
132 34
    public function setEnvWriter(EnvWriter $envWriter)
133
    {
134 34
        $this->envWriter = $envWriter;
135 34
    }
136
137
    /**
138
     * Run the builder
139
     *
140
     * @throws AskException
141
     * @return array
142
     */
143 12
    public function run()
144
    {
145 12
        $current = [];
146 12
        if ($this->config['loadEnv']) {
147 10
            $current = $this->envLoader->parse();
148
        }
149
150 12
        foreach ($this->questions as $name => $question) {
151
            // If We got an answer out of the loaded env, then let's use it.
152 8
            $default = isset($current[$name]) ? $current[$name] : $question['default'];
153
154
            // Ask the question.
155 8
            $this->answers[$name] = $this->io->ask($name, $question['prompt'], $default, $question['required']);
156
        }
157
158 12
        return $this->answers;
159
    }
160
161
    /**
162
     * Write the answers to a file.
163
     *
164
     * @return bool The status of the write operation.
165
     */
166 4
    public function write()
167
    {
168
        try {
169 4
            $this->envWriter->save($this->answers);
170 2
            $this->io->out(sprintf('Answers saved to %s', $this->envFile));
171 2
            return true;
172 2
        } catch (WritableException $e) {
173 2
            $this->io->out($e->getMessage());
174 2
            return false;
175
        }
176
    }
177
178
    /**
179
     * Set a question to ask the user.
180
     *
181
     * @param string $name The name of the env variable to read/use
182
     * @param string $prompt The prompt you want to give the user
183
     * @param string $default A default answer, if any.
184
     * @param bool $required Is a response required?
185
     */
186 10
    public function ask($name, $prompt, $default = '', $required = false)
187
    {
188 10
        $this->questions[$name] = [
189 10
            'prompt' => $prompt,
190 10
            'default' => $default,
191 10
            'required' => $required,
192
        ];
193 10
    }
194
195
    /**
196
     * Return all of the questions.
197
     *
198
     * @return array
199
     */
200 2
    public function getQuestions()
201
    {
202 2
        return $this->questions;
203
    }
204
}
205