AbstractReader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
wmc 10
eloc 17
dl 0
loc 48
ccs 5
cts 20
cp 0.25
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileContents() 0 8 2
A getBuilder() 0 3 1
A __construct() 0 3 1
A read() 0 18 6
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\FailedReadException;
15
16
/**
17
 * Reader - helper to read data from files of different types.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 *
21
 * @since php5.5
22
 */
23
abstract class AbstractReader
24
{
25
    /**
26
     * @var Builder
27
     */
28
    protected $builder;
29
30 1
    public function __construct(Builder $builder)
31
    {
32 1
        $this->builder = $builder;
33 1
    }
34
35 1
    public function getBuilder()
36
    {
37 1
        return $this->builder;
38
    }
39
40
    public function read($path)
41
    {
42
        $skippable = 0 === strncmp($path, '?', 1) ? '?' : '';
43
        if ($skippable) {
44
            $path = substr($path, 1);
45
        }
46
47
        if (is_readable($path)) {
48
            $res = $this->readRaw($path);
49
50
            return is_array($res) ? $res : [];
51
        }
52
53
        if (empty($skippable)) {
54
            throw new FailedReadException("failed read file: $path");
55
        }
56
57
        return [];
58
    }
59
60
    public function getFileContents($path)
61
    {
62
        $res = file_get_contents($path);
63
        if (false === $res) {
64
            throw new FailedReadException("failed read file: $path");
65
        }
66
67
        return $res;
68
    }
69
70
    abstract public function readRaw($path);
71
}
72