Factory::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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