Factory::getFormatClass()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Shared\FormatFiles;
4
5
6
use kalanis\kw_mapper\Interfaces\IFileFormat;
7
use kalanis\kw_mapper\MapperException;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Class Factory
14
 * @package kalanis\kw_mapper\Storage\Shared\FormatFiles
15
 */
16
class Factory
17
{
18 22
    public static function getInstance(): self
19
    {
20 22
        return new self();
21
    }
22
23
    /**
24
     * @param string $path
25
     * @throws MapperException
26
     * @return IFileFormat
27
     */
28 22
    public function getFormatClass(string $path): IFileFormat
29
    {
30
        try {
31
            /** @var class-string $path */
32 22
            $reflect = new ReflectionClass($path);
33 21
            $instance = $reflect->newInstance();
34 1
        } catch (ReflectionException $ex) {
35 1
            throw new MapperException(sprintf('Wanted class *%s* not exists!', $path), $ex->getCode(), $ex);
36
        }
37 21
        if (!$instance instanceof IFileFormat) {
38 1
            throw new MapperException(sprintf('Defined class *%s* is not instance of IFileFormat!', $path));
39
        }
40 20
        return $instance;
41
    }
42
}
43