VcsDetailsResolver   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 16
eloc 57
c 3
b 0
f 1
dl 0
loc 124
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveInitialCommitReference() 0 13 3
A resolveReleaseLinks() 0 29 6
A resolveReleaseTime() 0 33 6
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Resolvers;
7
8
class VcsDetailsResolver
9
{
10
    /**
11
     * @var \Vaimo\ComposerChangelogs\Utils\SystemUtils
12
     */
13
    private $systemUtils;
14
15
    /**
16
     * @var \Vaimo\ComposerChangelogs\Utils\PathUtils
17
     */
18
    private $pathUtils;
19
20
    /**
21
     * @var string[][]
22
     */
23
    private $linkTemplates = array(
24
        'bitbucket' => array(
25
            'link' => '{base}/src/{higher}',
26
            'diff' => '{base}/branches/compare/{higher}..{lower}#diff'
27
        ),
28
        'github' => array(
29
            'link' => '{base}/tree/{higher}',
30
            'diff' => '{base}/compare/{lower}...{higher}'
31
        )
32
    );
33
34
    /**
35
     * @var string[]
36
     */
37
    private $dateQueryTemplates = array(
38
        '.hg' => 'hg log --rev \'{version}\' --template=\'{date|isodate}\'',
39
        '.git' => 'git log {version}~1..{version} --simplify-by-decoration --pretty=format:"%ai"',
40
    );
41
42
    private $headQueryTemplates = array(
43
        '.hg' => 'hg log -r "branch(default) and 0:" -l 1 --template "{node}"',
44
        '.git' => 'git rev-list --max-parents=0 HEAD'
45
    );
46
47
    public function __construct()
48
    {
49
        $this->systemUtils = new \Vaimo\ComposerChangelogs\Utils\SystemUtils();
50
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
51
    }
52
53
    public function resolveInitialCommitReference($repositoryRoot)
54
    {
55
        $result = '0';
56
57
        foreach ($this->headQueryTemplates as $folder => $command) {
58
            if (!file_exists($this->pathUtils->composePath($repositoryRoot, $folder))) {
59
                continue;
60
            }
61
62
            $result = $this->systemUtils->getCommandStdOut($command, $repositoryRoot, '0');
63
        }
64
65
        return trim($result);
66
    }
67
68
    public function resolveReleaseLinks($repositoryUrl, $version, $lastVersion = '')
69
    {
70
        if (!$repositoryUrl) {
71
            return array();
72
        }
73
74
        $urlComponents = parse_url($repositoryUrl);
75
76
        if (!isset($urlComponents['host'])) {
77
            return array();
78
        }
79
80
        $hostCode = strtok($urlComponents['host'], '.');
81
82
        if (!isset($this->linkTemplates[$hostCode])) {
83
            return array();
84
        }
85
86
        $data = array();
87
88
        foreach ($this->linkTemplates[$hostCode] as $code => $template) {
89
            $data[$code] = str_replace(
90
                array('{base}', '{higher}', '{lower}'),
91
                array($repositoryUrl, $version, $lastVersion ?: '0'),
92
                $template
93
            );
94
        }
95
96
        return $data;
97
    }
98
99
    public function resolveReleaseTime($repositoryRoot, $version)
100
    {
101
        if (!$repositoryRoot) {
102
            return array();
103
        }
104
105
        foreach ($this->dateQueryTemplates as $folder => $commandTemplate) {
106
            if (!file_exists($this->pathUtils->composePath($repositoryRoot, $folder))) {
107
                continue;
108
            }
109
110
            try {
111
                $result = $this->systemUtils->getCommandStdOut(
112
                    str_replace('{version}', $version, $commandTemplate),
113
                    $repositoryRoot
114
                );
115
            } catch (\Exception $exception) {
116
                return array();
117
            }
118
119
            if (!$result) {
120
                return array();
121
            }
122
123
            $segments = explode(' ', trim($result, " \t\n\r\0\x0B\""));
124
125
            return array(
126
                'date' => array_shift($segments),
127
                'time' => array_shift($segments)
128
            );
129
        }
130
131
        return array();
132
    }
133
}
134