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.
Completed
Push — master ( 789152...394008 )
by Drew
01:53
created

ComposerScriptRunner::askQuestions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
namespace Dbtlr\PHPEnvBuilder;
4
5
use Composer\Script\Event;
6
use Composer\Factory;
7
use Dbtlr\PHPEnvBuilder\IOHandler\ComposerIOHandler;
8
9
class ComposerScriptRunner
10
{
11
    /** @var array */
12
    protected $config = [];
13
14
    /** @var Builder */
15
    protected $builder;
16
17
    /** @var Event */
18
    protected $event;
19
20
    /** @var string */
21
    protected $basePath;
22
23
    /**
24
     * ComposerScriptRunner constructor.
25
     *
26
     * @throws \InvalidArgumentException
27
     * @param Event $event
28
     * @param Builder|null $builder
29
     */
30 22
    public function __construct(Event $event, Builder $builder = null)
31
    {
32 22
        $this->event = $event;
33 22
        $this->builder = $builder;
34
35 22
        $this->populateConfig();
36
37 16
        if (!$this->builder) {
38 14
            $this->createBuilder();
39
        }
40 16
    }
41
42
    /**
43
     * Set a particular config value.
44
     *
45
     * @param string $key
46
     * @param mixed $value
47
     */
48 16
    public function set(string $key, $value)
49
    {
50 16
        $this->config[$key] = $value;
51 16
    }
52
53
    /**
54
     * Get the requested config var.
55
     *
56
     * @param string $key
57
     * @return mixed|null
58
     */
59 16
    public function get(string $key)
60
    {
61 16
        return isset($this->config[$key]) ? $this->config[$key] : null;
62
    }
63
64
    /**
65
     * @return string
66
     */
67 16
    public function getBasePath()
68
    {
69 16
        if (!isset($this->basePath)) {
70 16
            $this->basePath = realpath(dirname(Factory::getComposerFile()));
71
        }
72
73 16
        return $this->basePath;
74
    }
75
76
    /**
77
     * @param string $basePath
78
     */
79 14
    public function setBasePath(string $basePath)
80
    {
81 14
        $this->basePath = $basePath;
82 14
    }
83
84
    /**
85
     * Get the full path to the env file.
86
     *
87
     * @return mixed|null|string
88
     */
89 16
    public function getEnvFile()
90
    {
91 16
        $basePath = $this->getBasePath();
92 16
        $envFile = $this->get('envFile');
93 16
        $startsWith = substr($envFile, 0, 1);
94 16
        if (!$startsWith !== '/' && $startsWith !== '~' && $startsWith !== '\\') {
95 16
            $envFile = $basePath . DIRECTORY_SEPARATOR . $envFile;
96
        }
97
98 16
        return $envFile;
99
    }
100
101
    /**
102
     * @throws \InvalidArgumentException
103
     */
104 22
    protected function populateConfig()
105
    {
106 22
        $extras = $this->event->getComposer()->getPackage()->getExtra();
107
108 22
        $this->verifyExtras($extras);
109
110 16
        $config = $extras['php-env-builder'];
111
112 16
        $this->set('envFile', isset($config['envFile']) ? $config['envFile'] : '.env');
113 16
        $this->set('clobber', isset($config['clobber']) ? $config['clobber'] : false);
114 16
        $this->set('verbose', isset($config['verbose']) ? $config['verbose'] : false);
115 16
        $this->set('loadEnv', isset($config['loadEnv']) ? $config['loadEnv'] : false);
116 16
        $this->set('questions', $config['questions']);
117 16
    }
118
119
    /**
120
     * Verify that the extras a formatted properly and throw an exception if not.
121
     *
122
     * @throws \InvalidArgumentException
123
     * @param array $extras
124
     */
125 22
    protected function verifyExtras(array $extras)
126
    {
127 22
        if (!isset($extras['php-env-builder'])) {
128 2
            throw new \InvalidArgumentException(
129
                'The parameter handler needs to be configured through the ' .
130 2
                'extra.php-env-builder setting.'
131
            );
132
        }
133
134 20
        if (!is_array($extras['php-env-builder'])) {
135 2
            throw new \InvalidArgumentException(
136 2
                'The extra.php-env-builder setting must be an array or a configuration object.'
137
            );
138
        }
139
140 18
        if (!isset($extras['php-env-builder']['questions']) || !is_array($extras['php-env-builder']['questions'])) {
141 2
            throw new \InvalidArgumentException(
142 2
                'The extra.php-env-builder.questions setting must be an array of questions.'
143
            );
144
        }
145 16
    }
146
147
    /**
148
     * @throws \InvalidArgumentException
149
     */
150 14
    protected function createBuilder()
151
    {
152 14
        $this->builder = new Builder(
153 14
            $this->getEnvFile(),
154
            [
155 14
                'verbose' => $this->get('verbose'),
156 14
                'loadEnv' => $this->get('loadEnv')
157
            ]
158
        );
159
160 14
        $this->builder->setIOHandler(new ComposerIOHandler($this->event->getIO()));
161 14
    }
162
163
    /**
164
     * Override the Builder.
165
     *
166
     * @param Builder $builder
167
     */
168 14
    public function setBuilder(Builder $builder)
169
    {
170 14
        $this->builder = $builder;
171 14
    }
172
173
    /**
174
     * Build the config based on a composer's package.json file.
175
     *
176
     * @throws \InvalidArgumentException
177
     * @param Event $event
178
     * @param Builder $builder
179
     */
180 2
    public static function build(Event $event, Builder $builder = null)
181
    {
182 2
        $runner = new ComposerScriptRunner($event, $builder);
183 2
        $runner->run();
184 2
    }
185
186
    /**
187
     * Run the builder
188
     */
189 12
    public function run()
190
    {
191 12
        if ($this->shouldCancelOnClobber()) {
192 2
            return;
193
        }
194
195 10
        $this->askQuestions($this->get('questions'));
196
197 6
        $this->builder->run();
198 6
        $this->builder->write();
199 6
    }
200
201
    /**
202
     * Run through each of the questions and ask each one.
203
     *
204
     * @param array $questons
205
     */
206 10
    protected function askQuestions(array $questons)
207
    {
208 10
        foreach ($questons as $question) {
209 10
            $this->verifyQuestion($question);
210
211 6
            $name = $question['name'];
212 6
            $prompt = $question['prompt'];
213 6
            $default = isset($question['default']) ? $question['default'] : '';
214 6
            $required = isset($question['required']) ?  (bool) $question['required'] : false;
215
216 6
            $this->builder->ask($name, $prompt, $default, $required);
217
        }
218 6
    }
219
220
    /**
221
     * Verify that the question has the minimum fields.
222
     *
223
     * @throws \InvalidArgumentException
224
     * @param array $question
225
     */
226 10
    protected function verifyQuestion(array $question)
227
    {
228 10
        if (!isset($question['name'])) {
229 2
            throw new \InvalidArgumentException(
230 2
                'The extra.php-env-builder.questions require all questions have a `name` property.'
231
            );
232
        }
233
234 8
        if (!isset($question['prompt'])) {
235 2
            throw new \InvalidArgumentException(
236 2
                'The extra.php-env-builder.questions require all questions have a `prompt` property.'
237
            );
238
        }
239 6
    }
240
241
    /**
242
     * If we're clobbering and we're not supposed to, should we cancel?
243
     *
244
     * @return bool
245
     */
246 12
    protected function shouldCancelOnClobber()
247
    {
248 12
        $fullPath = $this->getEnvFile();
249 12
        if (!$this->get('clobber') && file_exists($fullPath)) {
250 2
            $this->event->getIO()->write(sprintf('Env file `%s` already exists, skipping...', $fullPath));
251 2
            return true;
252
        }
253
254 10
        return false;
255
    }
256
}
257