Completed
Pull Request — master (#14)
by
unknown
06:16
created

FileLoader::load()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 22
ccs 4
cts 4
cp 1
crap 4
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
namespace Activerules\JsonReference\Loader;
4
5
use Activerules\JsonReference\JsonDecoder\JsonDecoder;
6
use Activerules\JsonReference\JsonDecoderInterface;
7
use Activerules\JsonReference\LoaderInterface;
8
use Activerules\JsonReference\SchemaLoadingException;
9
10
final class FileLoader implements LoaderInterface
11
{
12
    /**
13
     * @var JsonDecoderInterface
14
     */
15
    private $jsonDecoder;
16
17
    /**
18
     * @param JsonDecoderInterface $jsonDecoder
19
     */
20 62
    public function __construct(JsonDecoderInterface $jsonDecoder = null)
21
    {
22 62
        $this->jsonDecoder = $jsonDecoder ?: new JsonDecoder();
23 62
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 38
    public function load($path)
29
    {
30 38
        // Check if the file path is 'relative', i.e. starts with a dot.
31 4
        if(substr($path,0,1) === '.') {
32
            // If the path is 'relative' check for an defined AR_JSON_SCHEMA_DIR
33
            $schemaDir = getenv('AR_JSON_SCHEMA_DIR');
34 34
35
            // If root dir isn't set use the default relative path of './schema'
36
            if(! $schemaDir) {
37
                $schemaDir = getcwd() . '/schema';
38
            }
39
          
40
            // Define the full path to pass to existing League Reference File Loader
41
            $path = $schemaDir . ltrim($path, '.');
42
        }
43
        
44
        if (!file_exists($path)) {
45
            throw SchemaLoadingException::notFound($path);
46
        }
47
48
        return $this->jsonDecoder->decode(file_get_contents($path));
49
    }
50
}
51