Passed
Push — master ( 834a40...f4d4c2 )
by Allan
02:10
created

BootstrapCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 20
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Commands;
7
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputArgument as Argument;
11
use Symfony\Component\Console\Input\InputOption as Option;
12
13
use Vaimo\ComposerChangelogs\Composer\Plugin\Config as PluginConfig;
14
use Vaimo\ComposerChangelogs\Composer\Config as ComposerConfig;
15
16
class BootstrapCommand extends \Composer\Command\BaseCommand
17
{
18
    protected function configure()
19
    {
20
        $pluginConfig = new \Vaimo\ComposerChangelogs\Composer\Plugin\Config();
21
22
        $this->setName('changelog:bootstrap');
23
24
        $this->setDescription('Add basic configuration for the usage of generated change-logs');
25
26
        $this->addArgument(
27
            'name',
28
            Argument::OPTIONAL,
29
            'Targeted package name. Default: root package'
30
        );
31
32
        $this->addOption(
33
            '--format',
34
            null,
35
            Option::VALUE_OPTIONAL,
36
            sprintf('Format of the output (%s)', implode(', ', $pluginConfig->getAvailableFormats())),
37
            'md'
38
        );
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $packageName = $input->getArgument('name');
44
        $type = $input->getOption('type');
45
46
        $composerCtxFactory = new \Vaimo\ComposerChangelogs\Factories\ComposerContextFactory($this->getComposer());
47
        $composerCtx = $composerCtxFactory->create();
48
49
        $commandCtx = new \Vaimo\ComposerChangelogs\Console\Command\ExecutionContext($output, $composerCtx);
50
        
51
        $cfgResolverFactory = new \Vaimo\ComposerChangelogs\Factories\Changelog\ConfigResolverFactory($composerCtx);
52
        $cfgResolver = $cfgResolverFactory->create();
53
54
        $packageManager = new \Vaimo\ComposerChangelogs\Managers\PackageManager($composerCtx);
55
56
        $package = $commandCtx->resolvePackage($packageName);
57
58
        if ($package === null) {
59
            return 1;
60
        }
61
62
        $output->writeln(
63
            sprintf('Bootstrapping changelog generation for <info>%s</info>', $package->getName())
64
        );
65
        
66
        if ($cfgResolver->hasConfig($package)) {
67
            $rootPath = array(ComposerConfig::CONFIG_ROOT, PluginConfig::ROOT);
68
69
            $message = sprintf(
70
                'Configuration root (<comment>%s</comment>) already present in package config',
71
                implode('/', $rootPath)
72
            );
73
            
74
            $output->writeln($message);
75
            
76
            return 0;
77
        }
78
        
79
        try {
80
            $packageManager->bootstrapChangelogGeneration($package, $type);
81
        } catch (\Vaimo\ComposerChangelogs\Exceptions\UpdaterException $exception) {
82
            $message = sprintf('<error>%s</error>', $exception->getMessage());
83
            
84
            $output->writeln($message);
85
            
86
            return 1;
87
        }
88
        
89
        $output->writeln('<info>Done</info>');
90
        
91
        return 0;
92
    }
93
}
94