Passed
Pull Request — master (#34)
by
unknown
05:32 queued 02:27
created

Dependent::getDependents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 3
dl 0
loc 32
ccs 18
cts 18
cp 1
crap 4
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DependencyGuard\Composer\Repository;
8
9
use Composer\Repository\CompositeRepository;
10
use Composer\Repository\RepositoryInterface;
11
12
class Dependent
13
{
14
    /** @var CompositeRepository */
15
    private $repository;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param RepositoryInterface $repository
21
     */
22 1
    public function __construct(RepositoryInterface $repository)
23
    {
24 1
        $this->repository = new CompositeRepository([$repository]);
25 1
    }
26
27
    /**
28
     * Retrieves the dependents of a package in a recursive way.
29
     *
30
     * @param string $packageName
31
     * @param bool   $returnContext
32
     * @param array  $context
33
     *
34
     * @return array
35
     */
36 10
    public function getDependents(
37
        string $packageName,
38
        bool $returnContext,
39
        array $context = []
40
    ): array {
41 10
        if (!isset($context[$packageName])) {
42 10
            $context[$packageName] = $this->repository->getDependents(
43 10
                $packageName,
44 10
                null,
45 10
                false,
46 10
                false
47
            );
48
49 10
            foreach ($context[$packageName] as $key => $dependent) {
50 4
                $dependentContext = $this->getDependents($key, true, $context);
51 4
                $context          = array_merge(
52 4
                    $context,
53 4
                    $dependentContext
54
                );
55
56 4
                $context[$packageName] = array_merge(
57 4
                    $context[$packageName],
58 4
                    $dependentContext[$key]
59
                );
60
            }
61
        }
62
63 10
        if (!$returnContext) {
64 10
            return $context[$packageName];
65
        }
66
67 4
        return $context;
68
    }
69
}
70