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 ( 775c4d...b8f882 )
by Drew
04:38
created

ComposerScriptRunner::getBasePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
     */
29
    public function __construct(Event $event)
30
    {
31
        $this->event = $event;
32
33
        $this->populateConfig();
34
        $this->createBuilder();
35
    }
36
37
    /**
38
     * Set a particular config value.
39
     *
40
     * @param string $key
41
     * @param mixed $value
42
     */
43
    public function set(string $key, $value)
44
    {
45
        $this->config[$key] = $value;
46
    }
47
48
    /**
49
     * Get the requested config var.
50
     *
51
     * @param string $key
52
     * @return mixed|null
53
     */
54
    public function get(string $key)
55
    {
56
        return isset($this->config[$key]) ? $this->config[$key] : null;
57
    }
58
59
    /**
60
     * @return |string
0 ignored issues
show
Documentation introduced by
The doc-type |string could not be parsed: Unknown type name "|" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
61
     */
62
    public function getBasePath()
63
    {
64
        if (!isset($this->basePath)) {
65
            $this->basePath = realpath(dirname(Factory::getComposerFile()));
66
        }
67
68
        return $this->basePath;
69
    }
70
71
    /**
72
     * @param string $basePath
73
     */
74
    public function setBasePath(string $basePath)
75
    {
76
        $this->basePath = $basePath;
77
    }
78
79
    /**
80
     * Get the full path to the env file.
81
     *
82
     * @return mixed|null|string
83
     */
84
    public function getEnvFile()
85
    {
86
        $basePath = $this->getBasePath();
87
        $envFile = $this->get('envFile');
88
        $startsWith = substr($envFile, 0, 1);
89
        if (!$startsWith !== '/' && $startsWith !== '~' && $startsWith !== '\\') {
90
            $envFile = $basePath . DIRECTORY_SEPARATOR . $envFile;
91
        }
92
93
        return $envFile;
94
    }
95
96
    /**
97
     * @throws \InvalidArgumentException
98
     */
99
    protected function populateConfig()
100
    {
101
        $extras = $this->event->getComposer()->getPackage()->getExtra();
102
103
        if (!isset($extras['php-env-builder'])) {
104
            throw new \InvalidArgumentException('The parameter handler needs to be configured through the extra.php-env-builder setting.');
105
        }
106
107
        $config = $extras['php-env-builder'];
108
        if (!is_array($config)) {
109
            throw new \InvalidArgumentException('The extra.php-env-builder setting must be an array or a configuration object.');
110
        }
111
112
        $this->set('envFile', isset($config['envFile']) ? $config['envFile'] : '.env');
113
        $this->set('clobber', isset($config['clobber']) ? $config['clobber'] : false);
114
        $this->set('verbose', isset($config['verbose']) ? $config['verbose'] : false);
115
        $this->set('loadEnv', isset($config['loadEnv']) ? $config['loadEnv'] : false);
116
117
        if (!isset($config['questions']) || !is_array($config['questions'])) {
118
            throw new \InvalidArgumentException('The extra.php-env-builder.questions setting must be an array of questions.');
119
        }
120
121
        $this->set('questions', $config['questions']);
122
    }
123
124
    /**
125
     * @throws \InvalidArgumentException
126
     */
127
    protected function createBuilder()
128
    {
129
        $this->builder = new Builder(
130
            $this->getEnvFile(),
131
            [
132
                'verbose' => $this->get('verbose'),
133
                'loadEnv' => $this->get('loadEnv')
134
            ]
135
        );
136
137
        $this->builder->setIOHandler(new ComposerIOHandler($this->event->getIO()));
138
    }
139
140
    /**
141
     * Override the Builder.
142
     *
143
     * @param Builder $builder
144
     */
145
    public function setBuilder(Builder $builder)
146
    {
147
        $this->builder = $builder;
148
    }
149
150
    /**
151
     * Build the config based on a composer's package.json file.
152
     *
153
     * @throws \InvalidArgumentException
154
     * @param Event $event
155
     */
156
    public static function build(Event $event)
157
    {
158
        $runner = new ComposerScriptRunner($event);
159
        $runner->run();
160
    }
161
162
    /**
163
     * Run the builder
164
     */
165
    public function run()
166
    {
167
        $fullPath = $this->getEnvFile();
168
        if (!$this->get('clobber') && file_exists($fullPath)) {
169
            $this->event->getIO()->write(sprintf('Env file `%s` already exists, skipping...', $fullPath));
170
            return;
171
        }
172
173
        foreach ($this->get('questions') as $question) {
174
            if (!isset($question['name'])) {
175
                throw new \InvalidArgumentException('The extra.php-env-builder.questions require all questions have a `name` property.');
176
            }
177
178
            if (!isset($question['prompt'])) {
179
                throw new \InvalidArgumentException('The extra.php-env-builder.questions require all questions have a `prompt` property.');
180
            }
181
182
            $name = $question['name'];
183
            $prompt = $question['prompt'];
184
            $default = isset($question['default']) ? $question['default'] : '';
185
            $required = isset($question['required']) ?  (bool) $question['required'] : false;
186
187
            $this->builder->ask($name, $prompt, $default, $required);
188
        }
189
190
        $this->builder->run();
191
        $this->builder->write();
192
    }
193
}
194