RemoteSourceResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
c 3
b 0
f 0
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getVcsCommandsConfig() 0 15 3
A __construct() 0 9 1
A resolveForPackage() 0 25 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\Url;
7
8
use Composer\Package\PackageInterface;
9
10
class RemoteSourceResolver implements \Vaimo\ComposerChangelogs\Interfaces\UrlResolverInterface
11
{
12
    /**
13
     * @var \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver
14
     */
15
    private $packageInfoResolver;
16
17
    /**
18
     * @var \Vaimo\ComposerChangelogs\Normalizers\UrlNormalizer
19
     */
20
    private $urlNormalizer;
21
22
    /**
23
     * @var \Vaimo\ComposerChangelogs\Utils\PathUtils
24
     */
25
    private $pathUtils;
26
    
27
    /**
28
     * @var \Vaimo\ComposerChangelogs\Utils\SystemUtils
29
     */
30
    private $systemUtils;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $queryCommands = array(
36
        '.hg' => 'hg path default',
37
        '.git' => 'git remote get-url origin'
38
    );
39
    
40
    /**
41
     * @param \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver,
42
     */
43
    public function __construct(
44
        \Vaimo\ComposerChangelogs\Resolvers\PackageInfoResolver $packageInfoResolver
45
    ) {
46
        $this->packageInfoResolver = $packageInfoResolver;
47
48
        $this->urlNormalizer = new \Vaimo\ComposerChangelogs\Normalizers\UrlNormalizer();
49
        
50
        $this->pathUtils = new \Vaimo\ComposerChangelogs\Utils\PathUtils();
51
        $this->systemUtils = new \Vaimo\ComposerChangelogs\Utils\SystemUtils();
52
    }
53
54
    public function resolveForPackage(PackageInterface $package)
55
    {
56
        if (!$package instanceof \Composer\Package\CompletePackageInterface) {
57
            return '';
58
        }
59
60
        $support = $package->getSupport();
61
        
62
        $source = isset($support['source']) ? $support['source'] : '';
63
        
64
        if (!$source) {
65
            $commandsConfig = $this->getVcsCommandsConfig($package);
66
            
67
            foreach ($commandsConfig as $command => $sourcePath) {
68
                $result = $this->systemUtils->getCommandStdOut($command, $sourcePath);
69
                
70
                if (!$result) {
71
                    continue;
72
                }
73
74
                return $this->urlNormalizer->assureHttpAccessibility($result);
75
            }
76
        }
77
78
        return $source;
79
    }
80
    
81
    private function getVcsCommandsConfig(PackageInterface $package)
82
    {
83
        $result = array();
84
85
        foreach ($this->queryCommands as $folder => $command) {
86
            $sourcePath = $this->packageInfoResolver->getInstallPath($package);
87
88
            if (!file_exists($this->pathUtils->composePath($sourcePath, $folder))) {
89
                continue;
90
            }
91
92
            $result[$command] = $sourcePath;
93
        }
94
        
95
        return $result;
96
    }
97
}
98