Passed
Push — master ( 6909f4...6e30b5 )
by Marc
02:27
created

Loader::loadFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
crap 3
1
<?php
2
/*
3
 * This file is part of the phpflo\phpflo-fbp package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PhpFlo\Loader;
11
12
use PhpFlo\Common\DefinitionInterface;
13
use PhpFlo\Common\LoaderInterface;
14
use PhpFlo\Exception\LoaderException;
15
use PhpFlo\Fbp\FbpDefinition;
16
use PhpFlo\Fbp\FbpParser;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * Class Loader
21
 *
22
 * @package PhpFlo\Loader
23
 * @author Marc Aschmann <[email protected]>
24
 */
25
final class Loader implements LoaderInterface
26
{
27
    private static $types = [
28
        'yml' => 'yaml',
29
        'yaml' => 'yaml',
30
        'json' => 'json',
31
        'fbp' => 'fbp',
32
    ];
33
34
    /**
35
     * @param string $file name/path of file to load
36
     * @return DefinitionInterface|null
37
     * @throws LoaderException
38
     */
39 4
    public static function load($file)
40
    {
41 4
        $type = self::$types[self::checkType($file)];
42 4
        $content = self::loadFile($file);
43
44 3
        if (empty($content)) {
45
            throw new LoaderException("Loader::load(): no data found in file!");
46
        }
47
48
        switch ($type) {
49 3
            case 'fbp':
50 1
                $loader = new FbpParser($content);
51 1
                $definition = $loader->run();
52 1
                break;
53 2
            case 'yaml':
54 1
                $definition = new FbpDefinition(
55 1
                    Yaml::parse($content)
56 1
                );
57 1
                break;
58 1
            case 'json':
59 1
                $definition = new FbpDefinition(
60 1
                    json_decode($content, true)
61 1
                );
62 1
                break;
63
            default:
64
                $defnition = null;
0 ignored issues
show
Unused Code introduced by
$defnition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
                throw new LoaderException("Loader::load(): Something unexpected happened.");
66
        }
67
68 3
        return $definition;
69
    }
70
71
    /**
72
     * Check file if extension matches a loader.
73
     *
74
     * @param string $file
75
     * @return string
76
     */
77 4
    private static function checkType($file)
78
    {
79 4
        $parts = explode('.', $file);
80 4
        $type = array_pop($parts);
81
82 4
        if (!in_array($type, array_keys(self::$types))) {
83
            throw new LoaderException("Loader::checkType(): Could not find parser for {$file}!");
84
        }
85
86 4
        return $type;
87
    }
88
89
    /**
90
     * @param string $file
91
     * @return string
92
     */
93 4
    private static function loadFile($file)
94
    {
95 4
        if (file_exists($file) && is_readable($file)) {
96 3
            $content = file_get_contents($file);
97 3
        } else {
98 1
            throw new LoaderException(
99 1
                "Loader::loadFile(): {$file} does not exist!"
100 1
            );
101
        }
102
103 3
        return $content;
104
    }
105
}
106