Passed
Branch master (d39919)
by Allan
03:04 queued 01:07
created

VcsDetailsResolver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 54
dl 0
loc 120
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveInitialCommitReference() 0 13 3
A __construct() 0 4 1
A resolveReleaseLinks() 0 29 6
A resolveReleaseTime() 0 29 5
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 $initialQueryTemplates = 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->initialQueryTemplates as $folder => $command) {
58
            if (!file_exists($this->pathUtils->composePath($repositoryRoot, $folder))) {
59
                continue;
60
            }
61
62
            $result = $this->systemUtils->getCommandStdIn($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 ? $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
            $result = $this->systemUtils->getCommandStdIn(
111
                str_replace('{version}', $version, $commandTemplate),
112
                $repositoryRoot
113
            );
114
115
            if (!$result) {
116
                return array();
117
            }
118
            
119
            $segments = explode(' ', trim($result));
120
121
            return array(
122
                'date' => array_shift($segments),
123
                'time' => array_shift($segments)
124
            );
125
        }
126
        
127
        return array();
128
    }
129
}
130