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

TextFileReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 65
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A readFile() 0 9 1
A getPath() 0 4 1
A valid() 0 6 1
A validatePath() 0 6 2
A validateType() 0 6 2
A validateReadable() 0 6 2
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