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

RemoteSourceResolver::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\Url;
7
8
class RemoteSourceResolver implements \Vaimo\ComposerChangelogs\Interfaces\UrlResolverInterface
9
{
10
    /**
11
     * @var \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver
12
     */
13
    private $packageInfoResolver;
14
15
    /**
16
     * @var \Vaimo\ComposerChangelogs\Normalizers\UrlNormalizer
17
     */
18
    private $urlNormalizer;
19
20
    /**
21
     * @var \Vaimo\ComposerChangelogs\Utils\PathUtils
22
     */
23
    private $pathUtils;
24
    
25
    /**
26
     * @param \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver,
27
     */
28
    public function __construct(
29
        \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver
30
    ) {
31
        $this->packageInfoResolver = $packageInfoResolver;
32
33
        $this->urlNormalizer = new \Vaimo\ComposerChangelogs\Normalizers\UrlNormalizer();
34
        
35
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
36
    }
37
38
    public function resolveForPackage(\Composer\Package\PackageInterface $package)
39
    {
40
        if (!$package instanceof \Composer\Package\CompletePackageInterface) {
41
            return '';
42
        }
43
44
        $support = $package->getSupport();
45
46
        $queryCommands = array(
47
            '.hg' => 'hg path default',
48
            '.git' => 'git remote get-url origin'
49
        );
50
        
51
        if (!isset($support['source'])) {
52
            foreach ($queryCommands as $folder => $command) {
53
                $sourcePath = $this->packageInfoResolver->getSourcePath($package);
54
55
                if (!file_exists($this->pathUtils->composePath($sourcePath, $folder))) {
56
                    continue;
57
                }
58
59
                $process = new \Symfony\Component\Process\Process($command, $sourcePath);
60
61
                $process->setTimeout(null);
62
63
                try {
64
                    $process->mustRun();
65
66
                    $result = $process->getOutput();
67
                } catch (\Symfony\Component\Process\Exception\ProcessFailedException $exception) {
68
                    return '';
69
                }
70
71
                return $this->urlNormalizer->assureHttpAccessibility($result);
72
            }
73
        }
74
75
76
        return isset($support['source']) ? $support['source'] : '';
77
    }
78
}
79