Failed Conditions
Push — master ( 41301b...74c660 )
by Jonathan
19s queued 11s
created

ChangelogGenerator::buildMarkdownHeaderText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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