Passed
Push — master ( 3ac984...7fdc78 )
by Jonathan
03:15 queued 01:28
created

ChangelogGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNumberOfContributors() 0 5 1
A getNumberOfPullRequests() 0 4 1
A getNumberOfIssues() 0 4 1
A generate() 0 22 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 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(string $user, string $repository, string $milestone, OutputInterface $output) : void
29
    {
30 1
        $issues      = $this->issueRepository->getMilestoneIssues($user, $repository, $milestone);
31 1
        $issueGroups = $this->issueGrouper->groupIssues($issues);
32
33 1
        $output->writeln([
34 1
            sprintf('## %s', $milestone),
35 1
            '',
36 1
            sprintf('Total issues resolved: **%s**', $this->getNumberOfIssues($issues)),
37 1
            sprintf('Total pull requests resolved: **%s**', $this->getNumberOfPullRequests($issues)),
38 1
            sprintf('Total contributors: **%s**', $this->getNumberOfContributors($issues)),
39
        ]);
40
41 1
        foreach ($issueGroups as $issueGroup) {
42 1
            $output->writeln([
43 1
                '',
44 1
                sprintf('### %s', $issueGroup->getName()),
45 1
                '',
46
            ]);
47
48 1
            foreach ($issueGroup->getIssues() as $issue) {
49 1
                $output->writeln($issue->render());
50
            }
51
        }
52 1
    }
53
54
    /**
55
     * @param Issue[] $issues
56
     */
57 1
    private function getNumberOfIssues(array $issues) : int
58
    {
59
        return count(array_filter($issues, function (Issue $issue) : bool {
60 1
            return ! $issue->isPullRequest();
61 1
        }));
62
    }
63
64
    /**
65
     * @param Issue[] $issues
66
     */
67 1
    private function getNumberOfPullRequests(array $issues) : int
68
    {
69
        return count(array_filter($issues, function (Issue $issue) : bool {
70 1
            return $issue->isPullRequest();
71 1
        }));
72
    }
73
74
    /**
75
     * @param Issue[] $issues
76
     */
77 1
    private function getNumberOfContributors(array $issues) : int
78
    {
79
        return count(array_unique(array_map(function (Issue $issue) : string {
80 1
            return $issue->getUser();
81 1
        }, $issues)));
82
    }
83
}
84