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

PackageInfoResolver::getInstallPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
use Composer\Package\PackageInterface;
9
10
use Vaimo\ComposerChangelogs\Composer\Config as ConfigKeys;
11
12
class PackageInfoResolver
13
{
14
    const DEFAULT_PATH = '.';
15
16
    /**
17
     * @var \Composer\Installer\InstallationManager
18
     */
19
    private $installationManager;
20
21
    /**
22
     * @param \Composer\Installer\InstallationManager $installationManager
23
     */
24
    public function __construct(
25
        \Composer\Installer\InstallationManager $installationManager
26
    ) {
27
        $this->installationManager = $installationManager;
28
    }
29
30
    /**
31
     * @SuppressWarnings(PHPMD.StaticAccess)
32
     *
33
     * @param PackageInterface $package
34
     * @return bool|string
35
     */
36
    public function getInstallPath(PackageInterface $package)
37
    {
38
        return !$package instanceof \Composer\Package\RootPackage
39
            ? $this->installationManager->getInstallPath($package)
40
            : realpath(dirname(\Composer\Factory::getComposerFile()));
41
    }
42
43
    public function resolveNamesFromPaths(array $packagesByName, array $paths)
44
    {
45
        $paths = array_unique(
46
            array_map('dirname', $paths)
47
        );
48
49
        $names = array();
50
51
        foreach ($paths as $path) {
52
            $segments = explode(DIRECTORY_SEPARATOR, $path);
53
54
            while ($chunk = array_slice($segments, 0, 2)) {
55
                array_shift($segments);
56
57
                $name = implode(DIRECTORY_SEPARATOR, $chunk);
58
59
                if (!isset($packagesByName[$name])) {
60
                    continue;
61
                }
62
63
                $names[] = $name;
64
65
                break;
66
            }
67
        }
68
69
        return $names;
70
    }
71
72
    public function getAutoLoadPaths(PackageInterface $package)
73
    {
74
        $autoloadConfig = $package->getAutoload();
75
76
        if (!isset($autoloadConfig[ConfigKeys::PSR4_CONFIG])) {
77
            return array();
78
        }
79
80
        $installPath = $this->getInstallPath($package);
81
82
        $sourcePaths = array_map(
83
            function ($path) use ($installPath) {
84
                return $installPath . DIRECTORY_SEPARATOR . $path;
85
            },
86
            $autoloadConfig[ConfigKeys::PSR4_CONFIG]
87
        );
88
89
        return array_values($sourcePaths);
90
    }
91
}
92