Completed
Push — master ( d34e57...59e426 )
by Dmitry
03:19
created

RegistryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 54
ccs 0
cts 16
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegistry() 0 8 2
A buildRegistry() 0 12 1
A createAssetRepositoryManager() 0 6 1
1
<?php
2
3
/*
4
 * Asset Packagist
5
 *
6
 * @link      https://github.com/hiqdev/asset-packagist
7
 * @package   asset-packagist
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\assetpackagist\registry;
13
14
use Composer\IO\NullIO;
15
use Composer\Repository\RepositoryManager;
16
use Fxp\Composer\AssetPlugin\Config\Config;
17
use Fxp\Composer\AssetPlugin\Repository\AbstractAssetsRepository;
18
use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
19
use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
20
21
class RegistryFactory
22
{
23
    /**
24
     * @var array
25
     */
26
    protected static $classes = [
27
        'bower' => BowerRegistry::class,
28
        'npm'   => NpmRegistry::class,
29
    ];
30
31
    /**
32
     * @var AbstractAssetsRepository[]
33
     */
34
    public static $registries = [];
35
36
    /**
37
     * @param string $type
38
     * @param RepositoryManager $rm
39
     * @return AbstractAssetsRepository|BowerRegistry|NpmRegistry
40
     */
41
    public static function getRegistry($type, $rm)
42
    {
43
        if (!isset(static::$registries[$type])) {
44
            static::$registries[$type] = static::buildRegistry($type, $rm);
45
        }
46
47
        return static::$registries[$type];
48
    }
49
50
    /**
51
     * @param string $type
52
     * @param RepositoryManager $rm
53
     * @return mixed
54
     */
55
    protected static function buildRegistry($type, $rm)
56
    {
57
        $class = static::$classes[$type];
58
        $rm->setRepositoryClass($type, $class);
59
60
        $config = [
61
            'asset-repository-manager' => self::createAssetRepositoryManager($rm),
62
            'asset-options'      => [],
63
        ];
64
65
        return $rm->createRepository($type, $config);
66
    }
67
68
    public static function createAssetRepositoryManager($repositoryManager)
69
    {
70
        $filter = (new \ReflectionClass(VcsPackageFilter::class))->newInstanceWithoutConstructor();
71
72
        return new AssetRepositoryManager(new NullIO(), $repositoryManager, new Config([]), $filter);
73
    }
74
}
75