File   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 57
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPath() 0 3 1
A getContents() 0 13 3
A pathExists() 0 6 2
1
<?php declare(strict_types=1);
2
3
namespace Hyperized\Xml\Types;
4
5
use Hyperized\Xml\Constants\ErrorMessages;
6
use Hyperized\Xml\Exceptions\EmptyFile;
7
use Hyperized\Xml\Exceptions\FileCouldNotBeOpenedException;
8
use Hyperized\Xml\Exceptions\FileDoesNotExist;
9
10
/**
11
 * Class File
12
 *
13
 * @package Hyperized\Xml\Types\Xml
14
 */
15
class File
16
{
17
    /**
18
     * @var string
19
     */
20
    private $path;
21
22
    /**
23
     * File constructor.
24
     *
25
     * @param  string $path
26
     * @throws FileDoesNotExist
27
     */
28 16
    public function __construct(string $path)
29
    {
30 16
        $this->path = $path;
31 16
        $this->pathExists();
32 14
    }
33
34
    /**
35
     * @return bool
36
     * @throws FileDoesNotExist
37
     */
38 16
    private function pathExists(): bool
39
    {
40 16
        if (!file_exists($this->path)) {
41 4
            throw new FileDoesNotExist(ErrorMessages::FILE_DOES_NOT_EXIST);
42
        }
43 14
        return true;
44
    }
45
46
    /**
47
     * @return string
48
     * @throws EmptyFile
49
     * @throws FileCouldNotBeOpenedException
50
     */
51 14
    public function getContents(): string
52
    {
53 14
        $contents = file_get_contents($this->path);
54
55 14
        if ($contents === false) {
56 2
            throw new FileCouldNotBeOpenedException(ErrorMessages::FILE_COULD_NOT_BE_OPENED);
57
        }
58
59 12
        if ($contents === '') {
60 2
            throw new EmptyFile(ErrorMessages::EMPTY_FILE);
61
        }
62
63 10
        return $contents;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 4
    public function getPath(): string
70
    {
71 4
        return $this->path;
72
    }
73
}
74