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

IssueRepository::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 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