Passed
Push — master ( 7d2fba...2be253 )
by Jean Paul
01:17
created

StepLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 2
A step() 0 42 5
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 step ( string $parentClassName, string $stepName ) : Step
39
    {
40
        $parentClassShortName = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $parentClassShortName is dead and can be removed.
Loading history...
41
42
        try {
43
            $reflection = new ReflectionClass( $parentClassName );
44
45
            $parentClassShortName = $reflection->getShortName();
46
        } catch ( ReflectionException $reflectionException ) {
47
            $errorMessage = sprintf( "Something went wrong while trying to get the short class name: %s", $reflectionException->getMessage() );
48
            throw new SourceWatcherException( $errorMessage );
49
        } 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...
50
            $errorMessage = sprintf( "Something unexpected went wrong while trying to get the short class name: %s", $exception->getMessage() );
51
            throw new SourceWatcherException( $errorMessage );
52
        }
53
54
        $baseNameSpace = "Coco\\SourceWatcher\\Core";
55
56
        $packages = [ "Extractor" => sprintf( "%s\\%s", $baseNameSpace, "Extractors" ), "Transformer" => sprintf( "%s\\%s", $baseNameSpace, "Transformers" ), "Loader" => sprintf( "%s\\%s", $baseNameSpace, "Loaders" ) ];
57
58
        $subPackage = $packages[$parentClassShortName];
59
60
        $textUtils = new TextUtils();
61
62
        $step = null;
63
64
        try {
65
            $temporaryClassName = sprintf( "%s_%s", $stepName, $parentClassShortName );
66
67
            $pascalCaseClassName = $textUtils->textToPascalCase( $temporaryClassName );
68
69
            $fullyQualifiedClassName = sprintf( "%s\\%s", $subPackage, $pascalCaseClassName );
70
71
            if ( class_exists( $fullyQualifiedClassName ) ) {
72
                $step = new $fullyQualifiedClassName();
73
            }
74
        } catch ( Exception $exception ) {
75
            $errorMessage = sprintf( "Something unexpected went wrong while trying to load the step: %s", $exception->getMessage() );
76
            throw new SourceWatcherException( $errorMessage );
77
        }
78
79
        return $step;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $step could return the type null which is incompatible with the type-hinted return Coco\SourceWatcher\Core\Step. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
}
82