Completed
Push — master ( 11a376...8d38b3 )
by Andrii
03:54
created

AddTagLinks::generateTagHref()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 9
nc 4
nop 3
crap 4
1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\modifiers;
12
13
use hiqdev\chkipper\history\History;
14
15
/**
16
 * Modifier that adds links for tags.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class AddTagLinks extends AbstractModifier
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    public function run(History $history)
26
    {
27 2
        $prev = null;
28 2
        foreach (array_keys($history->getTags()) as $tag) {
29 2
            if ($prev && ($history->isLastTag($prev) || !$history->hasLink($prev))) {
30 2
                $history->addLink($prev, static::generateTagHref($history, $prev, $tag));
31 2
            }
32 2
            $prev = $tag;
33 2
        }
34 2
    }
35
36
    /**
37
     * Generate href to a version tag.
38
     * @param History $history history object
39
     * @param string $prev previous tag
40
     * @param string $curr current tag
41
     * @return string
42
     */
43 2
    public function generateTagHref(History $history, $prev, $curr)
44
    {
45 2
        $project = $history->getProject();
46 2
        if ($history->isInitTag($curr)) {
47 2
            if ($history->isLastTag($prev)) {
48 1
                return "https://github.com/$project/releases";
49
            }
50
51 1
            return "https://github.com/$project/releases/tag/$prev";
52
        }
53 1
        if ($history->isLastTag($prev)) {
54 1
            $prev = 'HEAD';
55 1
        }
56
57 1
        return "https://github.com/$project/compare/$curr...$prev";
58
    }
59
}
60