JsonLoader::loadJson()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace JsonSpec;
4
5
use JsonSpec\Exception\MissingDirectoryException;
6
use JsonSpec\Exception\MissingFileException;
7
8
class JsonLoader
9
{
10
11
    private $directory;
12
13
    /**
14
     * @param string $directory
15
     */
16
    public function __construct($directory = null)
17
    {
18
        $this->setDirectory($directory);
19
    }
20
21
    /**
22
     * @param string $directory
23
     */
24
    public function setDirectory($directory)
25
    {
26
        if (!is_null($directory)) {
27
            $this->directory = rtrim($directory, '/').'/';
28
        }
29
    }
30
31
    /**
32
     * @param  string $path
33
     * @return string
34
     */
35
    public function loadJson($path)
36
    {
37
        if (!$this->directory) {
38
            throw new MissingDirectoryException();
39
        }
40
41
        $path = $this->directory . $path;
42
        if (!is_file($path)) {
43
            throw new MissingFileException($path);
44
        }
45
46
        return file_get_contents($path);
47
    }
48
49
}
50