TableConverterFactory::getClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 7
cp 0.5714
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 2.3149
1
<?php
2
namespace Pumpkin\Database;
3
4
/**
5
 * Class ConverterFactory
6
 * @package Pumpkin\Database
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class TableConverterFactory
10
{
11
12
    /**
13
     * @var string[]
14
     */
15
    private static $supportedFileExtensions;
16
17
    /**
18
     * @return string[]
19
     */
20 7
    public static function getSupportedFileExtensions()
21
    {
22 7
        if (null === self::$supportedFileExtensions) {
23 1
            foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'TableConverter') as $fileName) {
24 1
                $extension = substr($fileName, 0, -strlen('TableConverter.php'));
25 1
                if ($extension) {
26 1
                    self::$supportedFileExtensions[] = strtolower($extension);
27 1
                }
28 1
            }
29 1
        }
30 7
        return self::$supportedFileExtensions;
31
    }
32
33
    /**
34
     * @param Table $table
35
     * @return ITableConverter
36
     */
37 5
    public function build(Table $table)
38
    {
39 5
        $className = $this->getClassName($table->getDataPath());
40 4
        return new $className($table);
41
    }
42
43
    /**
44
     * @param string $path
45
     * @return string
46
     * @throws \RuntimeException
47
     */
48 4
    private function getClassName($path)
49
    {
50 4
        $className = 'Pumpkin\Database\TableConverter\\' . pathinfo($path, PATHINFO_EXTENSION) . 'TableConverter';
51 4
        if (class_exists($className)) {
52 4
            return $className;
53
        }
54
        throw new \RuntimeException(
55
            sprintf('This file extension is not supported: "%s"', pathinfo($path, PATHINFO_EXTENSION))
56
        );
57
    }
58
}
59