Completed
Push — master ( 065f8e...3ac984 )
by Jonathan
10s
created

ChangelogGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNumberOfPullRequests() 0 4 1
A getNumberOfIssues() 0 4 1
A generate() 0 21 3
A __construct() 0 4 1
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 count;
10
use function sprintf;
11
12
class ChangelogGenerator
13
{
14
    /** @var IssueRepository */
15
    private $issueRepository;
16
17
    /** @var IssueGrouper */
18
    private $issueGrouper;
19
20 1
    public function __construct(IssueRepository $issueRepository, IssueGrouper $issueGrouper)
21
    {
22 1
        $this->issueRepository = $issueRepository;
23 1
        $this->issueGrouper    = $issueGrouper;
24 1
    }
25
26 1
    public function generate(string $user, string $repository, string $milestone, OutputInterface $output) : void
27
    {
28 1
        $issues      = $this->issueRepository->getMilestoneIssues($user, $repository, $milestone);
29 1
        $issueGroups = $this->issueGrouper->groupIssues($issues);
30
31 1
        $output->writeln([
32 1
            sprintf('## %s', $milestone),
33 1
            '',
34 1
            sprintf('Total issues resolved: **%s**', $this->getNumberOfIssues($issues)),
35 1
            sprintf('Total pull requests resolved: **%s**', $this->getNumberOfPullRequests($issues)),
36
        ]);
37
38 1
        foreach ($issueGroups as $issueGroup) {
39 1
            $output->writeln([
40 1
                '',
41 1
                sprintf('### %s', $issueGroup->getName()),
42 1
                '',
43
            ]);
44
45 1
            foreach ($issueGroup->getIssues() as $issue) {
46 1
                $output->writeln($issue->render());
47
            }
48
        }
49 1
    }
50
51
    /**
52
     * @param Issue[] $issues
53
     */
54 1
    private function getNumberOfIssues(array $issues) : int
55
    {
56
        return count(array_filter($issues, function (Issue $issue) : bool {
57 1
            return ! $issue->isPullRequest();
58 1
        }));
59
    }
60
61
    /**
62
     * @param Issue[] $issues
63
     */
64 1
    private function getNumberOfPullRequests(array $issues) : int
65
    {
66
        return count(array_filter($issues, function (Issue $issue) : bool {
67 1
            return $issue->isPullRequest();
68 1
        }));
69
    }
70
}
71