SharedPackageSolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 88
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
B isSharedPackage() 0 23 6
A createCallbacks() 0 22 3
1
<?php
2
3
/*
4
 * This file is part of the "Composer Shared Package Plugin" package.
5
 *
6
 * https://github.com/Letudiant/composer-shared-package-plugin
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LEtudiant\Composer\Installer\Solver;
13
14
use Composer\Package\PackageInterface;
15
use LEtudiant\Composer\Installer\Config\SharedPackageInstallerConfig;
16
use LEtudiant\Composer\Installer\SharedPackageInstaller;
17
18
/**
19
 * @author Sylvain Lorinet <[email protected]>
20
 */
21
class SharedPackageSolver
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $packageCallbacks = array();
27
28
    /**
29
     * @var bool
30
     */
31
    protected $areAllShared = false;
32
33
34
    /**
35
     * @param SharedPackageInstallerConfig $config
36
     */
37
    public function __construct(SharedPackageInstallerConfig $config)
38
    {
39
        $packageList = $config->getPackageList();
40
41
        foreach ($packageList as $packageName) {
42
            if ('*' === $packageName) {
43
                $this->areAllShared = true;
44
            }
45
        }
46
47
        if (!$this->areAllShared) {
48
            $this->packageCallbacks = $this->createCallbacks($packageList);
49
        }
50
    }
51
52
    /**
53
     * @param PackageInterface $package
54
     *
55
     * @return bool
56
     */
57
    public function isSharedPackage(PackageInterface $package)
58
    {
59
        $prettyName = $package->getPrettyName();
60
61
        // Avoid putting this package into dependencies folder, because on the first installation the package won't be
62
        // installed in dependencies folder but in the vendor folder.
63
        // So I prefer keeping this behavior for further installs.
64
        if (SharedPackageInstaller::PACKAGE_PRETTY_NAME === $prettyName) {
65
            return false;
66
        }
67
68
        if ($this->areAllShared || SharedPackageInstaller::PACKAGE_TYPE === $package->getType()) {
69
            return true;
70
        }
71
72
        foreach ($this->packageCallbacks as $equalityCallback) {
73
            if ($equalityCallback($prettyName)) {
74
                return true;
75
            }
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * @param array $packageList
83
     *
84
     * @return array
85
     */
86
    protected function createCallbacks(array $packageList)
87
    {
88
        $callbacks = array();
89
90
        foreach ($packageList as $packageName) {
91
            // Has wild card (*)
92
            if (false !== strpos($packageName, '*')) {
93
                $pattern = str_replace('*', '[a-zA-Z0-9-_]+', str_replace('/', '\/', $packageName));
94
95
                $callbacks[] = function ($packagePrettyName) use ($pattern) {
96
                    return 1 === preg_match('/' . $pattern . '/', $packagePrettyName);
97
                };
98
            // Raw package name
99
            } else {
100
                $callbacks[] = function ($packagePrettyName) use ($packageName) {
101
                    return $packageName === $packagePrettyName;
102
                };
103
            }
104
        }
105
106
        return $callbacks;
107
    }
108
}
109