Completed
Push — master ( 76dc42...e93ba9 )
by Yo
02:39
created

CreateConfigurationCommand::loadConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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