Passed
Push — master ( 2be253...46260f )
by Jean Paul
01:26
created

StepLoader::getStep()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
c 0
b 0
f 0
nc 13
nop 2
dl 0
loc 48
rs 8.5386
1
<?php
2
3
namespace Coco\SourceWatcher\Core;
4
5
use Coco\SourceWatcher\Utils\TextUtils;
6
use ReflectionClass;
7
use ReflectionException;
8
9
/**
10
 * Class StepLoader
11
 * @package Coco\SourceWatcher\Core
12
 */
13
class StepLoader
14
{
15
    /**
16
     * @var StepLoader
17
     */
18
    private static StepLoader $instance;
19
20
    /**
21
     * @return StepLoader
22
     */
23
    public static function getInstance () : StepLoader
24
    {
25
        if ( is_null( static::$instance ) ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
26
            static::$instance = new static;
27
        }
28
29
        return static::$instance;
30
    }
31
32
    /**
33
     * @param string $parentClassName
34
     * @param string $stepName
35
     * @return Step
36
     * @throws SourceWatcherException
37
     */
38
    public function getStep ( string $parentClassName, string $stepName ) : ?Step
39
    {
40
        if ( empty( $parentClassName ) ) {
41
            throw new SourceWatcherException( "The parent class name must be provided." );
42
        }
43
44
        if ( empty( $stepName ) ) {
45
            throw new SourceWatcherException( "The step name must be provided." );
46
        }
47
48
        try {
49
            $reflection = new ReflectionClass( $parentClassName );
50
51
            $parentClassShortName = $reflection->getShortName();
52
        } catch ( ReflectionException $reflectionException ) {
53
            $errorMessage = sprintf( "Something went wrong while trying to get the short class name: %s", $reflectionException->getMessage() );
54
            throw new SourceWatcherException( $errorMessage );
55
        } catch ( Exception $exception ) {
0 ignored issues
show
Bug introduced by
The type Coco\SourceWatcher\Core\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
56
            $errorMessage = sprintf( "Something unexpected went wrong while trying to get the short class name: %s", $exception->getMessage() );
57
            throw new SourceWatcherException( $errorMessage );
58
        }
59
60
        $baseNameSpace = "Coco\\SourceWatcher\\Core";
61
62
        $packages = [ "Extractor" => sprintf( "%s\\%s", $baseNameSpace, "Extractors" ), "Transformer" => sprintf( "%s\\%s", $baseNameSpace, "Transformers" ), "Loader" => sprintf( "%s\\%s", $baseNameSpace, "Loaders" ) ];
63
64
        $subPackage = $packages[$parentClassShortName];
65
66
        $textUtils = new TextUtils();
67
68
        $step = null;
69
70
        try {
71
            $temporaryClassName = sprintf( "%s_%s", $stepName, $parentClassShortName );
72
73
            $pascalCaseClassName = $textUtils->textToPascalCase( $temporaryClassName );
74
75
            $fullyQualifiedClassName = sprintf( "%s\\%s", $subPackage, $pascalCaseClassName );
76
77
            if ( class_exists( $fullyQualifiedClassName ) ) {
78
                $step = new $fullyQualifiedClassName();
79
            }
80
        } catch ( Exception $exception ) {
81
            $errorMessage = sprintf( "Something unexpected went wrong while trying to load the step: %s", $exception->getMessage() );
82
            throw new SourceWatcherException( $errorMessage );
83
        }
84
85
        return $step;
86
    }
87
}
88