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 ( a3eb0e...789152 )
by Drew
02:15
created

ComposerScriptRunner::verifyExtras()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5
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
        $fullPath = $this->getEnvFile();
192 12
        if (!$this->get('clobber') && file_exists($fullPath)) {
193 2
            $this->event->getIO()->write(sprintf('Env file `%s` already exists, skipping...', $fullPath));
194 2
            return;
195
        }
196
197 10
        foreach ($this->get('questions') as $question) {
198 10
            $this->verifyQuestion($question);
199
200 6
            $name = $question['name'];
201 6
            $prompt = $question['prompt'];
202 6
            $default = isset($question['default']) ? $question['default'] : '';
203 6
            $required = isset($question['required']) ?  (bool) $question['required'] : false;
204
205 6
            $this->builder->ask($name, $prompt, $default, $required);
206
        }
207
208 6
        $this->builder->run();
209 6
        $this->builder->write();
210 6
    }
211
212
    /**
213
     * Verify that the question has the minimum fields.
214
     *
215
     * @throws \InvalidArgumentException
216
     * @param array $question
217
     */
218 10
    protected function verifyQuestion(array $question)
219
    {
220 10
        if (!isset($question['name'])) {
221 2
            throw new \InvalidArgumentException(
222 2
                'The extra.php-env-builder.questions require all questions have a `name` property.'
223
            );
224
        }
225
226 8
        if (!isset($question['prompt'])) {
227 2
            throw new \InvalidArgumentException(
228 2
                'The extra.php-env-builder.questions require all questions have a `prompt` property.'
229
            );
230
        }
231 6
    }
232
}
233