Passed
Push — master ( 678f11...49ab1b )
by
unknown
05:16
created

DependentsResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 72
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getDependents() 0 32 4
A resolve() 0 6 1
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 DependentsResolver implements DependentsResolverInterface
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
     * Resolves the dependents of the package.
29
     *
30
     * @param string $packageName
31
     *
32
     * @return array
33
     */
34 5
    public function resolve(string $packageName): array
35
    {
36 5
        return $this->getDependents(
37 5
            $packageName,
38 5
            false,
39 5
            []
40
        );
41
    }
42
43
    /**
44
     * Retrieves the dependents of a package in a recursive way.
45
     *
46
     * @param string $packageName
47
     * @param bool   $returnContext
48
     * @param array  $context
49
     *
50
     * @return array
51
     */
52 10
    private function getDependents(
53
        string $packageName,
54
        bool $returnContext,
55
        array $context = []
56
    ): array {
57 10
        if (!isset($context[$packageName])) {
58 10
            $context[$packageName] = $this->repository->getDependents(
59 10
                $packageName,
60 10
                null,
61 10
                false,
62 10
                false
63
            );
64
65 10
            foreach ($context[$packageName] as $key => $dependent) {
66 4
                $dependentContext = $this->getDependents($key, true, $context);
67 4
                $context          = array_merge(
68 4
                    $context,
69 4
                    $dependentContext
70
                );
71
72 4
                $context[$packageName] = array_merge(
73 4
                    $context[$packageName],
74 4
                    $dependentContext[$key]
75
                );
76
            }
77
        }
78
79 10
        if (!$returnContext) {
80 10
            return $context[$packageName];
81
        }
82
83 4
        return $context;
84
    }
85
}
86