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

JsonFileLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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