Completed
Push — master ( f8f144...94be68 )
by James Ekow Abaka
01:43
created

DefaultModelFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 82.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 46
ccs 19
cts 23
cp 0.8261
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createModel() 0 4 1
A getClassName() 0 4 1
A getModelTable() 0 6 1
A getJunctionClassName() 0 15 2
A getClassFileDetails() 0 8 2
1
<?php
2
3
namespace ntentan\nibii\factories;
4
5
use ntentan\utils\Text;
6
use ntentan\nibii\interfaces\ModelFactoryInterface;
7
8
class DefaultModelFactory implements ModelFactoryInterface
9
{
10
11 14
    public function createModel($className, $context)
12
    {
13 14
        return new $className();
14
    }
15
16
    public function getClassName($model)
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...
17
    {
18
        return $model;
19
    }
20
21 36
    public function getModelTable($instance)
22
    {
23 36
        $class = new \ReflectionClass($instance);
24 36
        $nameParts = explode("\\", $class->getName());
25 36
        return Text::deCamelize(end($nameParts));
26
    }
27
28 4
    public function getJunctionClassName($classA, $classB)
29
    {
30 4
        $classA = $this->getClassFileDetails($classA);
31 4
        $classB = $this->getClassFileDetails($classB);
32 4
        if ($classA['namespace'] != $classB['namespace']) {
33
            throw new NibiiException(
34
            "Cannot automatically join two classes of different "
35
            . "namespaces. Please provide a model joiner or "
36
            . "explicitly specify your joint model."
37
            );
38
        }
39 4
        $classes = [$classA['class'], $classB['class']];
40 4
        sort($classes);
41 4
        return "{$classA['namespace']}\\" . implode('', $classes);
42
    }
43
    
44 4
    private function getClassFileDetails($className) {
45 4
        $arrayed = explode('\\', $className);
46 4
        $class = array_pop($arrayed);
47 4
        if ($arrayed[0] == '') {
48 4
            array_shift($arrayed);
49
        }
50 4
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
51
    }    
52
53
}
54