ConfigurationFactory::build()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 4
nop 3
1
<?php
2
3
namespace Libcast\AssetDistributor\Configuration;
4
5
use Psr\Log\LoggerInterface;
6
7
class ConfigurationFactory
8
{
9
    /**
10
     *
11
     * @param $vendor
12
     * @param $path
13
     * @return Configuration
14
     * @throws \Exception
15
     */
16
    public static function build($vendor, $path, LoggerInterface $logger = null)
17
    {
18
        if (!$configuration = parse_ini_file($path, true)) {
19
            throw new \Exception("File '$path' is not a valid PHP configuration file");
20
        }
21
22
        if (!in_array($vendor, array_keys($configuration))) {
23
            throw new \Exception("Missing $vendor configuration");
24
        }
25
26
        $class = sprintf('\Libcast\AssetDistributor\%1$s\%1$sConfiguration', $vendor);
27
        if (!class_exists($class)) {
28
            throw new \Exception("Missing configuration class for $vendor");
29
        }
30
31
        return new $class($configuration[$vendor], $logger);
32
    }
33
34
    /**
35
     *
36
     * @param $path
37
     * @return array
38
     * @throws \Exception
39
     */
40
    public static function getVendors($path)
41
    {
42
        if (!$configuration = parse_ini_file($path, true)) {
43
            throw new \Exception("File '$path' is not a valid PHP configuration file");
44
        }
45
46
        return array_keys($configuration);
47
    }
48
}
49