Completed
Push — master ( 7fdc78...830b42 )
by Jonathan
11s
created

GenerateChangelogCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 51
ccs 36
cts 36
cp 1
crap 1
rs 9.4109
c 0
b 0
f 0

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
declare(strict_types=1);
4
5
namespace ChangelogGenerator\Command;
6
7
use ChangelogGenerator\ChangelogConfig;
8
use ChangelogGenerator\ChangelogGenerator;
9
use InvalidArgumentException;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Output\StreamOutput;
15
use function fopen;
16
use function getcwd;
17
use function sprintf;
18
19
class GenerateChangelogCommand extends Command
20
{
21
    /** @var ChangelogGenerator */
22
    private $changelogGenerator;
23
24 7
    public function __construct(ChangelogGenerator $changelogGenerator)
25
    {
26 7
        $this->changelogGenerator = $changelogGenerator;
27
28 7
        parent::__construct();
29 7
    }
30
31 7
    protected function configure() : void
32
    {
33
        $this
34 7
            ->setName('generate')
35 7
            ->setDescription('Generate a changelog markdown document from a GitHub milestone.')
36 7
            ->setHelp(<<<EOT
37 7
The <info>%command.name%</info> command generates a changelog markdown document from a GitHub milestone:
38
39
    <info>%command.full_name% --user=doctrine --repository=migrations --milestone=2.0</info>
40
41
You can filter the changelog by label names using the --label option:
42
43
    <info>%command.full_name% --user=doctrine --repository=migrations --milestone=2.0 --label=Enhancement --label=Bug</info>
44
EOT
45
            )
46 7
            ->addOption(
47 7
                'user',
48 7
                null,
49 7
                InputOption::VALUE_REQUIRED,
50 7
                'User that owns the repository.'
51
            )
52 7
            ->addOption(
53 7
                'repository',
54 7
                null,
55 7
                InputOption::VALUE_REQUIRED,
56 7
                'The repository owned by the user.'
57
            )
58 7
            ->addOption(
59 7
                'milestone',
60 7
                null,
61 7
                InputOption::VALUE_REQUIRED,
62 7
                'The milestone to build the changelog for.'
63
            )
64 7
            ->addOption(
65 7
                'file',
66 7
                null,
67 7
                InputOption::VALUE_OPTIONAL,
68 7
                'Write the changelog to a file.',
69 7
                false
70
            )
71 7
            ->addOption(
72 7
                'append',
73 7
                null,
74 7
                InputOption::VALUE_NONE,
75 7
                'Append the changelog to the file.'
76
            )
77 7
            ->addOption(
78 7
                'label',
79 7
                null,
80 7
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
81 7
                'The labels to generate a changelog for.'
82
            )
83
        ;
84 7
    }
85
86 5
    protected function execute(InputInterface $input, OutputInterface $output) : void
87
    {
88 5
        $user       = $input->getOption('user');
89 5
        $repository = $input->getOption('repository');
90 5
        $milestone  = $input->getOption('milestone');
91 5
        $labels     = $input->getOption('label');
92
93 5
        $changelogOutput = $this->getChangelogOutput($input, $output);
94
95 5
        $changelogConfig = new ChangelogConfig(
96 5
            $user,
97 5
            $repository,
98 5
            $milestone,
99 5
            $labels
100
        );
101
102 5
        $this->changelogGenerator->generate($changelogConfig, $changelogOutput);
103 5
    }
104
105
    /**
106
     * @return false|resource
107
     */
108 1
    protected function fopen(string $file, string $mode)
109
    {
110 1
        return fopen($file, $mode);
111
    }
112
113
    /**
114
     * @throws InvalidArgumentException
115
     */
116 2
    protected function createStreamOutput(string $file, bool $append) : StreamOutput
117
    {
118 2
        $handle = $this->fopen($file, $this->getFileHandleMode($append));
119
120 2
        if ($handle === false) {
121 1
            throw new InvalidArgumentException(sprintf('Could not open handle for %s', $file));
122
        }
123
124 1
        return new StreamOutput($handle);
125
    }
126
127 2
    private function getFileHandleMode(bool $append) : string
128
    {
129 2
        return $append ? 'a+' : 'w+';
130
    }
131
132 5
    private function getChangelogOutput(InputInterface $input, OutputInterface $output) : OutputInterface
133
    {
134 5
        $file   = $input->getOption('file');
135 5
        $append = (bool) $input->getOption('append');
136
137 5
        $changelogOutput = $output;
138
139 5
        if ($file !== false) {
140 3
            $changelogOutput = $this->createStreamOutput($this->getChangelogFilePath($file), $append);
141
        }
142
143 5
        return $changelogOutput;
144
    }
145
146 3
    private function getChangelogFilePath(?string $file) : string
147
    {
148 3
        return $file === null ? sprintf('%s/CHANGELOG.md', getcwd()) : $file;
149
    }
150
}
151