Completed
Push — master ( aaccd5...c6e65b )
by Jonathan
10s
created

IssueRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMilestoneIssues() 0 17 2
A __construct() 0 4 1
A getLabels() 0 7 1
A getTitle() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use const ENT_COMPAT;
8
use function array_column;
9
use function htmlentities;
10
use function sort;
11
use function str_replace;
12
13
class IssueRepository
14
{
15
    /** @var IssueFetcher */
16
    private $issueFetcher;
17
18
    /** @var IssueFactory */
19
    private $issueFactory;
20
21 1
    public function __construct(IssueFetcher $issueFetcher, IssueFactory $issueFactory)
22
    {
23 1
        $this->issueFetcher = $issueFetcher;
24 1
        $this->issueFactory = $issueFactory;
25 1
    }
26
27
    /**
28
     * @return Issue[]
29
     */
30 1
    public function getMilestoneIssues(string $user, string $repository, string $milestone) : array
31
    {
32 1
        $issuesData = $this->issueFetcher->fetchMilestoneIssues($user, $repository, $milestone);
33
34 1
        $issues = [];
35
36 1
        foreach ($issuesData as $issue) {
37 1
            $issues[$issue['number']] = $this->issueFactory->create(
38 1
                $issue['number'],
39 1
                $this->getTitle($issue['title']),
40 1
                $issue['html_url'],
41 1
                $issue['user']['login'],
42 1
                $this->getLabels($issue['labels'])
43
            );
44
        }
45
46 1
        return $issues;
47
    }
48
49 1
    private function getTitle(string $title) : string
50
    {
51 1
        $title = htmlentities($title, ENT_COMPAT, 'UTF-8');
52 1
        $title = str_replace(['[', ']', '_'], ['&#91;', '&#92;', '&#95;'], $title);
53
54 1
        return $title;
55
    }
56
57
    /**
58
     * @param string[] $labels
59
     *
60
     * @return string[]
61
     */
62 1
    private function getLabels(array $labels) : array
63
    {
64 1
        $labels = array_column($labels, 'name');
65
66 1
        sort($labels);
67
68 1
        return $labels;
69
    }
70
}
71