Passed
Push — master ( 3e8e14...77675a )
by Yo
02:50
created

CreateConfigurationCommand::loadConfiguration()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 2
1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Command;
3
4
use \InvalidArgumentException;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Yoanm\ComposerConfigManager\Application\UpdateConfiguration;
10
use Yoanm\ComposerConfigManager\Application\UpdateConfigurationRequest;
11
use Yoanm\ComposerConfigManager\Application\Loader\ConfigurationLoaderInterface;
12
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
13
use Yoanm\ComposerConfigManager\Infrastructure\Command\Transformer\InputTransformer;
14
15
class CreateConfigurationCommand extends AbstractTemplatableCommand
16
{
17
    const NAME = 'create';
18
    const ARGUMENT_CONFIGURATION_DEST_FOLDER = 'destination';
19
20
    /** @var InputTransformer */
21
    private $inputTransformer;
22
    /** @var UpdateConfiguration */
23
    private $updateConfiguration;
24
25
    /**
26
     * @param InputTransformer             $inputTransformer
27
     * @param UpdateConfiguration          $updateConfiguration
28
     * @param ConfigurationLoaderInterface $configurationLoader
29
     */
30
    public function __construct(
31
        InputTransformer $inputTransformer,
32
        UpdateConfiguration $updateConfiguration,
33
        ConfigurationLoaderInterface $configurationLoader
34
    ) {
35
        parent::__construct($configurationLoader);
36
37
        $this->inputTransformer = $inputTransformer;
38
        $this->updateConfiguration = $updateConfiguration;
39
    }
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function configure()
44
    {
45
        $this
46
            ->setName(self::NAME)
47
            ->setDescription('Will create a composer configuration file')
48
            ->addArgument(
49
                self::ARGUMENT_CONFIGURATION_DEST_FOLDER,
50
                InputArgument::OPTIONAL,
51
                'Configuration file destination folder',
52
                '.'
53
            )
54
            ->addArgument(
55
                InputTransformer::KEY_PACKAGE_NAME,
56
                InputArgument::OPTIONAL,
57
                'Name for the composer package. Optionnal if a template containing package-name is given.'
58
            )
59
            ->addOption(
60
                InputTransformer::KEY_TYPE,
61
                null,
62
                InputOption::VALUE_REQUIRED,
63
                'Package type. Ex : "library" / "project"'
64
            )
65
            ->addOption(
66
                InputTransformer::KEY_LICENSE,
67
                null,
68
                InputOption::VALUE_REQUIRED,
69
                'Package license type'
70
            )
71
            ->addOption(
72
                InputTransformer::KEY_PACKAGE_VERSION,
73
                null,
74
                InputOption::VALUE_REQUIRED,
75
                'Package version number. Ex : "X.Y.Z"'
76
            )
77
            ->addOption(
78
                InputTransformer::KEY_DESCRIPTION,
79
                null,
80
                InputOption::VALUE_REQUIRED,
81
                'Package description'
82
            )
83
            ->addOption(
84
                InputTransformer::KEY_KEYWORD,
85
                null,
86
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
87
                'Package keywords'
88
            )
89
            ->addOption(
90
                InputTransformer::KEY_AUTHOR,
91
                null,
92
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
93
                'Package authors. Format "name[#email[#role]]'
94
            )
95
            ->addOption(
96
                InputTransformer::KEY_PROVIDED_PACKAGE,
97
                null,
98
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
99
                'List of packages provided by this one. Ex : "package-name#version"'
100
            )
101
            ->addOption(
102
                InputTransformer::KEY_SUGGESTED_PACKAGE,
103
                null,
104
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
105
                'List of packages suggested by this one. Ex : "package-name#description"'
106
            )
107
            ->addOption(
108
                InputTransformer::KEY_SUPPORT,
109
                null,
110
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
111
                'List of package support urls. Ex : "type#url"'
112
            )
113
            ->addOption(
114
                InputTransformer::KEY_AUTOLOAD_PSR0,
115
                null,
116
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
117
                'List of PSR-0 autoload. Ex : "namespace#path"'
118
            )
119
            ->addOption(
120
                InputTransformer::KEY_AUTOLOAD_PSR4,
121
                null,
122
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
123
                'List of PSR-4 autoload. Ex : "namespace#path"'
124
            )
125
            ->addOption(
126
                InputTransformer::KEY_AUTOLOAD_DEV_PSR0,
127
                null,
128
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
129
                'List of PSR-0 dev autoload. Ex : "namespace#path"'
130
            )
131
            ->addOption(
132
                InputTransformer::KEY_AUTOLOAD_DEV_PSR4,
133
                null,
134
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
135
                'List of PSR-4 dev autoload. Ex : "namespace#path"'
136
            )
137
            ->addOption(
138
                InputTransformer::KEY_REQUIRE,
139
                null,
140
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
141
                'List of required packages. Ex "vendor/package-name#~x.y"'
142
            )
143
            ->addOption(
144
                InputTransformer::KEY_REQUIRE_DEV,
145
                null,
146
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
147
                'List of required dev packages. Ex "vendor/package-name#~x.y"'
148
            )
149
            ->addOption(
150
                InputTransformer::KEY_SCRIPT,
151
                null,
152
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
153
                'List of scripts for the package. Ex : "script-name#command"'
154
            )
155
        ;
156
        parent::configure();
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    protected function execute(InputInterface $input, OutputInterface $output)
163
    {
164
        $configurationList = [];
165
        $templateConfiguration = $this->loadTemplateConfiguration($input);
166
        if ($templateConfiguration) {
167
            $configurationList[] = $templateConfiguration;
168
        }
169
        if ($newConfiguration = $this->loadConfiguration($input, $templateConfiguration)) {
170
            $configurationList[] = $newConfiguration;
171
        }
172
        $this->updateConfiguration->run(
173
            new UpdateConfigurationRequest(
174
                $configurationList,
175
                $input->getArgument(self::ARGUMENT_CONFIGURATION_DEST_FOLDER)
176
            )
177
        );
178
    }
179
180
    /**
181
     * @param InputInterface     $input
182
     * @param Configuration|null $templateConfiguration
183
     *
184
     * @return null|Configuration
185
     */
186
    protected function loadConfiguration(InputInterface $input, Configuration $templateConfiguration = null)
187
    {
188
        $packageName = $input->getArgument(InputTransformer::KEY_PACKAGE_NAME);
189
        if (null === $packageName) {
190
            if (null === $templateConfiguration
191
                || '' === trim($templateConfiguration->getPackageName())
192
            ) {
193
                throw new InvalidArgumentException(
194
                    sprintf(
195
                        'A package name must be given if no template containing package name is given !',
196
                        gettype($packageName)
197
                    )
198
                );
199
            }
200
201
            return null;
202
        }
203
204
        return $this->inputTransformer->fromCommandLine(
205
            [
206
                InputTransformer::KEY_PACKAGE_NAME => $packageName
207
            ] + $input->getOptions()
208
        );
209
    }
210
}
211