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 ( 8038d8...5353cd )
by Drew
09:45
created

ComposerScriptRunner::run()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 8.1635
c 0
b 0
f 0
cc 8
nc 8
nop 0
crap 8
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 18
    public function set(string $key, $value)
49
    {
50 18
        $this->config[$key] = $value;
51 18
    }
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
        if (!isset($extras['php-env-builder'])) {
109 2
            throw new \InvalidArgumentException(
110
                'The parameter handler needs to be configured through the ' .
111 2
                'extra.php-env-builder setting.'
112
            );
113
        }
114
115 20
        $config = $extras['php-env-builder'];
116 20
        if (!is_array($config)) {
117 2
            throw new \InvalidArgumentException(
118 2
                'The extra.php-env-builder setting must be an array or a configuration object.'
119
            );
120
        }
121
122 18
        $this->set('envFile', isset($config['envFile']) ? $config['envFile'] : '.env');
123 18
        $this->set('clobber', isset($config['clobber']) ? $config['clobber'] : false);
124 18
        $this->set('verbose', isset($config['verbose']) ? $config['verbose'] : false);
125 18
        $this->set('loadEnv', isset($config['loadEnv']) ? $config['loadEnv'] : false);
126
127 18
        if (!isset($config['questions']) || !is_array($config['questions'])) {
128 2
            throw new \InvalidArgumentException(
129 2
                'The extra.php-env-builder.questions setting must be an array of questions.'
130
            );
131
        }
132
133 16
        $this->set('questions', $config['questions']);
134 16
    }
135
136
    /**
137
     * @throws \InvalidArgumentException
138
     */
139 14
    protected function createBuilder()
140
    {
141 14
        $this->builder = new Builder(
142 14
            $this->getEnvFile(),
143
            [
144 14
                'verbose' => $this->get('verbose'),
145 14
                'loadEnv' => $this->get('loadEnv')
146
            ]
147
        );
148
149 14
        $this->builder->setIOHandler(new ComposerIOHandler($this->event->getIO()));
150 14
    }
151
152
    /**
153
     * Override the Builder.
154
     *
155
     * @param Builder $builder
156
     */
157 14
    public function setBuilder(Builder $builder)
158
    {
159 14
        $this->builder = $builder;
160 14
    }
161
162
    /**
163
     * Build the config based on a composer's package.json file.
164
     *
165
     * @throws \InvalidArgumentException
166
     * @param Event $event
167
     * @param Builder $builder
168
     */
169 2
    public static function build(Event $event, Builder $builder = null)
170
    {
171 2
        $runner = new ComposerScriptRunner($event, $builder);
172 2
        $runner->run();
173 2
    }
174
175
    /**
176
     * Run the builder
177
     */
178 12
    public function run()
179
    {
180 12
        $fullPath = $this->getEnvFile();
181 12
        if (!$this->get('clobber') && file_exists($fullPath)) {
182 2
            $this->event->getIO()->write(sprintf('Env file `%s` already exists, skipping...', $fullPath));
183 2
            return;
184
        }
185
186 10
        foreach ($this->get('questions') as $question) {
187 10
            if (!isset($question['name'])) {
188 2
                throw new \InvalidArgumentException(
189 2
                    'The extra.php-env-builder.questions require all questions have a `name` property.'
190
                );
191
            }
192
193 8
            if (!isset($question['prompt'])) {
194 2
                throw new \InvalidArgumentException(
195 2
                    'The extra.php-env-builder.questions require all questions have a `prompt` property.'
196
                );
197
            }
198
199 6
            $name = $question['name'];
200 6
            $prompt = $question['prompt'];
201 6
            $default = isset($question['default']) ? $question['default'] : '';
202 6
            $required = isset($question['required']) ?  (bool) $question['required'] : false;
203
204 6
            $this->builder->ask($name, $prompt, $default, $required);
205
        }
206
207 6
        $this->builder->run();
208 6
        $this->builder->write();
209 6
    }
210
}
211