Completed
Push — master ( f1eedc...1b618f )
by Adam
05:55
created

BundleLocator   A

Coupling/Cohesion

Components 0
Dependencies 4

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getBundleClasses() 0 7 1
A getComposerInstalledBundleClasses() 0 16 3
A getComposer() 0 4 1
1
<?php
2
3
namespace WellCommerce\Bundle\CoreBundle\Locator;
4
5
use Composer\Composer;
6
use Composer\Factory;
7
use Composer\IO\NullIO;
8
use Composer\Package\PackageInterface;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
11
/**
12
 * Class BundleLoader
13
 *
14
 * @author  Adam Piotrowski <[email protected]>
15
 */
16
final class BundleLocator
17
{
18
    /**
19
     * @var KernelInterface
20
     */
21
    private $kernel;
22
    
23
    public function __construct(KernelInterface $kernel)
24
    {
25
        $this->kernel = $kernel;
26
    }
27
    
28
    public function getBundleClasses(): array
29
    {
30
        $composerInstalledBundleClasses = $this->getComposerInstalledBundleClasses();
31
        $localBundles                   = [];
32
        
33
        return array_merge($composerInstalledBundleClasses, $localBundles);
34
    }
35
    
36
    private function getComposerInstalledBundleClasses(): array
37
    {
38
        $bundles         = [];
39
        $composer        = $this->getComposer();
40
        $localRepository = $composer->getRepositoryManager()->getLocalRepository();
41
        
42
        /** @var PackageInterface $package */
43
        foreach ($localRepository->getPackages() as $package) {
44
            $bundleClasses = $package->getExtra()['register-bundles'] ?? [];
45
            foreach ($bundleClasses as $bundleClass) {
46
                $bundles[$bundleClass] = $bundleClass;
47
            }
48
        }
49
        
50
        return $bundles;
51
    }
52
    
53
    private function getComposer(): Composer
54
    {
55
        return Factory::create(new NullIO());
56
    }
57
}
58