Completed
Push — master ( 126036...3fa63d )
by Maik
07:56
created

OrmEntityAnalyzer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 8
c 5
b 1
f 0
lcom 1
cbo 2
dl 0
loc 84
ccs 30
cts 30
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 8 1
B getPrimaryKey() 0 25 4
A getPrimaryKeyCol() 0 14 3
1
<?php
2
namespace Nkey\Caribu\Orm;
3
4
trait OrmEntityAnalyzer
5
{
6
    /**
7
     * Include mapping related functionality
8
     */
9
    use OrmMapping;
10
11
    /**
12
     * Get the name of table
13
     *
14
     * @param string $class The name of class
15
     *
16
     * @return string The name of table
17
     *
18
     * @throws OrmException
19
     */
20 32
    private static function getTableName($class)
21
    {
22 32
        $parts = explode('\\', $class);
23 32
        $simpleClassName = end($parts);
24 32
        $tableName = strtolower($simpleClassName);
25 32
        $tableName = preg_replace('#model$#', '', $tableName);
26 32
        return self::getAnnotatedTableName($class, $tableName);
27
    }
28
29
    /**
30
     * Retrieve the primary key value
31
     *
32
     * @param string $class The name of class where to retrieve the primary key value
33
     *
34
     * @return array Pair of column name and value of primary key
35
     *
36
     * @throws OrmException
37
     */
38 16
    private static function getPrimaryKey($class, $object, $onlyValue = false)
39
    {
40 16
        $primaryKey = self::getAnnotatedPrimaryKey($class, $object, $onlyValue);
41
42 16
        if (null !== $primaryKey) {
43 12
            return $primaryKey;
44
        }
45
46 4
        $pkCol = self::getPrimaryKeyCol($class);
47 4
        $method = sprintf("get%s", ucfirst($pkCol));
48
49
        try {
50 4
            $rfMethod = new \ReflectionMethod($class, $method);
51 3
            $primaryKey = $rfMethod->invoke($object);
52 3
            if (!$onlyValue) {
53
                $primaryKey = array(
54
                    $pkCol => $primaryKey
55 3
                );
56 3
            }
57 4
        } catch (\ReflectionException $exception) {
58 1
            throw OrmException::fromPrevious($exception);
59
        }
60
61 3
        return $primaryKey;
62
    }
63
64
    /**
65
     * Retrieve the name of column which represents the primary key
66
     *
67
     * @param string $class The name of class
68
     *
69
     * @return string The name of primary key column
70
     *
71
     * @throws OrmException
72
     */
73 17
    private static function getPrimaryKeyCol($class)
74
    {
75 17
        $instance = self::getInstance();
76
77 17
        $pkColumn = self::getAnnotatedPrimaryKeyColumn($class);
78 17
        if (null === $pkColumn) {
79 5
            $pkColumn = $instance->getDbType()->getPrimaryKeyColumn(self::getTableName($class), $instance);
80 5
        }
81 17
        if (null === $pkColumn) {
82 1
            $pkColumn = 'id';
83 1
        }
84
85 17
        return $pkColumn;
86
    }
87
}
88