Completed
Push — master ( 355804...f8d19f )
by James Ekow Abaka
02:36
created

ClassNameResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 52
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelClassName() 0 10 3
A getClassFileDetails() 0 9 2
A getJunctionClassName() 0 15 2
A getTableName() 0 6 1
A getDriverAdapterClassName() 0 4 1
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
    public function getModelClassName($default, $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
        if(self::$classResolver !== null && $model[0] !== "\\") {
28
            $resolver = self::$classResolver;
29
            $className = $resolver($model, $context);
0 ignored issues
show
Bug introduced by
The variable $model does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
        } else {
31
            $className = $model;
32
        }
33
        return $className;
34
    }
35
    
36
    private function getClassFileDetails($className)
37
    {
38
        $arrayed = explode('\\', $className);
39
        $class = array_pop($arrayed);
40
        if($arrayed[0] == '') {
41
            array_shift($arrayed);
42
        }
43
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
44
    }
45
46
    public function getJunctionClassName($classA, $classB)
47
    {
48
        $classA = $this->getClassFileDetails($classA);
49
        $classB = $this->getClassFileDetails($classB);
50
        if($classA['namespace'] != $classB['namespace']) {
51
            throw new NibiiException(
52
                "Cannot automatically join two classes of different "
53
                    . "namespaces. Please provide a model joiner or "
54
                    . "explicitly specify your joint model."
55
            );
56
        }
57
        $classes = [$classA['class'], $classB['class']];
58
        sort($classes);
59
        return "{$classA['namespace']}\\" . implode('', $classes);        
60
    }
61
62
    public function getTableName($instance)
63
    {
64
        $class = new \ReflectionClass($instance);
65
        $nameParts = explode("\\", $class->getName());
66
        return \ntentan\utils\Text::deCamelize(end($nameParts));           
67
    }
68
    
69
    public static function getDriverAdapterClassName()
70
    {
71
        return __NAMESPACE__ . '\adapters\\' . Text::ucamelize(Config::get('ntentan:db.driver')) . 'Adapter';
72
    }
73
}
74