• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / frontend-asset-handler / 15251341592

26 May 2025 10:12AM UTC coverage: 99.233%. Remained the same
15251341592

Pull #659

github

web-flow
Merge c6e7b1587 into 8b95d2cff
Pull Request #659: [TASK] Update phpunit/phpunit to v12

2459 of 2478 relevant lines covered (99.23%)

15.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.13
/src/Config/Initialization/Step/VcsConfigStep.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "cpsit/frontend-asset-handler".
7
 *
8
 * Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\FrontendAssetHandler\Config\Initialization\Step;
25

26
use CPSIT\FrontendAssetHandler\Config;
27
use CPSIT\FrontendAssetHandler\Vcs;
28
use Symfony\Component\Console;
29
use Symfony\Component\DependencyInjection;
30

31
use function array_replace_recursive;
32
use function is_array;
33
use function is_string;
34

35
/**
36
 * VcsConfigStep.
37
 *
38
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
39
 * @license GPL-3.0-or-later
40
 *
41
 * @internal
42
 */
43
final class VcsConfigStep extends BaseStep implements InteractiveStepInterface
44
{
45
    /**
46
     * @param DependencyInjection\ServiceLocator<Vcs\VcsProviderInterface> $vcsProviders
47
     */
48
    public function __construct(
10✔
49
        private readonly DependencyInjection\ServiceLocator $vcsProviders,
50
    ) {
51
        parent::__construct();
10✔
52
    }
53

54
    public function getInputOptions(): array
10✔
55
    {
56
        return [
10✔
57
            new Console\Input\InputOption(
10✔
58
                'vcs-type',
10✔
59
                null,
10✔
60
                Console\Input\InputOption::VALUE_REQUIRED,
10✔
61
                'Type of the asset\'s VCS, resolves to a supported VCS provider',
10✔
62
            ),
10✔
63
            new Console\Input\InputOption(
10✔
64
                'vcs-config-extra',
10✔
65
                null,
10✔
66
                Console\Input\InputOption::VALUE_REQUIRED,
10✔
67
                'Additional configuration for the asset VCS definition, should be a JSON-encoded string',
10✔
68
            ),
10✔
69
        ];
10✔
70
    }
71

72
    public function execute(Config\Initialization\InitializationRequest $request): bool
10✔
73
    {
74
        $input = $this->getInput($request);
10✔
75
        $io = new Console\Style\SymfonyStyle($input, $this->output);
10✔
76

77
        if ($input->isInteractive()) {
10✔
78
            $io->title('VCS');
10✔
79
        }
80

81
        // Early return if VCS should not be configured
82
        if (!$this->shouldConfigureVcs($request)) {
10✔
83
            return true;
1✔
84
        }
85

86
        // Initialize additional variables
87
        $additionalVariables = [];
9✔
88

89
        // VCS type
90
        $vcsType = $this->questionHelper->ask(
9✔
91
            $input,
9✔
92
            $this->output,
9✔
93
            $this->createChoiceQuestion(
9✔
94
                'Type',
9✔
95
                $this->vcsProviders->getProvidedServices(),
9✔
96
                $request->getOption('vcs-type'),
9✔
97
            ),
9✔
98
        );
9✔
99
        $request->setOption('vcs-type', $vcsType);
9✔
100

101
        // Additional variables for specific providers
102
        switch ($vcsType) {
103
            case Vcs\GitlabVcsProvider::getName():
9✔
104
                $this->requestAdditionalVariablesForGitlabVcsProvider($request, $additionalVariables);
7✔
105
                break;
7✔
106

107
            case Vcs\GithubVcsProvider::getName():
2✔
108
                $this->requestAdditionalVariablesForGithubVcsProvider($request, $additionalVariables);
2✔
109
                break;
2✔
110
        }
111

112
        // VCS config extra
113
        $vcsConfigExtra = $this->questionHelper->ask(
9✔
114
            $input,
9✔
115
            $this->output,
9✔
116
            $this->createQuestion(
9✔
117
                'Additional config',
9✔
118
                $request->getOption('vcs-config-extra'),
9✔
119
                validator: 'json',
9✔
120
            ),
9✔
121
        );
9✔
122

123
        if (is_string($vcsConfigExtra)) {
9✔
124
            $additionalVariables = array_replace_recursive(
1✔
125
                $additionalVariables,
1✔
126
                json_decode($vcsConfigExtra, true, flags: JSON_THROW_ON_ERROR),
1✔
127
            );
1✔
128
        }
129

130
        $request->setOption('vcs-config-extra', $additionalVariables);
9✔
131

132
        // Build VCS
133
        $this->buildVcs($request);
9✔
134

135
        return true;
9✔
136
    }
137

138
    /**
139
     * @param array<string, mixed> $additionalVariables
140
     */
141
    private function requestAdditionalVariablesForGitlabVcsProvider(
7✔
142
        Config\Initialization\InitializationRequest $request,
143
        array &$additionalVariables,
144
    ): void {
145
        $this->askForAdditionalVariable(
7✔
146
            $request,
7✔
147
            'Base URL',
7✔
148
            'base-url',
7✔
149
            $additionalVariables,
7✔
150
            'https://gitlab.com',
7✔
151
            ['notEmpty', 'url'],
7✔
152
        );
7✔
153

154
        $this->askForAdditionalVariable(
7✔
155
            $request,
7✔
156
            'Access token',
7✔
157
            'access-token',
7✔
158
            $additionalVariables,
7✔
159
            validator: 'notEmpty',
7✔
160
        );
7✔
161

162
        $this->askForAdditionalVariable(
7✔
163
            $request,
7✔
164
            'Project ID',
7✔
165
            'project-id',
7✔
166
            $additionalVariables,
7✔
167
            validator: 'integer',
7✔
168
        );
7✔
169
    }
170

171
    /**
172
     * @param array<string, mixed> $additionalVariables
173
     */
174
    private function requestAdditionalVariablesForGithubVcsProvider(
2✔
175
        Config\Initialization\InitializationRequest $request,
176
        array &$additionalVariables,
177
    ): void {
178
        $this->askForAdditionalVariable(
2✔
179
            $request,
2✔
180
            'Access token',
2✔
181
            'access-token',
2✔
182
            $additionalVariables,
2✔
183
            validator: 'notEmpty',
2✔
184
        );
2✔
185

186
        $this->askForAdditionalVariable(
2✔
187
            $request,
2✔
188
            'Repository (<owner>/<name>)',
2✔
189
            'repository',
2✔
190
            $additionalVariables,
2✔
191
            validator: 'notEmpty',
2✔
192
        );
2✔
193
    }
194

195
    private function buildVcs(Config\Initialization\InitializationRequest $request): void
9✔
196
    {
197
        $config = $request->getConfig();
9✔
198
        $definitionId = (int) $request->getOption('definition-id');
9✔
199
        $vcsType = $request->getOption('vcs-type');
9✔
200

201
        // Early return if VCS type is invalid
202
        if (!is_string($vcsType)) {
9✔
203
            return;
×
204
        }
205

206
        // Define default VCS configuration
207
        $vcsConfig = [
9✔
208
            'type' => $vcsType,
9✔
209
        ];
9✔
210

211
        // Merge additional VCS configuration
212
        $vcsConfigExtra = $request->getOption('vcs-config-extra');
9✔
213
        if (is_array($vcsConfigExtra)) {
9✔
214
            $vcsConfig = array_replace_recursive($vcsConfig, $vcsConfigExtra);
9✔
215
        }
216

217
        // Apply VCS
218
        $config['frontend-assets'][$definitionId]['vcs'] = $vcsConfig;
9✔
219
    }
220

221
    private function shouldConfigureVcs(Config\Initialization\InitializationRequest $request): bool
10✔
222
    {
223
        if (null !== $request->getOption('vcs-type')) {
10✔
224
            return true;
1✔
225
        }
226

227
        if ($this->getInput($request)->isInteractive()) {
9✔
228
            $this->output->writeln('The following VCS configuration is optional.');
9✔
229
        }
230

231
        return $this->askBooleanQuestion($request, 'Add VCS configuration?');
9✔
232
    }
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc