PackageResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveForNamespace() 0 18 4
A __construct() 0 6 1
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Composer\Plugin;
7
8
class PackageResolver
9
{
10
    /**
11
     * @var \Composer\Package\PackageInterface[]
12
     */
13
    private $additionalPackages;
14
15
    /**
16
     * @var \Vaimo\ComposerPatches\Package\ConfigAnalyser
17
     */
18
    private $packageAnalyser;
19
20
    /**
21
     * @param \Composer\Package\PackageInterface[] $additionalPackages
22
     */
23
    public function __construct(
24
        array $additionalPackages = array()
25
    ) {
26
        $this->additionalPackages = $additionalPackages;
27
28
        $this->packageAnalyser = new \Vaimo\ComposerPatches\Package\ConfigAnalyser();
29
    }
30
31
    /**
32
     * @param \Composer\Package\PackageInterface[] $packages
33
     * @param string $namespace
34
     * @return \Composer\Package\PackageInterface
35
     * @throws \Vaimo\ComposerPatches\Exceptions\PackageResolverException
36
     */
37
    public function resolveForNamespace(array $packages, $namespace)
38
    {
39
        $packages = array_merge($this->additionalPackages, $packages);
40
41
        foreach ($packages as $package) {
42
            if (!$this->packageAnalyser->isPluginPackage($package)) {
43
                continue;
44
            }
45
46
            if (!$this->packageAnalyser->ownsNamespace($package, $namespace)) {
47
                continue;
48
            }
49
50
            return $package;
51
        }
52
53
        throw new \Vaimo\ComposerPatches\Exceptions\PackageResolverException(
54
            'Failed to detect the plugin package'
55
        );
56
    }
57
}
58