PackageInfoResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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