Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

JsonFileLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 37
ccs 16
cts 16
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 24 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Loader;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Configuration\Exception\FileNotFound;
9
use Doctrine\Migrations\Configuration\Exception\JsonNotValid;
10
use const JSON_ERROR_NONE;
11
use function assert;
12
use function file_exists;
13
use function file_get_contents;
14
use function json_decode;
15
use function json_last_error;
16
17
/**
18
 * @internal
19
 */
20
final class JsonFileLoader extends AbstractFileLoader
21
{
22
    /** @var ArrayLoader */
23
    private $arrayLoader;
24
25 12
    public function __construct()
26
    {
27 12
        $this->arrayLoader = new ArrayLoader();
28 12
    }
29
30
    /**
31
     * @param mixed $file
32
     */
33 10
    public function load($file) : Configuration
34
    {
35 10
        if (! file_exists($file)) {
36 1
            throw FileNotFound::new();
37
        }
38
39 9
        $contents = file_get_contents($file);
40
41 9
        assert($contents !== false);
42
43 9
        $config = json_decode($contents, true);
44
45 9
        if (json_last_error() !== JSON_ERROR_NONE) {
46 1
            throw JsonNotValid::new();
47
        }
48
49 8
        if (isset($config['migrations_paths'])) {
50 3
            $config['migrations_paths'] = $this->getDirectoryRelativeToFile(
51 3
                $file,
52 3
                $config['migrations_paths']
53
            );
54
        }
55
56 8
        return $this->arrayLoader->load($config);
57
    }
58
}
59