AbstractReader::getFileContents()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 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
abstract class AbstractReader
22
{
23
    /**
24
     * @var Builder
25
     */
26
    protected $builder;
27
28 1
    public function __construct(Builder $builder)
29
    {
30 1
        $this->builder = $builder;
31 1
    }
32
33 1
    public function getBuilder(): Builder
34
    {
35 1
        return $this->builder;
36
    }
37
38
    public function read($path)
39
    {
40
        $skippable = 0 === strncmp($path, '?', 1) ? '?' : '';
41
        if ($skippable) {
42
            $path = substr($path, 1);
43
        }
44
45
        if (is_readable($path)) {
46
            $res = $this->readRaw($path);
47
48
            return is_array($res) ? $res : [];
49
        }
50
51
        if (empty($skippable)) {
52
            throw new FailedReadException("failed read file: $path");
53
        }
54
55
        return [];
56
    }
57
58
    public function getFileContents($path)
59
    {
60
        $res = file_get_contents($path);
61
        if (false === $res) {
62
            throw new FailedReadException("failed read file: $path");
63
        }
64
65
        return $res;
66
    }
67
68
    abstract public function readRaw($path);
69
}
70