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

ChangelogGenerator::generate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 16
cts 16
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
use function array_filter;
9
use function array_map;
10
use function array_unique;
11
use function count;
12
use function sprintf;
13
14
class ChangelogGenerator
15
{
16
    /** @var IssueRepository */
17
    private $issueRepository;
18
19
    /** @var IssueGrouper */
20
    private $issueGrouper;
21
22 1
    public function __construct(IssueRepository $issueRepository, IssueGrouper $issueGrouper)
23
    {
24 1
        $this->issueRepository = $issueRepository;
25 1
        $this->issueGrouper    = $issueGrouper;
26 1
    }
27
28 1
    public function generate(
29
        ChangelogConfig $changelogConfig,
30
        OutputInterface $output
31
    ) : void {
32 1
        $issues      = $this->issueRepository->getMilestoneIssues($changelogConfig);
33 1
        $issueGroups = $this->issueGrouper->groupIssues($issues);
34
35 1
        $output->writeln([
36 1
            sprintf('## %s', $changelogConfig->getMilestone()),
37 1
            '',
38 1
            sprintf('Total issues resolved: **%s**', $this->getNumberOfIssues($issues)),
39 1
            sprintf('Total pull requests resolved: **%s**', $this->getNumberOfPullRequests($issues)),
40 1
            sprintf('Total contributors: **%s**', $this->getNumberOfContributors($issues)),
41
        ]);
42
43 1
        foreach ($issueGroups as $issueGroup) {
44 1
            $output->writeln([
45 1
                '',
46 1
                sprintf('### %s', $issueGroup->getName()),
47 1
                '',
48
            ]);
49
50 1
            foreach ($issueGroup->getIssues() as $issue) {
51 1
                $output->writeln($issue->render());
52
            }
53
        }
54 1
    }
55
56
    /**
57
     * @param Issue[] $issues
58
     */
59 1
    private function getNumberOfIssues(array $issues) : int
60
    {
61
        return count(array_filter($issues, function (Issue $issue) : bool {
62 1
            return ! $issue->isPullRequest();
63 1
        }));
64
    }
65
66
    /**
67
     * @param Issue[] $issues
68
     */
69 1
    private function getNumberOfPullRequests(array $issues) : int
70
    {
71
        return count(array_filter($issues, function (Issue $issue) : bool {
72 1
            return $issue->isPullRequest();
73 1
        }));
74
    }
75
76
    /**
77
     * @param Issue[] $issues
78
     */
79 1
    private function getNumberOfContributors(array $issues) : int
80
    {
81
        return count(array_unique(array_map(function (Issue $issue) : string {
82 1
            return $issue->getUser();
83 1
        }, $issues)));
84
    }
85
}
86