Completed
Push — master ( 3fd618...834a40 )
by Allan
02:37
created

BootstrapCommand::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 100
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 55
c 1
b 0
f 1
dl 0
loc 100
rs 8.6707
cc 5
nc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
11
use Vaimo\ComposerChangelogs\Composer\Plugin\Config as PluginConfig;
12
use Vaimo\ComposerChangelogs\Composer\Config as ComposerConfig;
13
use Vaimo\ComposerChangelogs\Composer\Files as ComposerFiles;
14
use Vaimo\ComposerChangelogs\Factories;
15
16
class BootstrapCommand extends \Composer\Command\BaseCommand
17
{
18
    protected function configure()
19
    {
20
        $this->setName('changelog:bootstrap');
21
22
        $this->setDescription('Add basic configuration for the usage of change-logs');
23
24
        $this->addArgument(
25
            'name',
26
            \Symfony\Component\Console\Input\InputArgument::OPTIONAL,
27
            'Targeted package name. Default: root package'
28
        );
29
30
        $this->addOption(
31
            '--type',
32
            null,
33
            \Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
34
            'Type of output (sphinx, md)',
35
            'md'
36
        );
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $packageName = $input->getArgument('name');
42
        $type = $input->getOption('type');
43
44
        $composerRuntime = $this->getComposer();
45
46
        $composerCtxFactory = new \Vaimo\ComposerChangelogs\Factories\ComposerContextFactory(
47
            $composerRuntime
48
        );
49
50
        $composerContext = $composerCtxFactory->create();
51
52
        $packageRepoFactory = new Factories\PackageResolverFactory($composerContext);
53
        $packageResolver = $packageRepoFactory->create();
54
55
        $outputGenerator = new \Vaimo\ComposerChangelogs\Console\OutputGenerator($output);
56
        
57
        try {
58
            $package = $packageResolver->resolvePackage(is_string($packageName) ? $packageName : '');
59
        } catch (PackageResolverException $exception) {
0 ignored issues
show
Bug introduced by
The type Vaimo\ComposerChangelogs...ackageResolverException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
            $outputGenerator->writeResolverException($exception);
61
62
            return null;
63
        }
64
65
        $output->writeln(
66
            sprintf('Bootstrapping changelogs for <info>%s</info>', $package->getName())
67
        );
68
        
69
        /** @var /Composer/Composer $composer */
70
        $composer = $composerContext->getLocalComposer();
71
72
        $packageInfoResolver = new \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver(
73
            $composer->getInstallationManager()
74
        );
75
        
76
        $configExtractor = new \Vaimo\ComposerChangelogs\Extractors\VendorConfigExtractor($packageInfoResolver);
77
        
78
        $config = $configExtractor->getConfig($package);
79
80
        if (isset($config[PluginConfig::ROOT])) {
81
            $rootPath = array(ComposerConfig::CONFIG_ROOT, PluginConfig::ROOT);
82
83
            $message = sprintf(
84
                'Configuration root (<comment>%s</comment>) already present in package config', 
85
                implode('/', $rootPath)
86
            );
87
            
88
            $output->writeln($message);
89
            
90
            return 0;
91
        }
92
        
93
        $installPath = $packageInfoResolver->getInstallPath($package);
94
95
        $config = $configExtractor->getPackageFullConfig($package);
96
97
        $paths = [
98
            'md' => 'CHANGELOG.md',
99
            'sphinx' => 'docs/changelog.rst'
100
        ];
101
102
        $update = array(
103
            ComposerConfig::CONFIG_ROOT => array(
104
                PluginConfig::ROOT => array(
105
                    'source' => 'changelog.json',
106
                    'output' => array(
107
                        $type => $paths[$type]
108
                    )   
109
                )
110
            )
111
        );
112
        
113
        $config = array_replace_recursive($config, $update);
114
115
        $pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
116
117
        $encodedConfig = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
118
        
119
        $pkgConfigPath = $pathUtils->composePath($installPath, ComposerFiles::PACKAGE_CONFIG);
120
        $chLogConfigPath = $pathUtils->composePath($installPath, 'changelog.json');
121
        
122
        file_put_contents($pkgConfigPath, $encodedConfig);
123
        
124
        if (!file_exists($chLogConfigPath)) {
125
            $changeLog = array(
126
                '_readme' => array(
127
                    'The contents of this file are used to generate CHANGELOG.md; It\'s kept in '
128
                        . 'JSON/parsable format to make it',
129
                    'possible to generate change-logs in other formats as well (when needed) and '
130
                        . 'to do automatic releases based on',
131
                    'added change-log records. More on how to use it: https://github.com/vaimo/composer-changelogs'
132
                )
133
            );
134
            
135
            file_put_contents($chLogConfigPath, json_encode($changeLog, JSON_PRETTY_PRINT));
136
        }
137
138
        $output->writeln('<info>Done</info>');
139
    }
140
}
141