ChangelogGenerator::generate()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 32
rs 9.6666
ccs 19
cts 19
cp 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
use function array_filter;
10
use function array_map;
11
use function array_unique;
12
use function array_values;
13
use function count;
14
use function mb_strlen;
15
use function sprintf;
16
use function str_repeat;
17
18
use const PHP_EOL;
19
20
class ChangelogGenerator
21
{
22
    private IssueRepository $issueRepository;
23
24
    private IssueGrouper $issueGrouper;
25
26 3
    public function __construct(IssueRepository $issueRepository, IssueGrouper $issueGrouper)
27
    {
28 3
        $this->issueRepository = $issueRepository;
29 3
        $this->issueGrouper    = $issueGrouper;
30 3
    }
31
32 3
    public function generate(
33
        ChangelogConfig $changelogConfig,
34
        OutputInterface $output
35
    ): void {
36 3
        $issues      = $this->issueRepository->getMilestoneIssues($changelogConfig);
37 3
        $issueGroups = $this->issueGrouper->groupIssues($issues, $changelogConfig);
38
39 3
        $output->writeln([
40 3
            $this->buildMarkdownHeaderText($changelogConfig->getMilestone(), '='),
41 3
            '',
42 3
            sprintf('- Total issues resolved: **%s**', $this->getNumberOfIssues($issues)),
43 3
            sprintf('- Total pull requests resolved: **%s**', $this->getNumberOfPullRequests($issues)),
44 3
            sprintf('- Total contributors: **%s**', $this->getNumberOfContributors($issues)),
45
        ]);
46
47 3
        foreach ($issueGroups as $issueGroup) {
48 3
            $output->writeln([
49 3
                '',
50 3
                $this->buildMarkdownHeaderText($issueGroup->getName(), '-'),
51 3
                '',
52
            ]);
53
54 3
            foreach ($issueGroup->getIssues() as $issue) {
55 3
                $output->writeln($issue->render());
56
            }
57
        }
58
59 3
        if ($changelogConfig->showContributors()) {
60 3
            $this->outputContributors($output, $issues);
61
        }
62
63 3
        $output->writeln('');
64 3
    }
65
66
    /**
67
     * @param Issue[] $issues
68
     */
69 3
    private function outputContributors(OutputInterface $output, array $issues): void
70
    {
71 3
        $contributors = $this->buildContributorsList($issues);
72
73 3
        $output->writeln([
74 3
            '',
75 3
            $this->buildMarkdownHeaderText('Contributors', '-'),
76 3
            '',
77
        ]);
78
79 3
        foreach ($contributors as $contributor) {
80 3
            $output->writeln(sprintf(' - [@%s](https://github.com/%s)', $contributor, $contributor));
81
        }
82 3
    }
83
84
    /**
85
     * @param Issue[] $issues
86
     *
87
     * @return string[]
88
     */
89 3
    private function buildContributorsList(array $issues): array
90
    {
91 3
        $contributors = [];
92
93 3
        foreach ($issues as $issue) {
94 3
            foreach ($issue->getContributors() as $contributor) {
95 3
                $contributors[$contributor] = $contributor;
96
            }
97
        }
98
99 3
        return array_values($contributors);
100
    }
101
102 3
    private function buildMarkdownHeaderText(string $header, string $headerCharacter): string
103
    {
104 3
        return sprintf(
105 3
            '%s%s%s',
106 3
            $header,
107 3
            PHP_EOL,
108 3
            str_repeat($headerCharacter, mb_strlen($header))
109
        );
110
    }
111
112
    /**
113
     * @param Issue[] $issues
114
     */
115 3
    private function getNumberOfIssues(array $issues): int
116
    {
117 3
        return count(array_filter($issues, static function (Issue $issue): bool {
118 3
            return ! $issue->isPullRequest();
119 3
        }));
120
    }
121
122
    /**
123
     * @param Issue[] $issues
124
     */
125 3
    private function getNumberOfPullRequests(array $issues): int
126
    {
127 3
        return count(array_filter($issues, static function (Issue $issue): bool {
128 3
            return $issue->isPullRequest();
129 3
        }));
130
    }
131
132
    /**
133
     * @param Issue[] $issues
134
     */
135 3
    private function getNumberOfContributors(array $issues): int
136
    {
137 3
        return count(array_unique(array_map(static function (Issue $issue): string {
138 3
            return $issue->getUser();
139 3
        }, $issues)));
140
    }
141
}
142