Passed
Push — master ( 76b08e...5cd6b2 )
by Ryuichi
58:40 queued 56:20
created

EntityManager   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 87.23%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 96
ccs 41
cts 47
cp 0.8723
rs 10
wmc 23

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setColumnMeta() 0 3 1
A __construct() 0 3 1
D getEntity() 0 59 21
1
<?php
2
3
namespace WebStream\Database;
4
5
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * EntityManager
9
 * @author Ryuichi TANAKA.
10
 * @since 2015/01/11
11
 * @version 0.7
12
 */
13
class EntityManager
14
{
15
    use Injector;
16
17
    /**
18
     * @var string エンティティクラスパス
19
     */
20
    private $classpath;
21
22
    /**
23
     * @var array<string> カラムメタ情報
24
     */
25
    private $columnMeta;
26
27
    /**
28
     * コンストラクタ
29
     * @param string エンティティクラスパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment エンティティクラスパス at position 0 could not be parsed: Unknown type name 'エンティティクラスパス' at position 0 in エンティティクラスパス.
Loading history...
30
     */
31 16
    public function __construct($classpath)
32
    {
33 16
        $this->classpath = $classpath;
34 16
    }
35
36
    /**
37
     * カラムメタ情報を設定する
38
     * @param string カラムメタ情報
0 ignored issues
show
Documentation Bug introduced by
The doc comment カラムメタ情報 at position 0 could not be parsed: Unknown type name 'カラムメタ情報' at position 0 in カラムメタ情報.
Loading history...
39
     */
40 16
    public function setColumnMeta(array $columnMeta)
41
    {
42 16
        $this->columnMeta = $columnMeta;
43 16
    }
44
45
    /**
46
     * 列データをエンティティに変換して返却する
47
     * @param array<string> 列データ
0 ignored issues
show
Documentation Bug introduced by
The doc comment 列データ at position 0 could not be parsed: Unknown type name '列データ' at position 0 in 列データ.
Loading history...
48
     * @return object 列データエンティティ
49
     */
50 16
    public function getEntity($row)
51
    {
52 16
        $instance = new $this->classpath();
53 16
        $propertyMap = null;
54
55
        // EntityPropertyを使っている場合はリフレクションを使用しない
56
        if (!array_key_exists(EntityProperty::class, class_uses($instance))) {
57 8
            $propertyMap = [];
58 8
            $refClass = new \ReflectionClass($instance);
59 8
            $properties = $refClass->getProperties();
60 8
            foreach ($properties as $property) {
61 8
                if ($property->isPrivate() || $property->isProtected()) {
62 8
                    $property->setAccessible(true);
63
                }
64 8
                $propertyMap[strtolower($property->getName())] = $property;
65
            }
66
        }
67
68 16
        foreach ($row as $col => $value) {
69 16
            switch ($this->columnMeta[$col]) {
70 16
                case 'LONG':      // mysql:int
71 16
                case 'SHORT':     // mysql:smallint
72 16
                case 'int4':      // postgres:int
73 16
                case 'int2':      // postgres:smallint
74 16
                case 'integer':   // sqlite:int
75 16
                case 'smallint':  // sqlite:smallint
76 16
                    $value = intval($value);
77 16
                    break;
78 16
                case 'LONGLONG':  // mysql:bigint
79 16
                case 'int8':      // postgres:bigint
80 16
                case 'bigint':    // sqlite:bigint
81
                    $value = doubleval($value);
82
                    break;
83 16
                case 'DATETIME':  // mysql:datetime
84 16
                case 'DATE':      // mysql:date
85 16
                case 'timestamp': // postgres:timestamp, sqlite:timestamp
86 16
                case 'date':      // postgres:date, sqlite:date
87
                    $value = new \DateTime($value);
88
                    break;
89
                default: // string
90 16
                    break;
91
            }
92
93 16
            $col = strtolower(preg_replace_callback('/_([a-zA-Z])/', function ($matches) {
94
                return ucfirst($matches[1]);
95 16
            }, $col));
96
97 16
            if ($propertyMap === null) {
98 8
                $instance->{$col} = $value;
99
            } else {
100 8
                if (array_key_exists($col, $propertyMap)) {
101 8
                    $propertyMap[$col]->setValue($instance, $value);
102
                } else {
103
                    $this->logger->error("Column '$col' is failed mapping in " . $this->classpath);
104
                }
105
            }
106
        }
107
108 16
        return $instance;
109
    }
110
}
111