Loader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 85
ccs 34
cts 37
cp 0.9189
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 30 5
A checkType() 0 11 2
A loadFile() 0 12 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
declare(strict_types=1);
11
namespace PhpFlo\Loader;
12
13
use PhpFlo\Common\DefinitionInterface;
14
use PhpFlo\Common\LoaderInterface;
15
use PhpFlo\Exception\LoaderException;
16
use PhpFlo\Fbp\FbpDefinition;
17
use PhpFlo\Fbp\FbpParser;
18
use Symfony\Component\Yaml\Yaml;
19
20
/**
21
 * Class Loader
22
 *
23
 * @package PhpFlo\Loader
24
 * @author Marc Aschmann <[email protected]>
25
 */
26
final class Loader implements LoaderInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    private static $types = [
32
        'yml' => 'yaml',
33
        'yaml' => 'yaml',
34
        'json' => 'json',
35
        'fbp' => 'fbp',
36
    ];
37
38
    /**
39 7
     * @param string $file name/path of file to load
40
     * @return DefinitionInterface
41 7
     * @throws LoaderException
42 6
     */
43
    public static function load(string $file) : DefinitionInterface
44 4
    {
45 1
        $type = self::$types[self::checkType($file)];
46
        $content = self::loadFile($file);
47
48
        if (empty($content)) {
49 3
            throw new LoaderException("Loader::load(): no data found in file!");
50 1
        }
51 1
52 1
        switch ($type) {
53 2
            case 'fbp':
54 1
                $loader = new FbpParser($content);
55 1
                $definition = $loader->run();
56 1
                break;
57 1
            case 'yaml':
58 1
                $definition = new FbpDefinition(
59 1
                    Yaml::parse($content)
60 1
                );
61 1
                break;
62 1
            case 'json':
63
                $definition = new FbpDefinition(
64
                    json_decode($content, true)
65
                );
66
                break;
67 3
            default:
68
                throw new LoaderException("Loader::load(): Something unexpected happened.");
69
        }
70
71
        return $definition;
72
    }
73
74
    /**
75
     * Check file if extension matches a loader.
76 7
     *
77
     * @param string $file
78 7
     * @return string
79 7
     * @throws LoaderException
80
     */
81 7
    private static function checkType(string $file) : string
82 1
    {
83
        $parts = explode('.', $file);
84
        $type = array_pop($parts);
85 6
86
        if (!in_array($type, array_keys(self::$types))) {
87
            throw new LoaderException("Loader::checkType(): Could not find parser for {$file}!");
88
        }
89
90
        return $type;
91
    }
92 6
93
    /**
94 6
     * @param string $file
95 4
     * @return string
96 4
     * @throws LoaderException
97 2
     */
98 2
    private static function loadFile(string $file) : string
99 2
    {
100
        if (file_exists($file) && is_readable($file)) {
101
            $content = file_get_contents($file);
102 4
        } else {
103
            throw new LoaderException(
104
                "Loader::loadFile(): {$file} does not exist!"
105
            );
106
        }
107
108
        return $content;
109
    }
110
}
111