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

FileLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
B load() 0 22 4
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