Completed
Push — master ( 0585c5...8e425f )
by Victor
13s
created

TextFileReader::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LazyEight\DiTesto;
4
5
use LazyEight\DiTesto\Exceptions\IOException;
6
use LazyEight\DiTesto\Interfaces\FileInterface;
7
use LazyEight\DiTesto\Interfaces\FileReaderInterface;
8
9
class TextFileReader implements FileReaderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * TextFileReader constructor.
18
     * @param string $path
19
     */
20 1
    public function __construct(string $path)
21
    {
22 1
        $this->path = $path;
23 1
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28 4
    public function readFile():FileInterface
29
    {
30 4
        $this->valid($this->getPath());
31
32 1
        $textFile = new TextFile($this->getPath());
33 1
        $textFile->read();
34
35 1
        return $textFile;
36
    }
37
38
    /**
39
     * @return string
40
     */
41 1
    public function getPath():string
42
    {
43 1
        return $this->path;
44
    }
45
46 4
    private function valid(string $path)
47
    {
48 4
        $this->validatePath($path);
49 3
        $this->validateReadable($path);
50 2
        $this->validateType($path);
51 1
    }
52
53 4
    private function validatePath(string $path)
54
    {
55 4
        if (empty($path)) {
56 1
            throw new IOException('Invalid path.');
57
        }
58 3
    }
59
60 2
    private function validateType(string $path)
61
    {
62 2
        if ('text/plain' !== mime_content_type($path)) {
63 1
            throw new IOException('Invalid mime type. Only text/plain type is allowed.');
64
        }
65 1
    }
66
67 2
    private function validateReadable(string $path)
68
    {
69 2
        if (!(new TextFile($path))->isReadable()) {
70 1
            throw new IOException("Can't read the file");
71
        }
72 1
    }
73
}
74