Reader::contents()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/*
4
 * This file is part of Payload.
5
 *
6
 * (c) DraperStudio <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DraperStudio\Payload\Readers;
13
14
use DraperStudio\Payload\Exceptions\InvalidFileTypeException;
15
use DraperStudio\Payload\Utils\File;
16
17
/**
18
 * Class Reader.
19
 */
20
abstract class Reader
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $extensions = [];
26
27
    /**
28
     * @param $path
29
     * @param null $class
30
     *
31
     * @return mixed
32
     */
33
    abstract public function read($path, $class = null);
34
35
    /**
36
     * @param $path
37
     *
38
     * @return bool
39
     *
40
     * @throws InvalidFileTypeException
41
     */
42 42
    public function check($path)
43
    {
44 42
        $extension = File::extension($path);
45
46 42
        if (!in_array($extension, $this->extensions)) {
47 21
            throw new InvalidFileTypeException(
48 21
                sprintf('%s is an invalid file type for the %s class', $extension, get_class($this)));
49
        }
50
51 21
        return true;
52
    }
53
54
    /**
55
     * @param $path
56
     *
57
     * @return string
58
     *
59
     * @throws InvalidFileTypeException
60
     * @throws \DraperStudio\Payload\Exceptions\FileDoesNotExistException
61
     */
62 36
    public function contents($path)
63
    {
64 36
        if ($this->check($path)) {
65 18
            return File::contents($path);
66
        }
67
    }
68
69
    /**
70
     * @param $path
71
     *
72
     * @return mixed
73
     *
74
     * @throws InvalidFileTypeException
75
     * @throws \DraperStudio\Payload\Exceptions\FileDoesNotExistException
76
     */
77 6
    public function get($path)
78
    {
79 6
        if ($this->check($path)) {
80 3
            return File::get($path);
81
        }
82
    }
83
}
84