Completed
Push — master ( 7e591a...ced319 )
by James Ekow Abaka
02:48
created

Nibii::getModelTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0933

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
ccs 5
cts 7
cp 0.7143
cc 2
eloc 8
nc 2
nop 1
crap 2.0933
1
<?php
2
3
namespace ntentan\nibii;
4
5
class Nibii
6
{
7
    private static $classResolver;
8
    private static $modelResolver;
9
    private static $modelJoiner;
10
    private static $tableResolver;
11
12 4
    public static function load($path)
13
    {
14 4
        return (new \ReflectionClass(self::getClassName($path)))->newInstance();
15
    }
16
    
17 4
    private static function getClassFileDetails($className)
18
    {
19 4
        $arrayed = explode('\\', $className);
20 4
        $class = array_pop($arrayed);
21 4
        if($arrayed[0] == '') {
22 4
            array_shift($arrayed);
23 4
        }
24 4
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
25
    }
26
    
27 4
    public static function joinModels($classA, $classB)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
28
    {
29 4
        if(self::$modelJoiner) {
30
            $modelJoiner = self::$modelJoiner;
31
            return $modelJoiner($classA, $classB);
32
        } else {
33 4
            $classA = self::getClassFileDetails($classA);
34 4
            $classB = self::getClassFileDetails($classB);
35 4
            if($classA['namespace'] != $classB['namespace']) {
36
                throw new NibiiException(
37
                    "Cannot automatically join two classes of different "
38
                        . "namespaces. Please provide a model joiner or "
39
                        . "explicitly specify your joint model."
40
                );
41
            }
42 4
            $classes = [$classA['class'], $classB['class']];
43 4
            sort($classes);
44 4
            return "{$classA['namespace']}\\" . implode('', $classes);
45
        }
46
    }
47
    
48 36
    public static function getModelTable($instance)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50 36
        if(self::$tableResolver) {
51
            $tableResolver = self::$tableResolver;
52
            return $tableResolver($instance);
53
        } else {
54 36
            $class = new \ReflectionClass($instance);
55 36
            $nameParts = explode("\\", $class->getName());
56 36
            return \ntentan\utils\Text::deCamelize(end($nameParts));            
57
        }
58
    }
59
60 12
    public static function getClassName($model, $context = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
    {
62 12
        if(self::$classResolver !== null && $model[0] !== "\\") {
63
            $resolver = self::$classResolver;
64
            $className = $resolver($model, $context);
65
        } else {
66 12
            $className = $model;
67
        }
68 12
        return $className;
69
    }
70
    
71 8
    public static function getModelName($class)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
72
    {
73 8
        if(self::$modelResolver) {
74
            $modelResolver = self::$modelResolver;
75
            return $modelResolver($class);
76
        } else {
77 8
            return $class;
78
        }
79
    }
80
81
    public static function setClassResolver($classResolver)
82
    {
83
        self::$classResolver = $classResolver;
84
    }
85
    
86
    public static function setModelJoiner($modelJoiner)
87
    {
88
        self::$modelJoiner = $modelJoiner;
89
    }
90
    
91
    public static function setTableResolver($tableResolver)
92
    {
93
        self::$tableResolver = $tableResolver;
94
    }
95
}
96