Completed
Push — master ( 145f77...f73370 )
by James Ekow Abaka
04:01
created

ClassNameResolver::getDriverAdapterClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace ntentan\nibii;
10
11
use ntentan\nibii\interfaces\ClassResolverInterface;
12
use ntentan\nibii\interfaces\ModelJoinerInterface;
13
use ntentan\nibii\interfaces\TableNameResolverInterface;
14
use ntentan\config\Config;
15
use ntentan\utils\Text;
16
17
/**
18
 * Description of DefaultClassResolver
19
 *
20
 * @author ekow
21
 */
22
class ClassNameResolver implements ClassResolverInterface, ModelJoinerInterface,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
23
    TableNameResolverInterface
24
{
25 14
    public function getModelClassName($className, $context)
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...
26
    {
27 14
        return $className;
28
    }
29
    
30 4
    private function getClassFileDetails($className)
31
    {
32 4
        $arrayed = explode('\\', $className);
33 4
        $class = array_pop($arrayed);
34 4
        if($arrayed[0] == '') {
35 4
            array_shift($arrayed);
36
        }
37 4
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
38
    }
39
40 4
    public function getJunctionClassName($classA, $classB)
41
    {
42 4
        $classA = $this->getClassFileDetails($classA);
43 4
        $classB = $this->getClassFileDetails($classB);
44 4
        if($classA['namespace'] != $classB['namespace']) {
45
            throw new NibiiException(
46
                "Cannot automatically join two classes of different "
47
                    . "namespaces. Please provide a model joiner or "
48
                    . "explicitly specify your joint model."
49
            );
50
        }
51 4
        $classes = [$classA['class'], $classB['class']];
52 4
        sort($classes);
53 4
        return "{$classA['namespace']}\\" . implode('', $classes);        
54
    }
55
56 36
    public function getTableName($instance)
57
    {
58 36
        $class = new \ReflectionClass($instance);
59 36
        $nameParts = explode("\\", $class->getName());
60 36
        return \ntentan\utils\Text::deCamelize(end($nameParts));           
61
    }
62
    
63
    public static function getDriverAdapterClassName()
64
    {
65
        $driver = Config::get('ntentan:db.driver', false);
66
        if($driver) {
67
            return __NAMESPACE__ . '\adapters\\' . Text::ucamelize(Config::get('ntentan:db.driver')) . 'Adapter';
68
        } 
69
        throw new NibiiException("Please specify a driver");
70
    }
71
}
72