Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ChangelogEntryCommand
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Console\Command;
21
22use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
23use FastForward\DevTools\Changelog\Document\ChangelogDocument;
24use FastForward\DevTools\Changelog\Entry\ChangelogEntryType;
25use FastForward\DevTools\Changelog\Manager\ChangelogManagerInterface;
26use FastForward\DevTools\Console\Input\HasJsonOption;
27use FastForward\DevTools\Filesystem\FilesystemInterface;
28use Psr\Log\LoggerInterface;
29use Symfony\Component\Console\Attribute\AsCommand;
30use Symfony\Component\Console\Command\Command;
31use Symfony\Component\Console\Input\InputArgument;
32use Symfony\Component\Console\Input\InputInterface;
33use Symfony\Component\Console\Input\InputOption;
34use Symfony\Component\Console\Output\OutputInterface;
35
36/**
37 * Inserts a changelog entry into the managed changelog file.
38 */
39#[AsCommand(
40    name: 'changelog:entry',
41    description: 'Adds a changelog entry to Unreleased or a specific version section.'
42)]
43 class ChangelogEntryCommand extends Command
44{
45    use HasJsonOption;
46    use LogsCommandResults;
47
48    /**
49     * @param FilesystemInterface $filesystem
50     * @param ChangelogManagerInterface $changelogManager
51     * @param LoggerInterface $logger
52     */
53    public function __construct(
54        private  FilesystemInterface $filesystem,
55        private  ChangelogManagerInterface $changelogManager,
56        private  LoggerInterface $logger,
57    ) {
58        parent::__construct();
59    }
60
61    /**
62     * Configures the entry authoring arguments and options.
63     */
64    protected function configure(): void
65    {
66        $this->setHelp(
67            'This command appends one categorized changelog entry to the selected changelog file so it can be'
68            . ' reused by local authoring flows and skills.'
69        );
70
71        $this->addJsonOption()
72            ->addArgument(
73                name: 'message',
74                mode: InputArgument::REQUIRED,
75                description: 'The changelog entry text to append.',
76            )
77            ->addOption(
78                name: 'type',
79                shortcut: 't',
80                mode: InputOption::VALUE_REQUIRED,
81                description: 'The changelog category (added, changed, deprecated, removed, fixed, security).',
82                default: 'added',
83            )
84            ->addOption(
85                name: 'release',
86                mode: InputOption::VALUE_REQUIRED,
87                description: 'The target release section. Defaults to Unreleased.',
88                default: ChangelogDocument::UNRELEASED_VERSION,
89            )
90            ->addOption(
91                name: 'date',
92                mode: InputOption::VALUE_REQUIRED,
93                description: 'Optional release date for published sections in YYYY-MM-DD format.',
94            )
95            ->addOption(
96                name: 'file',
97                mode: InputOption::VALUE_REQUIRED,
98                description: 'Path to the changelog file.',
99                default: 'CHANGELOG.md',
100            );
101    }
102
103    /**
104     * Appends a changelog entry to the requested section.
105     *
106     * @param InputInterface $input
107     * @param OutputInterface $output
108     */
109    protected function execute(InputInterface $input, OutputInterface $output): int
110    {
111        $file = $this->filesystem->getAbsolutePath((string) $input->getOption('file'));
112        $type = ChangelogEntryType::fromInput((string) $input->getOption('type'));
113        $version = (string) $input->getOption('release');
114        $date = $input->getOption('date');
115        $message = (string) $input->getArgument('message');
116
117        $this->changelogManager->addEntry($file, $type, $message, $version, \is_string($date) ? $date : null);
118
119        return $this->success(
120            'Added {type} changelog entry to [{release}] in {absolute_file}.',
121            $input,
122            [
123                'absolute_file' => $file,
124                'type' => strtolower($type->value),
125                'release' => $version,
126                'date' => \is_string($date) ? $date : null,
127                'message' => $message,
128            ],
129        );
130    }
131}