Completed
Push — master ( 05283a...49c7da )
by Victor
12s
created

TextFileReader::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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->validate($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
    /**
47
     * @param string $path
48
     */
49 4
    private function validate(string $path)
50
    {
51 4
        $this->validatePath($path);
52 3
        $this->validateReadable($path);
53 2
        $this->validateType($path);
54 1
    }
55
56
    /**
57
     * @param string $path
58
     * @throws IOException
59
     */
60 4
    private function validatePath(string $path)
61
    {
62 4
        if (empty($path)) {
63 1
            throw new IOException('Invalid path.');
64
        }
65 3
    }
66
67
    /**
68
     * @param string $path
69
     * @throws IOException
70
     */
71 2
    private function validateType(string $path)
72
    {
73 2
        if ('text/plain' !== mime_content_type($path)) {
74 1
            throw new IOException('Invalid mime type. Only text/plain type is allowed.');
75
        }
76 1
    }
77
78
    /**
79
     * @param string $path
80
     * @throws IOException
81
     */
82 2
    private function validateReadable(string $path)
83
    {
84 2
        if (!(new TextFile($path))->isReadable()) {
85 1
            throw new IOException("Can't read the file");
86
        }
87 1
    }
88
}
89