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.

ComposerScriptRunner   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 7
dl 0
loc 251
ccs 91
cts 91
cp 1
rs 9.44
c 0
b 0
f 0

15 Methods

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