Completed
Push — master ( a30571...7a668c )
by James Ekow Abaka
02:45
created

DefaultModelFactory::getJunctionClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 2.0438
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 36
    public function getModelTable($instance)
17
    {
18 36
        $class = new \ReflectionClass($instance);
19 36
        $nameParts = explode("\\", $class->getName());
20 36
        return Text::deCamelize(end($nameParts));
21
    }
22
23 4
    public function getJunctionClassName($classA, $classB)
24
    {
25 4
        $classA = $this->getClassFileDetails($classA);
26 4
        $classB = $this->getClassFileDetails($classB);
27 4
        if ($classA['namespace'] != $classB['namespace']) {
28
            throw new NibiiException(
29
            "Cannot automatically join two classes of different "
30
            . "namespaces. Please provide a model joiner or "
31
            . "explicitly specify your joint model."
32
            );
33
        }
34 4
        $classes = [$classA['class'], $classB['class']];
35 4
        sort($classes);
36 4
        return "{$classA['namespace']}\\" . implode('', $classes);
37
    }
38
    
39 4
    private function getClassFileDetails($className) {
40 4
        $arrayed = explode('\\', $className);
41 4
        $class = array_pop($arrayed);
42 4
        if ($arrayed[0] == '') {
43 4
            array_shift($arrayed);
44
        }
45 4
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
46
    }    
47
48
}
49