Passed
Push — master ( c3d659...ab3de1 )
by Allan
03:38
created

ReleaseDetailsResolver::composePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 ReleaseDetailsResolver
9
{
10
    /**
11
     * @var \Vaimo\ComposerChangelogs\Validators\ConstraintValidator
12
     */
13
    private $constraintValidator;
14
15
    /**
16
     * @var string[][]
17
     */
18
    private $linkTemplates = array(
19
        'bitbucket' => array(
20
            'link' => '{base}/src/{higher}',
21
            'diff' => '{base}/branches/compare/{higher}..{lower}#diff'
22
        ),
23
        'github' => array(
24
            'link' => '{base}/tree/{higher}',
25
            'diff' => '{base}/compare/{lower}...{higher}'
26
        )
27
    );
28
29
    /**
30
     * @var string[]
31
     */
32
    private $dateQueryTemplates = array(
33
        '.hg' => 'hg log --rev \'{version}\' --template=\'{date|isodate}\'',
34
        '.git' => 'git log {version}~1..{version} --simplify-by-decoration --pretty="format:%ai"',
35
    );
36
    
37
    private $initialQueryTemplates = array(
38
        '.hg' => 'hg log -r "branch(default) and 0:" -l 1 --template "{node}"',
39
        '.git' => 'git rev-list --max-parents=0 HEAD'
40
    );
41
    
42
    public function __construct()
43
    {
44
        $this->constraintValidator = new \Vaimo\ComposerChangelogs\Validators\ConstraintValidator();
45
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
0 ignored issues
show
Bug Best Practice introduced by
The property pathUtils does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
    }
47
48
    public function resolveOverview(array $release)
49
    {
50
        $overviewLines = isset($release['overview'])
51
            ? (is_array($release['overview']) ? $release['overview'] : array($release['overview']))
52
            : array();
53
54
        $reducedLines = explode(PHP_EOL, implode(
55
            ' ',
56
            array_map(function ($line) {
57
                return !trim($line) ? PHP_EOL . PHP_EOL: $line;
58
            }, $overviewLines)
59
        ));
60
        
61
        return array(
62
            'version' => isset($release['version']) ? $release['version'] : '',
63
            'overview' => $overviewLines,
64
            'summary' => isset($release['summary']) ? $release['summary'] : '',
65
            'overview-reduced' => $reducedLines,
66
        );
67
    }
68
69
    public function resolveInitialCommitReference($repositoryRoot)
70
    {
71
        $result = '0';
72
73
        foreach ($this->initialQueryTemplates as $folder => $command) {
74
            if (!file_exists($this->pathUtils->composePath($repositoryRoot, $folder))) {
75
                continue;
76
            }
77
78
            $result = $this->getCommandStdIn($command, $repositoryRoot, '0');
79
        }
80
        
81
        return trim($result);
82
    }
83
    
84
    public function resolveReleaseLinks($repositoryUrl, $version, $lastVersion = '')
85
    {
86
        if (!$repositoryUrl) {
87
            return array();
88
        }
89
        
90
        $urlComponents = parse_url($repositoryUrl);
91
92
        if (!isset($urlComponents['host'])) {
93
            return array();
94
        }
95
96
        $hostCode = strtok($urlComponents['host'], '.');
97
98
        if (!isset($this->linkTemplates[$hostCode])) {
99
            return array();
100
        }
101
        
102
        $data = array();
103
104
        foreach ($this->linkTemplates[$hostCode] as $code => $template) {
105
            $data[$code] = str_replace(
106
                array('{base}', '{higher}', '{lower}'),
107
                array($repositoryUrl, $version, $lastVersion ? $lastVersion : '0'),
108
                $template
109
            );
110
        }
111
112
        return $data;
113
    }
114
    
115
    private function getCommandStdIn($command, $cwd, $default = '')
116
    {
117
        $process = new \Symfony\Component\Process\Process($command, $cwd);
118
119
        $process->setTimeout(null);
120
121
        try {
122
            $process->mustRun();
123
124
            return $process->getOutput();
125
        } catch (\Symfony\Component\Process\Exception\ProcessFailedException $exception) {
126
            return $default;
127
        }
128
    }
129
    
130
    public function resolveReleaseTime($repositoryRoot, $version)
131
    {
132
        if (!$repositoryRoot) {
133
            return array();
134
        }
135
        
136
        foreach ($this->dateQueryTemplates as $folder => $commandTemplate) {
137
            if (!file_exists($this->pathUtils->composePath($repositoryRoot, $folder))) {
138
                continue;
139
            }
140
141
            $result = $this->getCommandStdIn(
142
                str_replace('{version}', $version, $commandTemplate),
143
                $repositoryRoot,
144
                array()
145
            );
146
147
            if (!isset($result[1])) {
148
                return array();
149
            }
150
            
151
            $segments = explode(' ', trim($result));
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type array; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
            $segments = explode(' ', trim(/** @scrutinizer ignore-type */ $result));
Loading history...
152
153
            return array(
154
                'date' => array_shift($segments),
155
                'time' => array_shift($segments)
156
            );
157
        }
158
        
159
        return array();
160
    }
161
    
162
    public function resolveChangeGroups(array $release)
163
    {
164
        $reservedKeys = array(
165
            'overview',
166
            'version',
167
            'branch',
168
            'summary',
169
            'overview-reduced',
170
            'link',
171
            'diff',
172
            'date',
173
            'time'
174
        );
175
        
176
        return array_diff_key(
177
            $release,
178
            array_fill_keys($reservedKeys, true)
179
        );
180
    }
181
}
182