FileSystemReader::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LazyEight\DiTesto\FileSystem;
4
5
use LazyEight\DiTesto\FileSystem\Exceptions\FileSystemException;
6
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemPathInterface;
7
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemReaderInterface;
8
use LazyEight\DiTesto\FileSystem\Exceptions\InvalidPathException;
9
10
class FileSystemReader implements FileSystemReaderInterface
11
{
12
    /**
13
     * @var FileSystemPathInterface
14
     */
15
    private $path;
16
17
    /**
18
     * FileSystemReader constructor.
19
     * @param FileSystemPathInterface $path
20
     */
21 1
    public function __construct(FileSystemPathInterface $path)
22
    {
23 1
        $this->path = $path;
24 1
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29 1
    public function isReadable(): bool
30
    {
31 1
        return is_readable($this->path->rawPath());
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37 4
    public function read(): string
38
    {
39 4
        $this->validate();
40
41 1
        return file_get_contents($this->path->rawPath());
42
    }
43
44
    /**
45
     * @throws FileSystemException
46
     * @throws InvalidPathException
47
     */
48 4
    private function validate()
49
    {
50 4
        if ($this->path->isDirectory()) {
51 1
            throw new InvalidPathException("Error, can't read content from a directory.");
52
        }
53
54 3
        if (!$this->isReadable()) {
55 2
            throw new FileSystemException("Error, can't read the file. The file not exists or is not readable.");
56
        }
57 1
    }
58
}
59