PluginPackageResolver::resolveForNamespace()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 11
c 2
b 0
f 1
dl 0
loc 21
rs 9.9
cc 4
nc 4
nop 2
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
class PluginPackageResolver
9
{
10
    /**
11
     * @var \Composer\Package\PackageInterface[]
12
     */
13
    private $additionalPackages;
14
    
15
    /**
16
     * @var \Vaimo\ComposerChangelogs\Analysers\PackageAnalyser
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\ComposerChangelogs\Analysers\PackageAnalyser();
29
    }
30
31
    /**
32
     * @param \Composer\Package\PackageInterface[] $packages
33
     * @param string $namespace
34
     * @return \Composer\Package\PackageInterface
35
     * @throws \Exception
36
     */
37
    public function resolveForNamespace(array $packages, $namespace)
38
    {
39
        $packages = array_merge(
40
            $this->additionalPackages,
41
            array_values($packages)
42
        );
43
        
44
        foreach ($packages as $package) {
45
            if (!$this->packageAnalyser->isPluginPackage($package)) {
46
                continue;
47
            }
48
49
            if (!$this->packageAnalyser->ownsNamespace($package, $namespace)) {
50
                continue;
51
            }
52
53
            return $package;
54
        }
55
56
        throw new \Vaimo\ComposerChangelogs\Exceptions\PackageResolverException(
57
            'Failed to detect the plugin package'
58
        );
59
    }
60
}
61