AbstractReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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