Config::setParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\Config;
4
5
use Bigwhoop\Trumpet\Config\Params\Param;
6
use Bigwhoop\Trumpet\Exceptions\InvalidArgumentException;
7
use Symfony\Component\Yaml\Yaml;
8
9
final class Config
10
{
11
    /** @var Param[] */
12
    private $params = [];
13
14
    public function setParam(string $name, Param $param)
15
    {
16
        $this->params[$name] = $param;
17
    }
18
    
19
    public function readTrumpetFile(string $path): Presentation
20
    {
21
        if (!is_readable($path)) {
22
            throw new InvalidArgumentException("Trumpet file '$path' must exist and be readable.");
23
        }
24
25
        $yaml = file_get_contents($path);
26
27
        try {
28
            $data = Yaml::parse($yaml, true, true);
29
        } catch (\Throwable $t) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
30
            throw new ConfigException($t->getMessage(), 0, $t);
31
        }
32
33
        if (!is_array($data)) {
34
            throw new ConfigException("Trumpet file '$path' is invalid. It probably is empty.");
35
        }
36
37
        $presentation = new Presentation();
38
39
        foreach ($data as $key => $value) {
40
            if (!array_key_exists($key, $this->params)) {
41
                throw new ConfigException("Trumpet file contains a key '$key' which is not supported.");
42
            }
43
44
            $this->params[$key]->parse($value, $presentation);
45
        }
46
47
        return $presentation;
48
    }
49
}
50