Completed
Push — master ( 210110...022d76 )
by ARCANEDEV
8s
created

RepositoriesTrait::prependRepositories()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Composer\Package\RootPackageInterface;
4
use Composer\Repository\RepositoryManager;
5
6
/**
7
 * Trait     RepositoriesTrait
8
 *
9
 * @package  Arcanedev\Composer\Entities\PackageTraits
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait RepositoriesTrait
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Traits
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    use PackageTrait;
19
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Main Functions
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * Add a collection of repositories described by the given configuration
26
     * to the given package and the global repository manager.
27
     *
28
     * @param  \Composer\Package\RootPackageInterface    $root
29
     */
30
    private function prependRepositories(RootPackageInterface $root)
31
    {
32
        $json = $this->getJson();
33
34
        if (isset($json['repositories'])) {
35
            $repoManager  = $this->getComposer()->getRepositoryManager();
36
            $repositories = [];
37
38
            foreach ($json['repositories'] as $repoJson) {
39
                $this->addRepository($repoManager, $repositories, $repoJson);
40
            }
41
42
            /** @var \Composer\Package\RootPackageInterface $unwrapped */
43
            self::unwrapIfNeeded($root, 'setRepositories')
44
                ->setRepositories(array_merge($repositories, $root->getRepositories()));
45
        }
46
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Other Functions
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Add a repository to collection of repositories.
54
     *
55
     * @param  \Composer\Repository\RepositoryManager  $repoManager
56
     * @param  array                                   $repositories
57
     * @param  array                                   $repoJson
58
     */
59
    private function addRepository(
60
        RepositoryManager $repoManager, array &$repositories, $repoJson
61
    ) {
62
        if (isset($repoJson['type'])) {
63
            $this->getLogger()->info("Prepending {$repoJson['type']} repository");
64
65
            $repository = $repoManager->createRepository($repoJson['type'], $repoJson);
66
67
            $repoManager->prependRepository($repository);
68
            $repositories[] = $repository;
69
        }
70
    }
71
}
72