TomlLoader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 18 3
A supports() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony\Config-loader.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yosymfony\ConfigLoader\Loaders;
13
14
use Yosymfony\Toml\Toml;
15
use Yosymfony\ConfigLoader\ConfigFileLoader;
16
use Yosymfony\ConfigLoader\Repository;
17
use Yosymfony\ConfigLoader\RepositoryInterface;
18
19
/**
20
 * TOML file loader
21
 *
22
 * @author Victor Puertas <[email protected]>
23
 */
24
class TomlLoader extends ConfigFileLoader
25
{
26
    public const TYPE = "toml";
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws RuntimeException If Yosymfony Toml package is not installed
32
     */
33
    public function load(string $resource, string $type = null) : RepositoryInterface
34
    {
35
        if (class_exists('Yosymfony\Toml\Toml') === false) {
36
            throw new \RuntimeException('"Yosymfony Toml" is required to read TOML files.');
37
        }
38
39
        $resourceContent = $resource;
40
        
41
        if (empty($type)) {
42
            $file = $this->getLocation($resource);
43
            $resourceContent = $this->readFile($file);
44
        }
45
46
        $parsedResource = Toml::parse($resourceContent);
47
        $repository = new Repository($parsedResource ?? []);
48
49
        return $this->parseImports($repository, $resource);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function supports(string $resource, string $type = null) : bool
56
    {
57
        return $type === self::TYPE || $this->hasResourceExtension($resource, 'toml');
58
    }
59
}
60