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

OrmEntityAnalyzer::getTableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
crap 1
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