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

DefaultModelFactory::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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