ReaderFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 3
b 0
f 0
dl 0
loc 50
ccs 12
cts 13
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detectType() 0 7 2
A findClass() 0 7 2
A create() 0 5 1
A get() 0 13 2
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config\readers;
12
13
use hiqdev\composer\config\Builder;
14
use hiqdev\composer\config\exceptions\UnsupportedFileTypeException;
15
16
/**
17
 * Reader - helper to load data from files of different types.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class ReaderFactory
22
{
23
    private static $loaders;
24
25
    protected static $knownReaders = [
26
        'env'   => EnvReader::class,
27
        'php'   => PhpReader::class,
28
        'json'  => JsonReader::class,
29
        'yaml'  => YamlReader::class,
30
        'yml'   => YamlReader::class,
31
    ];
32
33 1
    public static function get(Builder $builder, $path)
34
    {
35 1
        $type = static::detectType($path);
36 1
        $class = static::findClass($type);
37 1
38 1
        #return static::create($builder, $type);
39
40
        $uniqid = $class . ':' . spl_object_hash($builder);
41 1
        if (empty(static::$loaders[$uniqid])) {
0 ignored issues
show
Bug introduced by
Since $loaders is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $loaders to at least protected.
Loading history...
42
            static::$loaders[$uniqid] = static::create($builder, $type);
43
        }
44 1
45
        return static::$loaders[$uniqid];
46 1
    }
47
48
    public static function detectType($path)
49
    {
50 1
        if (strncmp(basename($path), '.env.', 5) === 0) {
51
            return 'env';
52
        }
53 1
54
        return pathinfo($path, PATHINFO_EXTENSION);
55 1
    }
56
57 1
    public static function findClass($type)
58
    {
59
        if (empty(static::$knownReaders[$type])) {
60
            throw new UnsupportedFileTypeException("unsupported file type: $type");
61
        }
62
63
        return static::$knownReaders[$type];
64
    }
65
66
    public static function create(Builder $builder, $type)
67
    {
68
        $class = static::findClass($type);
69
70
        return new $class($builder);
71
    }
72
}
73