AbstractReader::read()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 16
nop 1
crap 42
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