Completed
Pull Request — master (#13)
by Victor
01:57
created

FileSystemReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 21.43%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 48
ccs 3
cts 14
cp 0.2143
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isReadable() 0 4 1
A read() 0 5 1
A validate() 0 10 3
1
<?php
2
3
namespace LazyEight\DiTesto\FileSystem;
4
5
use LazyEight\DiTesto\Exceptions\FileSystemException;
6
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemPathInterface;
7
use LazyEight\DiTesto\Interfaces\FileSystem\FileSystemReaderInterface;
8
use Test\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
    public function isReadable(): bool
30
    {
31
        return is_readable($this->path->rawPath());
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function read(): string
38
    {
39
        $this->validate();
40
        return file_get_contents($this->path->rawPath());
41
    }
42
43
    /**
44
     * @throws FileSystemException
45
     * @throws InvalidPathException
46
     */
47
    public function validate()
48
    {
49
        if ($this->path->isDirectory()) {
50
            throw new InvalidPathException("Error, can't read content from a directory.");
51
        }
52
53
        if (!$this->isReadable()) {
54
            throw new FileSystemException("Error, can't read the file. The file not exists or is not readable.");
55
        }
56
    }
57
}
58