• 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

95.56
/src/Command/InitConfigCommand.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\Command;
25

26
use CPSIT\FrontendAssetHandler\ChattyInterface;
27
use CPSIT\FrontendAssetHandler\Config;
28
use CPSIT\FrontendAssetHandler\Exception;
29
use CPSIT\FrontendAssetHandler\Json;
30
use JsonException;
31
use Symfony\Component\Console;
32

33
use function sprintf;
34

35
/**
36
 * InitConfigCommand.
37
 *
38
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
39
 * @license GPL-3.0-or-later
40
 */
41
final class InitConfigCommand extends Console\Command\Command
42
{
43
    private const SUCCESSFUL = 0;
44
    private const ERROR_FAILED_ACTION = 1;
45

46
    private Console\Style\SymfonyStyle $io;
47

48
    /**
49
     * @param non-empty-list<Config\Initialization\Step\StepInterface> $initSteps
50
     */
51
    public function __construct(
5✔
52
        private readonly array $initSteps,
53
        private readonly Config\ConfigFacade $configFacade,
54
        private readonly Json\SchemaValidator $validator,
55
    ) {
56
        parent::__construct('init');
5✔
57
    }
58

59
    protected function configure(): void
5✔
60
    {
61
        $this->setDescription('Initialize a new configuration file to handle Frontend assets');
5✔
62

63
        $this->addOptionsFromConfigurableInitSteps();
5✔
64
    }
65

66
    protected function initialize(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): void
5✔
67
    {
68
        $this->io = new Console\Style\SymfonyStyle($input, $output);
5✔
69
    }
70

71
    /**
72
     * @throws Exception\InvalidConfigurationException
73
     * @throws Exception\MissingConfigurationException
74
     * @throws JsonException
75
     */
76
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
5✔
77
    {
78
        if ($input->isInteractive()) {
5✔
79
            $output->writeln([
5✔
80
                'Welcome to the Frontend Asset Handler!',
5✔
81
                'You can use the following command to initialize a new asset configuration for your Frontend assets.',
5✔
82
                'Follow the guide and answer all relevant questions to get started.',
5✔
83
            ]);
5✔
84
        }
85

86
        $request = Config\Initialization\InitializationRequest::fromCommandInput($input);
5✔
87

88
        // Run init steps
89
        foreach ($this->initSteps as $step) {
5✔
90
            if ($step instanceof ChattyInterface) {
5✔
91
                $step->setOutput($output);
4✔
92
            }
93

94
            if (!$step->execute($request)) {
5✔
95
                return $this->exitOnFailedInitStep($step);
1✔
96
            }
97
        }
98

99
        // Validate config
100
        if (!$this->validator->validate($request->getConfig())) {
4✔
101
            throw Exception\InvalidConfigurationException::asReported($this->validator->getLastValidationErrors()->errors());
1✔
102
        }
103

104
        // Write config
105
        if (!$this->configFacade->write($request->getConfig())) {
3✔
106
            throw Exception\FilesystemFailureException::forFailedWriteOperation($request->getConfigFile());
×
107
        }
108

109
        if ($input->isInteractive()) {
3✔
110
            $this->io->newLine();
3✔
111
        }
112

113
        $this->io->success(
3✔
114
            sprintf('Asset configuration was successfully written to %s', $request->getConfigFile()),
3✔
115
        );
3✔
116

117
        return self::SUCCESSFUL;
3✔
118
    }
119

120
    private function addOptionsFromConfigurableInitSteps(): void
5✔
121
    {
122
        $fullDefinition = $this->getDefinition();
5✔
123
        $nativeDefinition = $this->getNativeDefinition();
5✔
124

125
        foreach ($this->initSteps as $step) {
5✔
126
            if (!($step instanceof Config\Initialization\Step\InteractiveStepInterface)) {
5✔
127
                continue;
5✔
128
            }
129

130
            $options = $step->getInputOptions();
5✔
131

132
            // Add options to full and native definition
133
            $fullDefinition->addOptions($options);
5✔
134
            if ($nativeDefinition !== $fullDefinition) {
5✔
135
                $nativeDefinition->addOptions($options);
×
136
            }
137
        }
138
    }
139

140
    private function exitOnFailedInitStep(Config\Initialization\Step\StepInterface $failedAction): int
1✔
141
    {
142
        $this->io->error(
1✔
143
            sprintf('Action "%s" failed to initialize the configuration.', $failedAction::class),
1✔
144
        );
1✔
145

146
        return self::ERROR_FAILED_ACTION;
1✔
147
    }
148
}
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